# 1. Introduction

Finding the shortest path in a maze is a classic problem in computer science and algorithms. The problem can be effectively solved using a breadth-first search (BFS) algorithm. The maze is typically represented as a 2D array where open cells are denoted by 1 and blocked cells are denoted by 0. The BFS approach ensures that we explore all possible paths in increasing order of their length, hence guaranteeing that the first path we find to the destination is the shortest.

# 2. Program Steps

1. Represent the maze as a 2D array.

2. Define a Node class which will represent a point in the maze.

3. Initialize a queue to keep track of nodes to explore.

4. Add the start node to the queue and begin BFS.

5. For each node, explore its valid neighbors (up, down, left, right) that haven’t been visited yet.

6. Add these neighbors to the queue and mark them as visited.

7. Continue this process until the destination is found or all possible paths are explored.

8. If the destination is reached, display the shortest path length.

# 3. Code Program

```
import java.util.LinkedList;
import java.util.Queue;
public class MazeShortestPath {
private static final int ROW = 4, COL = 4;
// These arrays are used to get row and column
// numbers of 4 neighbors of a given cell
private static int rowNum[] = {-1, 0, 0, 1};
private static int colNum[] = {0, -1, 1, 0};
static class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
static class Node {
Point point;
int distance;
Node(Point point, int distance) {
this.point = point;
this.distance = distance;
}
}
static boolean isValid(int row, int col, boolean[][] visited, int[][] maze) {
return (row >= 0) && (row < ROW) && (col >= 0) && (col < COL) && (maze[row][col] == 1 && !visited[row][col]);
}
public static int BFS(int[][] maze, Point src, Point dest) {
boolean[][] visited = new boolean[ROW][COL];
Queue<Node> queue = new LinkedList<>();
Node s = new Node(src, 0);
queue.add(s);
visited[src.x][src.y] = true;
while (!queue.isEmpty()) {
Node curr = queue.poll();
Point pt = curr.point;
if (pt.x == dest.x && pt.y == dest.y) {
return curr.distance;
}
for (int i = 0; i < 4; i++) {
int row = pt.x + rowNum[i];
int col = pt.y + colNum[i];
if (isValid(row, col, visited, maze)) {
visited[row][col] = true;
Node adjacentCell = new Node(new Point(row, col), curr.distance + 1);
queue.add(adjacentCell);
}
}
}
return Integer.MAX_VALUE;
}
public static void main(String[] args) {
int[][] maze = {
{1, 0, 1, 1},
{1, 1, 1, 0},
{0, 1, 0, 1},
{1, 1, 1, 1}
};
Point source = new Point(0, 0);
Point dest = new Point(3, 3);
int dist = BFS(maze, source, dest);
if (dist != Integer.MAX_VALUE)
System.out.println("Shortest Path is " + dist);
else
System.out.println("Shortest Path doesn't exist");
}
}
```

### Output:

Shortest Path is 7

# 4. Step By Step Explanation

1. The maze is represented as a 2D array of integers, where *1* represents an open path and *0* represents a blocked path.

2. The *Point* class is used to represent a cell in the maze.

3. The *Node* class encapsulates a point and its current distance from the source.

4. The *isValid* function checks if a potential move is within the maze, unvisited, and leads to an open path.

5. The *BFS* method carries out the breadth-first search. It takes the maze, the source point, and the destination point as parameters.

6. A queue is used to maintain nodes that are pending exploration. We start with the source node and mark it as visited.

7. For each node being explored, we look at its neighbors in all four directions (up, down, left, right).

8. Valid neighbors that haven't been visited are added to the queue.

9. This process continues until the destination is reached or there are no more nodes left to explore.

10. If the destination is reached, we return the distance (i.e., the length of the shortest path). If not, we return *Integer.MAX_VALUE* indicating the path doesn't exist.

11. In the *main* method, a sample maze is created, BFS is invoked, and the result is displayed.