HDU – 2102 A计划(BFS)

作者: qwq 分类: ACM 发布时间: 2017-03-27 19:34

可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。Input输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N*M(1 <= N,M <=10)。T如上所意。接下去的前N*M表示迷宫的第一层的布置情况,后N*M表示迷宫第二层的布置情况。Output如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。Sample Input

1
5 5 14
S*#*.
.#...
.....
****.
...#.

..*.P
#.*..
***..
...*.
*.#..

Sample Output

YES

 

这个题算普通的简单题,但是仍然浪费了我很长的时间,我犯了好几个错误。

1 有好多地方的==号写成了=号,而且一时半会也没有发现。

以后使用==号的时候要注意常量放在前面,变量放在后面,比如0==a这种形式,来避免这样的错误。

2判断能否传送的时候注意上下两层都是#的时候也是不能传送的,不只是有另一层是墙壁这一种情况。

3一定要使用is_visted数组,要不然没法搜索,而且vis数组在使用过后一定要清零

 

 

[code lang=”c”]#include<iostream>
#include<queue>
#include<cstdio>

using namespace std;
int c, n, m, t;
int f[2][10][10];
int vis[2][10][10] = { 0 };

int sx[4] = { 0,0,1,-1 };
int sy[4] = { 1,-1,0,0 };
struct local {
int x;
int y;
int fl;
int step;
}endq;
int is_ok(int x, int y, int fl) {
if (x>=0 && y>=0 && x<n&&y<m) {
if (f[fl][x][y] == 0)return 1;
if (f[fl][x][y] == 4)return 1;
if (f[fl][x][y] == 1)return 0;
if (f[fl][x][y] == 2) {
if (f[fl == 0 ? 1 : 0][x][y] == 0) {
return 2;
}
else {
return 0;
}
}
}
return 0;
}

int bfs() {
queue<struct local> q;
struct local tem;
struct local next;
tem.x = tem.y = tem.fl = tem.step = 0;
q.push(tem);
while (!q.empty()) {
tem = q.front();
q.pop();
if (tem.x == endq.x&&tem.y == endq.y&&tem.fl == endq.fl) {

return tem.step;
}
for (int i = 0; i<4; i++) { if (vis[tem.fl][tem.x + sx[i]][tem.y + sy[i]] != 0) continue; int r = is_ok(tem.x + sx[i], tem.y + sy[i], tem.fl); if (r == 1) { vis[tem.fl][tem.x + sx[i]][tem.y + sy[i]] = tem.step + 1; next.x = tem.x + sx[i]; next.y = tem.y + sy[i]; next.fl = tem.fl; next.step = tem.step + 1; q.push(next); } if (r == 2) { vis[tem.fl][tem.x + sx[i]][tem.y + sy[i]] = tem.step + 1; next.x = tem.x + sx[i]; next.y = tem.y + sy[i]; next.fl = tem.fl == 1 ? 0 : 1; next.step = tem.step + 1; q.push(next); } } } return -1; } int main() { cin >> c;
char tem;
for (int i = 0; i<c; i++) { memset(vis, 0, sizeof(vis)); cin >> n >> m >> t;
for (int j = 0; j<n; j++) {
for (int q = 0; q<m; q++) { cin >> tem;
if (tem == ‘S’)
{
f[0][j][q] = 0;

}
if (tem == ‘*’) {
f[0][j][q] = 1;
}
if (tem == ‘#’) {
f[0][j][q] = 2;
}
if (tem ==’P’) {
f[0][j][q] = 0;
endq.x = j;
endq.y = q;
endq.fl = 0;
}
if (tem == ‘.’) {
f[0][j][q] = 0;
}
}
}
getchar();

for (int j = 0; j<n; j++) {
for (int q = 0; q<m; q++) { cin >> tem;
if (tem == ‘S’)
{
f[1][j][q] = 0;

}
if (tem == ‘*’) {
f[1][j][q] = 1;
}
if (tem == ‘#’) {
f[1][j][q] = 2;
}
if (tem == ‘P’) {
f[1][j][q] = 0;
endq.x = j;
endq.y = q;
endq.fl = 1;
}
if (tem == ‘.’) {
f[1][j][q] = 0;
}
}
}

int r = bfs();
//cout << r;
if (r!=-1&&r <=t)
cout << "YES" << endl;
else
cout << "NO" << endl;

}

return 0;
}[/code]

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注