-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Rat_In_A_Maze.cpp
50 lines (40 loc) · 1.16 KB
/
Rat_In_A_Maze.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
#include<iostream>
#include<vector>
using namespace std;
void solve(vector<vector<int>> maze,int n, vector<vector<bool>> &visited, int i, int j, string path, vector<string> &ans)
{
if(i==n-1 && j==n-1)
{
ans.push_back(path);
return;
}
visited[i][j] = 1;
if(i+1<n &&maze[i+1][j]==1 && visited[i+1][j]!=1)
solve(maze,n,visited,i+1,j,path+'D',ans);
if(j-1>-1 && maze[i][j-1]==1 && visited[i][j-1]!=1)
solve(maze,n,visited,i,j-1,path+'L',ans);
if(j+1<n && maze[i][j+1]==1 && visited[i][j+1]!=1)
solve(maze,n,visited,i,j+1,path+'R',ans);
if(i-1>-1 && maze[i-1][j]==1 && visited[i-1][j]!=1)
solve(maze,n,visited,i-1,j,path+'U',ans);
visited[i][j] = 0;
}
vector<string> SearchMaze(vector<vector<int>> maze, int n)
{
vector<string> ans;
if(maze[0][0]==0)
return ans;
vector<vector<bool>> visited (n,vector<bool>(n,0));
string path = "";
solve(maze,n,visited,0,0,path,ans);
return ans;
}
int main()
{
vector<vector<int>> maze = {{1,1},{1,1}};
vector<string> ans = SearchMaze(maze,2);
for(auto i: ans)
cout<<i<<" ";
cout<<endl;
return 0;
}