-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva00614.cpp
executable file
·137 lines (116 loc) · 3.79 KB
/
uva00614.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);
using namespace std;
typedef long long int64;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector< pair<int, int> > vii;
struct Node{
int d, pr, pc;
bool onPath;
Node(){
d = -1, pr = -1, pc = -1, onPath = false;
}
};
enum direction {W = 0, N, E, S};
int dc[] = {-1, 0, 1, 0}; // W, N, E, S
int dr[] = {0, -1, 0, 1};
vector< vector<Node> > dist;
vector< vector<int> > wall;
bool legal(int r, int c, direction direct){
int nR = dist.size()-1, nC = dist[0].size()-1, newR, newC;
newR = r + dr[direct];
newC = c + dc[direct];
if(newR == 0 || newR > nR || newC == 0 || newC > nC) return false; // out boundary
if(dist[newR][newC].d != -1) return false; // already visited
switch(direct){ // wall exists
case N:
if(wall[newR][newC] >= 2) return false;
break; // this break is necessary! Because if the 'if' test is false, the function won't return and it will continue to execute from here! Thus, a break is necessay!
case S:
if(wall[r][c] >= 2) return false;
break;
case W:
if(wall[newR][newC] == 1 || wall[newR][newC] == 3) return false;
break;
case E:
if(wall[r][c] == 1 || wall[r][c] == 3) return false;
break;
}
return true;
}
bool dfs(int r, int c, int d, int er, int ec){
dist[r][c].d = d + 1;
if(r == er && c == ec) return true;
int newR, newC;
bool found = false;
for(direction direct = W; direct <= S; direct = direction(direct + 1)){
if(legal(r, c, direct)){
newR = r + dr[direct];
newC = c + dc[direct];
dist[newR][newC].pr = r;
dist[newR][newC].pc = c;
found = dfs(newR, newC, d+1, er, ec);
if(found) return true;
}
}
return false;
}
void print(int &count){
cout << "Maze " << ++count << endl << endl;
// print header
for(int c = 1; c < dist[0].size(); ++c) cout << "+---";
cout << "+" << endl;
for(int r = 1; r < dist.size(); ++r){
cout << "|"; // left boundary
for(int c = 1; c < dist[0].size(); ++c){
cout << setw(3) << right;
if(dist[r][c].onPath) cout << dist[r][c].d;
else{
if(dist[r][c].d != -1) cout << "???";
else cout << " ";
}
if(c+1 < dist[0].size()){
if(wall[r][c] == 1 || wall[r][c] == 3) cout << "|";
else cout << " ";
}
}
cout << "|"; // right boundary
cout << endl;
// draw the walls between rows
if(r+1 < dist.size()){
cout << "+";
for(int c = 1; c < dist[0].size(); ++c){
if(wall[r][c] >= 2) cout << "---";
else cout << " ";
cout << "+";
}
cout << endl;
}
}
// print tail
for(int c = 1; c < dist[0].size(); ++c) cout << "+---";
cout << "+" << endl << endl << endl;
}
void setPath(int r, int c){
dist[r][c].onPath = true;
if(dist[r][c].pr != -1 && dist[r][c].pc != -1) setPath(dist[r][c].pr, dist[r][c].pc);
}
int main(){ _
int nR, nC, sr, sc, er, ec, count = 0;
while(cin >> nR >> nC){
if(nR + nC == 0) break;
cin >> sr >> sc >> er >> ec;
dist.assign(nR+1, vector<Node> (nC+1));
wall.assign(nR+1, vi(nC+1));
for(int i = 1; i <= nR; ++i)
for(int j = 1; j <= nC; ++j)
cin >> wall[i][j];
int d = 0;
bool success;
success = dfs(sr, sc, d, er, ec);
if(success) setPath(er, ec);
print(count);
}
return 0;
}