forked from darecoder/Crux-DS-Algo-In-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
executable file
·48 lines (35 loc) · 1.1 KB
/
Solution.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
package com.company.Doubts;
import java.util.*;
public class Solution{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int col = s.nextInt();
boolean[][] maze = new boolean[row][col]; //By default all the value are false.
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
int n = s.nextInt();
if(n == 1){
maze[i][j] = true;
}
}
}
maze("", 0,0,row-1,col-1,maze);
}
public static void maze(String path, int c_row, int c_col,int row,int col, boolean[][] maze){
if(c_row == row && c_col == col){
System.out.println(path);
return;
}
if(c_row> row || c_col > col){
return;
}
if(!maze[c_row][c_col]){
return;
}
maze[c_row][c_col] = false;
maze(path + "R",c_row,c_col+1,row,col,maze);
maze(path + "D",c_row+1,c_col,row,col,maze);
maze[c_row][c_col] = true;
}
}