Maze Solver program in Java.

import java.util.*;
import java.io.*;
class maze2
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Integer arr[][] = {{0,0,0,0,9},
{1,0,0,1,0},
{0,1,1,0,0},
{1,0,1,1,1},
{8,0,0,0,0}};
int size = arr.length*arr[0].length;
ArrayList<ArrayList<Integer>> maze = new ArrayList<ArrayList<Integer>>(size);
int[] index = {0};
Arrays.asList(arr).forEach(row -> {
maze.add(new ArrayList<Integer>());
Arrays.asList(row).forEach(item -> maze.get(index[0]).add(item));
index[0]++;
});
Graph graph = new Graph(size,maze);
// System.out.println(maze);
System.out.println("\ndfs");
graph.dfs(20,4);
}
}
class Graph {
int V ;
int start , end ;
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>() ;
ArrayList<ArrayList<Integer>> maze = new ArrayList<ArrayList<Integer>>() ;
ArrayList<Integer> walls = new ArrayList<>();
public Graph(int V,ArrayList<ArrayList<Integer>> maze) {
this.V = V;
this.maze = maze;
for(int i=0;i<V;i++)
list.add(new ArrayList<Integer>());
addWalls();
addEdge();
int count=0;
for(ArrayList<Integer> aaa : list)
{
System.out.print((count++)+ " : ");
System.out.println(aaa);
}
System.out.println("Start :" +start);
System.out.println("End :"+end);
bfs(this.start,this.end);
}
void addEdge() {
for(int i=0;i<V;i++) {
if(!walls.contains(i)) _addEdge(i);
}
}
void _addEdge(int node) {
// up = node-5 // down = node +5 //right = node +1 //left = node-1
int col = (node+1)%5;
int up = (node<5) ? -1 :node-5;
int down =(node>19)?-1 :node+5;
int r = (col==0)?-1 :node+1;
int l = (col==1)?-1 :node-1;
int tr = (node<5 || col==0)?-1:up+1;
int tl = (node<5 || col==1)?-1:up-1;
int dr = (node>19 || col==0)?-1:down+1;
int dl = (node>19 || col==1)?-1:down-1;
int[] arr = {up,down,r,l,dr,dl,tr,tl};
for(int i=0;i<8;i++)
{
if(arr[i]>=0 && !walls.contains(arr[i])) list.get(node).add(arr[i]);
}
}
void dfs(int start,int end) {
boolean[] visited = new boolean[V];
int[] parent = new int[V];
Stack<Integer> stack = new Stack<>();
visited[start] = true;
parent[start] = -1;
stack.push(start);
while(!stack.isEmpty()) {
int p = stack.pop();
// System.out.print(p+" : ");
if(p == end) break;
for(int neigh : list.get(p)) {
if(!visited[neigh]) {
visited[neigh] = true;
parent[neigh] = p;
stack.push(neigh);
}
}
}
int cur = end;
while(parent[cur] !=-1){
System.out.print(cur+" : ");
cur = parent[cur];
}
System.out.println(cur);
}
void bfs(int start , int end){
boolean[] visited = new boolean[V];
int[] parent = new int[V];
Queue<Integer> q = new LinkedList<Integer>();
visited[start] = true;
parent[start] = -1;
q.add(start);
while(!q.isEmpty()) {
int p = q.poll();
if(p == end) break;
for(int neigh : list.get(p)) {
if(!visited[neigh]) {
visited[neigh] = true;
parent[neigh] = p;
q.add(neigh);
}
}
}
int curr = end;
while(parent[curr] !=-1) {
System.out.print(curr+" => ");
curr = parent[curr];
}
System.out.print(curr);
}
void addWalls() {
int[] ind = {0};
maze.forEach(row -> row.forEach(num -> {
if(num == 1) walls.add(ind[0]);
if(num == 8) this.start = ind[0];
if(num == 9) this.end = ind[0];
ind[0]++;
}));
}
}
Output :
0 : [1, 6]
1 : [6, 2, 0, 7]
2 : [7, 3, 1, 6]
3 : [4, 2, 9, 7]
4 : [9, 3]
5 : []
6 : [1, 7, 10, 2, 0]
7 : [2, 6, 13, 3, 1]
8 : []
9 : [4, 14, 13, 3]
10 : [16, 6]
11 : []
12 : []
13 : [14, 9, 7]
14 : [9, 13]
15 : []
16 : [21, 22, 20, 10]
17 : []
18 : []
19 : []
20 : [21, 16]
21 : [16, 22, 20]
22 : [23, 21, 16]
23 : [24, 22]
24 : [23]
Start :20
End :4
4 => 3 => 7 => 6 => 10 => 16 => 20
dfs
4 : 3 : 2 : 6 : 10 : 16 : 20