-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
103 lines (92 loc) · 2.59 KB
/
Copy pathMaze.cpp
File metadata and controls
103 lines (92 loc) · 2.59 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Given a m x n Maze, where a value of 1 implies a blocked path, while a 0 means one can walk right on through, please output all possible paths from (0, 0) to (m - 1, n - 1) in alphabetical order.
// If there is no any path to reach exit, output “No path”. Note that the moving directions include North, East, West, and South only.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Maze{
public:
void output();
void findpath(vector<vector<int>> board, vector<int> path, int x, int y, int m, int n);
public:
vector<vector <int>>pathdirection;
};
void Maze:: findpath(vector<vector<int>> board, vector<int> path, int x, int y, int m, int n){
if(x==m-1&&y==n-1){
pathdirection.push_back(path);
return;
}
board[x][y]=1;
if(x!=0&&board[x-1][y]!=1){
x--;
path.push_back(0);
findpath(board, path, x, y, m, n);
path.pop_back();
x++;
}
if(y!=0&&board[x][y-1]!=1){
y--;
path.push_back(1);
findpath(board, path, x, y, m, n);
path.pop_back();
y++;
}
if(y!=(int)board[x].size()-1&&board[x][y+1]!=1){
y++;
path.push_back(2);
findpath(board, path, x, y, m, n);
path.pop_back();
y--;
}
if(x!=(int)board.size()-1&&board[x+1][y]!=1){
x++;
path.push_back(3);
findpath(board, path, x, y, m, n);
path.pop_back();
x--;
}
}
void Maze:: output(){
if((int)pathdirection.size()>=1){
for(int i=0;i<(int)pathdirection.size();i++){
int x=0;
int y=0;
cout<<"("<<x<<","<<y<<")";
for(int j=0;j<(int)pathdirection[i].size();j++){
if(pathdirection[i][j]==0){
x--;
}else if(pathdirection[i][j]==1){
y--;
}else if(pathdirection[i][j]==2){
y++;
}else if(pathdirection[i][j]==3){
x++;
}
cout<<","<<"("<<x<<","<<y<<")";
}
cout<<endl;
}
}else{
cout<<"No path"<<endl;
}
}
int main(int argc, const char * argv[]) {
int m;
int n;
while(cin>>m>>n){
vector<vector<int>> board;
for(int i=0;i<m;i++){
string input;
cin>>input;
vector<int> line;
for(int j=0;j<n;j++){
line.push_back(input[j]-'0');
}
board.push_back(line);
}
Maze maze;
vector<int> path;
maze.findpath(board, path, 0, 0, m, n);
maze.output();
}
}