-
Notifications
You must be signed in to change notification settings - Fork 0
/
rat_maze_find_path.cpp
90 lines (70 loc) · 1.42 KB
/
rat_maze_find_path.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#define loop(i,a,b) for(int i=a;i<b;i++)
#include <cmath>
#include <cstdio>
#include <queue>
#include <iostream>
#include <algorithm>
#define size 1000
using namespace std;
#define N 4
int count = 0;
bool isSafe(bool maze[N][N],int x,int y){
if( !maze[x][y] || x >= N || y >= N ){
return false;
}
else{
return true;
}
}
void print_path(bool path[N][N]){
loop(i,0,N){
loop(j,0,N){
cout<<path[i][j]<<" ";
}
cout<<"\n";
}
}
bool solve_mazeUtil(bool maze[N][N],int x,int y,bool path[N][N]){
print_path(path);
cout<<endl<<endl;
if ( x==N-1 && y==N-1) {
path[x][y] = 1;
return true;
}
if ( isSafe(maze,x,y) ) {
path[x][y] = 1;
if ( solve_mazeUtil(maze,x+1,y,path) ){
return true;
}
if ( solve_mazeUtil(maze,x,y+1,path) ){
return true;
}
path[x][y] = 0;
return false;
}
else{
return false;
}
}
void solve(bool maze[N][N]){
bool path[N][N] = {{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}};
if ( solve_mazeUtil(maze,0,0,path) ){
cout<<"YES";
}
else{
cout<<"NO";
}
}
int main() {
bool maze[N][N] = {
{1, 1, 1, 0},
{0, 0, 1, 1},
{1, 1, 1, 0},
{1, 1, 1, 1}
};
solve(maze);
return 0;
}