# HDU – 2102 A计划(BFS)

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

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

Sample Output

YES

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

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]