1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
YES
1.起点一定是(0,0,0)
2.如果#的对应位置是#或者* 无法移动
3.终点可以再任意一层
4.昨晚上睡觉时候想到的。如果在第二层中有#会移动到第一层。。我忽略了4, 以至于wa了几次
#include <stdio.h> #include <queue> #include <string.h> using namespace std; char map[2][15][15]; int n,m,t; int dir[4][2]={1,0,-1,0,0,1,0,-1}; int vis[2][15][15],flag; struct node { int k,x,y,cnt; friend bool operator<(node a,node b) { return a.cnt>b.cnt; } }; bool yes(int k,int x,int y) { if(k<0||k>=2||x<0||y<0||x>=n||y>=m||map[k][x][y]=='*') return false; if(vis[k][x][y]) return false; return true; } void bfs() { priority_queue<node>s; node temp,temp1; while(!s.empty()) s.pop(); temp.k=0,temp.x=0,temp.y=0,temp.cnt=0; s.push(temp); vis[0][0][0]=1; while(!s.empty()) { temp1=temp=s.top(); s.pop(); if(map[temp.k][temp.x][temp.y]=='P') { flag=temp.cnt; break; } if(temp.cnt>t) break; for(int i=0;i<4;i++) { temp.x+=dir[i][0]; temp.y+=dir[i][1]; if(yes(temp.k,temp.x,temp.y)) { vis[temp.k][temp.x][temp.y]=1; temp.cnt++; if(map[temp.k][temp.x][temp.y]=='#'&&temp.k==0&&(map[temp.k+1][temp.x][temp.y]=='#'||map[temp.k+1][temp.x][temp.y]=='*')) { temp=temp1; continue; } if(map[temp.k][temp.x][temp.y]=='#'&&temp.k==1&&(map[temp.k-1][temp.x][temp.y]=='#'||map[temp.k-1][temp.x][temp.y]=='*')) { temp=temp1; continue; } if(map[temp.k][temp.x][temp.y]=='#'&&temp.k==0) { temp.k++; vis[temp.k][temp.x][temp.y]=1; } if(map[temp.k][temp.x][temp.y]=='#'&&temp.k==1) { temp.k--; vis[temp.k][temp.x][temp.y]=1; } s.push(temp); } temp=temp1; } } } int main() { int ncase; scanf("%d",&ncase); while(ncase--) { memset(map,0,sizeof(map)); memset(vis,0,sizeof(vis)); scanf("%d %d %d",&n,&m,&t); for(int k=0;k<2;k++) for(int i=0;i<n;i++) scanf("%s",map[k][i]); flag=0; bfs(); if(flag&&flag<=t) printf("YES\n"); else printf("NO\n"); } return 0; }