forked from OpenGenus/cosmos
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrat_in_maze.cpp
More file actions
67 lines (51 loc) · 1.3 KB
/
rat_in_maze.cpp
File metadata and controls
67 lines (51 loc) · 1.3 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
65
66
67
#include<iostream>
using namespace std;
///i,j = current cell, m,n = destination
bool solveMaze(char maze[][10],int sol[][10],int i,int j,int m,int n,int &ways){
///Base Case
if(i==m && j==n){
sol[m][n] = 1;
ways++;
///Print the soln
}
///Rec Case
///Assume jis cell pe khada hai vaha se rasta hai
sol[i][j] = 1;
/// Right mein X nahi hai
if(j+1<=n && maze[i][j+1]!='X'){
bool rightSeRastaHai = solveMaze(maze,sol,i,j+1,m,n,ways);
if(rightSeRastaHai){
// return true;
}
}
/// Down jake dekho agr rasta nahi mila right se
if(i+1<=m && maze[i+1][j]!='X'){
bool downSeRastaHai = solveMaze(maze,sol,i+1,j,m,n,ways);
if(downSeRastaHai){
//return true;
}
}
///Agr code is line mein aagya ! - Backtracking
sol[i][j] =0;
return false;
}
int main(){
char maze[10][10] = {
"00XXX",
"00000",
"0XX00",
"000X0",
"0X000"
};
int m = 4,n=4;
int sol[10][10] = {0};
int ways=0;
solveMaze(maze,sol,0,0,m,n,ways);
if(ways!=0){
cout<<"Total ways "<<ways<<endl;
}
else{
cout<<"Koi rasta nahi hai "<<endl;
}
return 0;
}