liveBook · Manning

Maze

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A navigational problem space consisting of connected locations and passages with a goal of finding a path between locations.

A maze is a grid of locations used to test pathfinding. Each cell is empty or blocked; a search starts at one location and tries to reach a goal. A randomly generated maze replaces some empty cells with walls according to a sparseness setting, but a path is not guaranteed. section 2.2

func successorsForMaze(_ maze: Maze) -> (MazeLocation) -> [MazeLocation] {
func successors(ml: MazeLocation) -> [MazeLocation] { //no  diagonals
    var newMLs: [MazeLocation] = [MazeLocation]()
    if (ml.row + 1 < maze.count) && (maze[ml.row + 1][ml.col] != .Blocked) {
        newMLs.append(MazeLocation(row: ml.row + 1, col: ml.col))
    }
    if (ml.row - 1 >= 0) && (maze[ml.row - 1][ml.col] != .Blocked) {
        newMLs.append(MazeLocation(row: ml.row - 1, col: ml.col))
    }
    if (ml.col + 1 < maze[0].count) && (maze[ml.row][ml.col + 1] != .Blocked) {
        newMLs.append(MazeLocation(row: ml.row, col: ml.col + 1))
    }
    if (ml.col - 1 >= 0) && (maze[ml.row][ml.col - 1] != .Blocked) {
        newMLs.append(MazeLocation(row: ml.row, col: ml.col - 1))
    }

    return newMLs
}
    return successors
}

The successor function turns the grid into a search problem: from a location, it returns unblocked neighbors one step up, down, left, or right, without crossing the grid boundary. section 2.2

A location stores its row and column and is hashable so search algorithms can track visited places. Depth-first search follows a path until it must backtrack; breadth-first search explores nearer locations first. A* can use Manhattan distance to guide movement toward the goal. Once a search finds the goal, parent links reconstruct the path for marking on the printed maze. section 2.2

Try a small maze

Generate a 10-by-10 grid with sparseness 0.2. Search from (0, 0) to (9, 9); if a solution exists, mark its route with P, its start with S, and its goal with G. section 2.2

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Builds random generation, locations, legal moves, goal testing, and path display; does not guarantee a generated maze has a solution.
Examples
Runs searches on randomly generated 10-by-10 grids and prints routes marked with start, goal, and path characters.
What it assumes
Assumes basic Swift programming while defining the maze representation and search-specific location type.
What you can do after
Build it from scratch in Swift, then run a pathfinding algorithm and display a route when one exists.
How it connects
Connects hashable states and successor functions to graph search, and Manhattan distance to A* heuristics.

Also explained in

Compare how 2 books explain maze →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.