-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq6.cpp
More file actions
199 lines (170 loc) · 3.43 KB
/
q6.cpp
File metadata and controls
199 lines (170 loc) · 3.43 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <queue>
using std::endl;
using std::cin;
using std::cout;
using std::queue;
// 6:献给阿尔吉侬的花束
struct State {
int time = 0;
int x = 0, y = 0;
};
// 上下左右
int dir[4][2] = { {0,1} ,{0,-1} ,{-1,0} ,{1,0} };
// 迷宫大小
int r = 0, c = 0;
// 迷宫,这里visted和迷宫放在一起
bool visit[300][300];
// 记录open1与open2中的元素
int exist1[300][300];
int exist2[300][300];
int expand(State state, queue<State>& open, int exist1[300][300], int exist2[300][300]) {
for (int i = 0; i < 4; i++) {
int newX = state.x + dir[i][0];
int newY = state.y + dir[i][1];
// 超出边界
if (newX < 0 || newX >= c || newY < 0 || newY >= r) {
continue;
}
// 找到交集
if (exist2[newX][newY] >= 0) {
return state.time + 1 + exist2[newX][newY];
}
// 已走过或者有墙
if (visit[newX][newY]) {
continue;
}
State newState;
newState.x = newX;
newState.y = newY;
newState.time = state.time + 1;
visit[newX][newY] = true;
exist1[newX][newY] = newState.time;
open.push(newState);
}
return -1;
}
// 双向广搜
int dbfs(State& begin, State& end) {
if (begin.x == end.x && begin.y == end.y) {
return 0;
}
for (int i = 0; i < c ; i++){
for (int j = 0; j < r ; j++){
exist1[i][j] = -1;
exist2[i][j] = -1;
}
}
queue<State> open1;
queue<State> open2;
open1.push(begin);
open2.push(end);
exist1[begin.x][begin.y] = 0;
exist2[end.x][end.y] = 0;
while (!open1.empty() || !open2.empty()) {
// 判断该拓展哪一边
bool open1EX = true;
if (open1.empty()) {
open1EX = false;
} else if (open2.empty()) {
open1EX = true;
} else if (open1.size() <= open2.size()) {
open1EX = true;
} else {
open1EX = false;
}
if (open1EX) {
// 拓展正向队列
State state = open1.front();
open1.pop();
int res = expand(state, open1, exist1, exist2);
if (res != -1) {
return res;
}
} else {
// 拓展反向队列
State state = open2.front();
open2.pop();
int res = expand(state, open2, exist2, exist1);
if (res != -1) {
return res;
}
}
}
return -1;
}
//// 单向广搜
//int bfs(State& begin, State& end) {
//
// queue<State> open;
// open.push(begin);
//
// while (!open.empty()) {
//
// State state = open.front();
// open.pop();
//
// for (int i = 0; i < 4; i++) {
// int newX = state.x + dir[i][0];
// int newY = state.y + dir[i][1];
//
// // 超出边界
// if (newX < 0 || newX >= c || newY < 0 || newY >= r) {
// continue;
// }
//
// // 找到奶酪
// if (newX == end.x && newY == end.y) {
// return state.time + 1;
// }
//
// // 已走过或者有墙
// if (maze[newX][newY]) {
// continue;
// }
//
// State newState;
// newState.x = newX;
// newState.y = newY;
// newState.time = state.time + 1;
//
// maze[newX][newY] = true;
// open.push(newState);
// }
// }
//
// return -1;
//}
int main(int argc, char *argv[]) {
int t = 0;
cin >> t;
while (t--) {
cin >> r >> c;
State begin;
State end;
char ch = '\0';
for (int j = 0; j < r; j++) {
for (int i = 0; i < c; i++) {
cin >> ch;
if (ch == 'S') {
begin.x = i;
begin.y = j;
visit[i][j] = true;
} else if (ch == 'E') {
end.x = i;
end.y = j;
visit[i][j] = true;
} else {
visit[i][j] = ch == '#';
}
}
}
int res = dbfs(begin, end);
if (res == -1) {
cout << "oop!" << endl;
} else {
cout << res << endl;
}
}
return 0;
}