-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.cpp
39 lines (34 loc) · 1004 Bytes
/
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
#include <bits/stdc++.h>
using namespace std;
vector<string> v1;
void path(vector<vector<int> >maze,int n,int m,int cr,int cc,string ans){
if(cr<0 || cc<0 || cr==n || cc==m || maze[cr][cc]==0)return;
if(cr==n-1 && cc==m-1){
v1.push_back(ans);
return;
}
maze[cr][cc]=0;
path(maze,n,m,cr-1,cc,ans+'t');//top
path(maze,n,m,cr+1,cc,ans+'b');//bottom
path(maze,n,m,cr,cc-1,ans+'l');//left
path(maze,n,m,cr,cc+1,ans+'r');//right
maze[cr][cc]=1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<vector<int> >maze = {
{ 1, 0, 1, 1 },
{ 1, 1, 1, 1 },
{ 0, 0, 0, 1 },
{ 1, 1, 0, 1 }
};
path(maze,maze.size(),maze[0].size(),0,0,"");
sort(v1.begin(),v1.end());
cout<<v1[0]<<endl;
return 0;
}