转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102
1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
YES
比较坑的地方:
/*1.碰到‘#‘传送们必传送,没有第二种选择。
2.有可能起点和终点在同一层,样例是分别在两层里。起点和终点可以在任意的位置。
3.两边都是传送门时,或者一边是传送门一边是墙时,这两种情况是死路。
4.输入时要注意两张地图间有空行,要特殊处理。
*/
代码如下:
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
#define M 17
int xx[4]={1,0,0,-1};
int yy[4]={0,1,-1,0};
int x,y,z;
int n, m, ttime;
char map[2][M][M];
int vis[2][M][M];//标记
struct node
{
int x, y, z;
int Time;
};
//q.push(front);
void bfs()
{
int i;
int dx, dy, dz;
queue<node>q;
node front, rear;
memset(vis,0,sizeof(vis));
front.x = x,front.y=y,front.z=z;
front.Time = 0;
vis[z][x][y] = 1;
q.push(front);//若这个定义在外面则必须每次清空
while(!q.empty())
{
front = q.front();
q.pop();
for(i = 0; i < 4; i++)
{
dx = front.x+xx[i];
dy = front.y+yy[i];
dz = front.z;
//判断是否越界,目标点是否为墙,是否超时
if(dx>=0&&dx<n&&dy>=0&&dy<m&&!vis[dz][dx][dy]
&&map[dz][dx][dy]!='*'&&front.Time<ttime)
{
if(map[dz][dx][dy]=='#' && !vis[dz][dx][dy])
{//表示遇到传送器的情况
vis[dz][dx][dy] = 1;
if(front.z == 1)//如果在上层,就传到下层
dz = 0;
else if(front.z == 0)//如果在下层,就传到上层
dz = 1;
}
vis[dz][dx][dy] = 1;
if(map[dz][dx][dy] == 'P')//找到公主
{
printf("YES\n");
return;
}
rear.x = dx,rear.y = dy;
rear.z = dz,rear.Time=front.Time+1;
q.push(rear);
}
}
}
printf("NO\n");
}
int main()
{
int c;
int i, j, k;
while(~scanf("%d",&c))
{
while(c--)
{
// while(!q.empty())
// q.pop();
// scanf("%d%d%d",&n,&m,&ttime);
cin >> n >> m >> ttime;
for(k = 0; k < 2; k++)
{
for(i = 0; i < n; i++)
{
// scanf("%s",map[k][i]);
for(j = 0; j < m; j++)
{
cin>>map[k][i][j];
if(map[k][i][j] == 'S')
{
x = i, y = j, z = k;
}
}
}
}
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
if(map[0][i][j]=='#' && map[1][i][j]=='*')
{//如果传送机上方是墙,他就会撞死。。。。
//所以这种情况也要排除。
map[0][i][j] = '*';
}
if(map[0][i][j]=='*' && map[1][i][j]=='#')
{//如果传送机下方是墙,同样他会撞死,也排除
map[1][i][j] = '*';
}
if(map[0][i][j]=='#' && map[1][i][j]=='#')
{//如果传送机上方还是传送机,就会陷入死循环,
//所以这种情况我们把上下对应的两个位置设为墙.
map[0][i][j] = '*',map[1][i][j] = '*';
}
}
}
bfs();
}
}
return 0;
}
hdu 2102 A计划(双层BFS)(详解),布布扣,bubuko.com
原文:http://blog.csdn.net/u012860063/article/details/37054857