-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRatInMaze.java
More file actions
56 lines (41 loc) · 1.4 KB
/
RatInMaze.java
File metadata and controls
56 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
public class RatInMaze {
private static int countPaths = 0;
private static boolean isSafe(int[][] maze, int x, int y, boolean[][] visited) {
return (x >= 0 && x < maze.length && y >= 0 && y < maze.length && maze[x][y] == 1 && !visited[x][y]);
}
private static void solveMaze(int[][] maze, int x, int y, boolean[][] visited) {
int N = maze.length;
if (x == N - 1 && y == N - 1) {
countPaths++;
return;
}
visited[x][y] = true;
if (isSafe(maze, x + 1, y, visited)) {
solveMaze(maze, x + 1, y, visited);
}
if (isSafe(maze, x, y + 1, visited)) {
solveMaze(maze, x, y + 1, visited);
}
if (isSafe(maze, x - 1, y, visited)) {
solveMaze(maze, x - 1, y, visited);
}
if (isSafe(maze, x, y - 1, visited)) {
solveMaze(maze, x, y - 1, visited);
}
visited[x][y] = false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] maze = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
maze[i][j] = sc.nextInt();
}
}
boolean[][] visited = new boolean[N][N];
solveMaze(maze, 0, 0, visited);
System.out.println(countPaths);
}
}