1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
YES
思路:水BFS,被坑了好久。。。注意上下两层都是传送机的情况。
#include <cstdio>
#include <queue>
using namespace std;
struct S{
int x,y,z,step;
bool operator<(const S &p) const
{
return step>p.step;
}
}t;
char mp[2][10][11];
bool vis[2][10][10];
int nxt[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int main()
{
int T,n,m,lim,i,j,k;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&lim);
for(i=0;i<2;i++) for(j=0;j<n;j++) scanf("%s",mp[i][j]);
for(i=0;i<2;i++) for(j=0;j<n;j++) for(k=0;k<m;k++) vis[i][j][k]=0;
int top=0,bottom=1;
vis[0][0][0]=1;
mp[0][0][0]='.';
t.x=0;
t.y=0;
t.z=0;
t.step=0;
priority_queue<S>que;
que.push(t);
while(!que.empty())
{
t=que.top();
if(mp[t.x][t.y][t.z]=='P')
{
puts("YES");
break;
}
que.pop();
for(i=0;i<4;i++)
{
t.y+=nxt[i][0];
t.z+=nxt[i][1];
if(t.y>=0 && t.y<n && t.z>=0 && t.z<m && !vis[t.x][t.y][t.z] && t.step<lim)
{
vis[t.x][t.y][t.z]=1;
if(mp[t.x][t.y][t.z]=='.' || mp[t.x][t.y][t.z]=='P')
{
t.step++;
que.push(t);
t.step--;
}
else if(mp[t.x][t.y][t.z]=='#')
{
if(!vis[!t.x][t.y][t.z] && (mp[!t.x][t.y][t.z]=='.' || mp[!t.x][t.y][t.z]=='P'))
{
vis[!t.x][t.y][t.z]=1;
t.x=!t.x;
t.step++;
que.push(t);
t.step--;
t.x=!t.x;
}
}
}
t.y-=nxt[i][0];
t.z-=nxt[i][1];
}
}
if(que.empty()) puts("NO");
}
}
HDU-2102-A计划(BFS+优先队列),布布扣,bubuko.com
原文:http://blog.csdn.net/faithdmc/article/details/38533463