-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRatInAMaze.cpp
More file actions
64 lines (57 loc) · 1.04 KB
/
RatInAMaze.cpp
File metadata and controls
64 lines (57 loc) · 1.04 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
57
58
59
60
61
62
63
64
// Rat in a maze
#include<iostream>
using namespace std;
const int D=5;
bool isSafe(int in[D][D],int out[D][D],int m, int n,int i,int j){
while(i<m && j<n){
if(in[i][j]==1){
return false;
}
else{
return true;
}
}
return false;
}
bool RatInAMaze(int in[D][D],int out[D][D],int m, int n, int i, int j){
// base case
if(i == m-1 && j == n-1){
out[m-1][n-1]=1;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cout<<out[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<"--------------------";
cout<<endl;
return false;
}
out[i][j] = 1;
//recursive case
if(isSafe(in,out,m,n,i,j+1)){
bool success = RatInAMaze(in,out,m,n,i,j+1);
if(success==true){
return true;
}
}
if(isSafe(in,out,m,n,i+1,j)){
bool success = RatInAMaze(in,out,m,n,i+1,j);
if(success==true){
return true;
}
}
out[i][j]=0;
return false;
}
int main(){
int out[5][5] = {0};
int m;
cin>>m;
int n;
cin>>n;
int in[5][5] = {{0,0,0,0,1},{0,1,0,1,0},{0,0,0,1,0},{1,0,0,0,0},{1,0,0,0,0}};
RatInAMaze(in,out,m,n,0,0);
return 0;
}