forked from monuverma1501/HactoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rats in Maze
64 lines (54 loc) · 1.18 KB
/
Rats in Maze
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
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;
bool ratInMaze(char maze[][1005],int sol[][1005],int i,int j,int m,int n)
{
if(i==m && j==n)
{
sol[i][j] = 1;
for(int row=0;row<=m;row++)
{
for(int col=0;col<=n;col++)
{
cout<<sol[row][col]<<" ";
}
cout<<endl;
}
cout<<endl;
return true;
}
//Rat should be inside the maze
if(i>m || j>n)
return false;
//Should not encounter a blocked cell
if(maze[i][j] == 'X')
return false;
sol[i][j] = 1;
bool rightSuccess = ratInMaze(maze,sol,i,j+1,m,n);
if(rightSuccess){
return true ;
}
bool downSuccess = ratInMaze(maze,sol,i+1,j,m,n);
if(downSuccess){
return true ;
}
//Backtrack
sol[i][j] = 0;
return false;
}
int main()
{
char maze[1005][1005];
int sol[1005][1005]= {0};
int m ; //No of rows
int n ; //No of cols
cin >> m >> n;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin >> maze[i][j];
}
}
bool ans = ratInMaze(maze,sol,0,0,m-1,n-1);
if(!ans)
cout<<"-1";
return 0;
}