-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFLoofFill.java
More file actions
37 lines (31 loc) · 1.15 KB
/
Copy pathFLoofFill.java
File metadata and controls
37 lines (31 loc) · 1.15 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
import java.util.Scanner;
public class FLoofFill {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int m = scn.nextInt();
int[][]arr = new int[n][m];
for(int i = 0 ; i<n ; i++){
for(int j = 0 ; j< m ;j ++){
arr[i][j] = scn.nextInt();
}
}
boolean[][] visited = new boolean[n][m];
floodfill(arr , 0 , 0 ,"", visited);
}
public static void floodfill(int[][] maze , int row, int col , String asf, 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(asf);
return;
}
visited[row][col] = true;
floodfill(maze , row -1 , col ,asf+"t" , visited);
floodfill(maze , row , col -1,asf+"l" , visited);
floodfill(maze , row+1 , col ,asf+"d" , visited);
floodfill(maze , row , col+1 ,asf+"r" , visited);
visited[row][col] = false;
}
}