Problem F
木乃伊迷宫
时限:1000ms 内存限制:10000K 总时限:3000ms
描述:
木乃伊地下宫殿是一个6行6列的迷宫。作为敢到木乃伊地下宫殿里去探险的你,有没有跟木乃伊抓迷藏的心理准备呵!游戏在木乃伊所在的迷宫里展开,任务就是尽快赶到出口。你一次只能走一步,而木乃伊可以走两步,但木乃伊是很笨的,他总是先尽量跟你达到同一列,如果已经是同一列了,他才会像你走来,有墙的地方人和木乃伊都不能过,你可以利用障碍物牵制住木乃伊。
输入:
先输入墙的数量n,然后在后续的n行里每行有3个数表示一堵墙,3个数分别为格子的行、列和墙的位置(0表示这个格子的下方是墙,1表示这个格子的右方是墙),再下来的3行每行2个数,分别表示木乃伊、人还有出口的位置。
输出:
如果能安全逃生则输出Yes,否则输出No,答案占一行。
输入样例:
5
0 0 0
1 1 1
1 4 1
3 4 1
4 3 0
3 3
3 1
5 5
输出样例:
No
#include <iostream>
#include <queue>
using namespace std;
struct rec {
int mx, my;
int px, py;
bool useful;
};
queue <rec> q;
int visited[7][7][7][7];
int maze[8][8][2];
int n;//墙的数量
int mstart_x, mstart_y, pstart_x, pstart_y, target_x, target_y;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
void init() {
cin >> n;
for (int i = 0; i < n; i++) {
int x, y, w;
cin >> x >> y >> w;
maze[x][y][w] = 1;
}
cin >> mstart_x >> mstart_y;
cin >> pstart_x >> pstart_y;
cin >> target_x >> target_y;
}
bool iswall(int x, int y, int i) {
switch (i) {
case 0://向左走
return maze[x][y - 1][1];
case 1://向下走
return maze[x][y][0];
case 2://向右走
return maze[x][y][1];
case 3://向上走
return maze[x - 1][y][0];
}
return false;
}
rec move(rec now, int i) {
rec next;
next.px = now.px + dx[i];
next.py = now.py + dy[i];
if (next.py < 0 || next.py > 5 || next.px < 0 || next.px > 5 || iswall(now.px, now.py, i)) {
next.useful = false;
return next;
}
int step = 2;
// cout << endl;
// cout << "木乃伊的位置:";
// cout << now.mx << now.my << endl;
// cout << "人的位置:";
// cout << next.px << next.py << endl;
next.mx = now.mx, next.my = now.my;
while (step--) {
if (next.py > next.my && !iswall(next.mx, next.my, 2)) {
next.my++;
} else if (next.py < next.my && !iswall(next.mx, next.my, 0)) {
next.my--;
} else if (next.py == next.my) {
if (next.px > next.mx && !iswall(next.mx, next.my, 1)) {
next.mx++;
} else if (next.px < next.mx && !iswall(next.mx, next.my, 3)) {
next.mx--;
} else if (next.px == next.mx) {
// cout << next.px << ' ' << next.py << "catch" << endl;
next.useful = false;
return next;
}
}
}
if (visited[next.px][next.py][next.mx][next.my]) {
next.useful = false;
} else {
next.useful = true;
visited[next.px][next.py][next.mx][next.my] = 1;
}
return next;
}
bool bfs() {
rec temp;
temp.mx = mstart_x, temp.my = mstart_y;
temp.px = pstart_x, temp.py = pstart_y;
visited[temp.px][temp.py][temp.mx][temp.my] = 1;
q.push(temp);
while (q.size()) {
rec now = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
rec next = move(now, i);
if (next.useful == true) {
if (next.px == target_x && next.py == target_y) {
return true;
}
q.push(next);
}
}
}
return false;
}
int main() {
init();
if (bfs()) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}