-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfloodfill.java
48 lines (32 loc) · 1.46 KB
/
floodfill.java
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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int m = Integer.parseInt(br.readLine());
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
String str = br.readLine();
for (int j = 0; j < m; j++) {
arr[i][j] = Integer.parseInt(str.split(" ")[j]);
}
}
floodfill(arr, 0, 0, "", new boolean[n][m]);
}
public static void floodfill(int[][] maze, int row, int col, String psf, boolean[][] visited){
if(row < 0 || col < 0 || row >= maze.length || col >= maze[0].length ||
maze[row][col] == 1 || visited[row][col] == true){
return;
} else if(row == maze.length - 1 && col == maze[0].length - 1){
System.out.println(psf);
return;
}
visited[row][col] = true;
floodfill(maze, row - 1, col, psf + "t", visited);
floodfill(maze, row, col - 1, psf + "l", visited);
floodfill(maze, row + 1, col, psf + "d", visited);
floodfill(maze, row, col + 1, psf + "r", visited);
visited[row][col] = false;
}
}