http://acm.hdu.edu.cn/showproblem.php?pid=2102
#include "map" #include "queue" #include "math.h" #include "stdio.h" #include "string.h" #include "iostream" #include "algorithm" #define abs(x) x > 0 ? x : -x #define max(a,b) a > b ? a : b #define min(a,b) a < b ? a : b using namespace std; int di[6][3] = {{0,0,1},{0,1,0},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}}; int Map[5][15][15]; bool vis[5][15][15]; int t; struct Node { int zz,xx,yy; int step; }; void Bfs() { memset(vis,0,sizeof(vis)); queue<Node>Q; Node now,next; int l,r; now.zz=1; now.xx=1; now.yy=1; now.step=0; vis[1][1][1] = 1; Q.push(now); while(!Q.empty()) { now = Q.front(); Q.pop(); if(Map[now.zz][now.xx][now.yy]==3) { if(now.step<=t) printf("YES\n"); else printf("NO\n"); return; } if(Map[now.zz][now.xx][now.yy]==2) l=4,r=6; if(Map[now.zz][now.xx][now.yy]==1) l=0,r=4; for(int i=l; i<r; i++) { next.zz = now.zz + di[i][0]; next.xx = now.xx + di[i][1]; next.yy = now.yy + di[i][2]; next.step = now.step + 1; if(i==4||i==5) next.step = now.step; if(Map[next.zz][next.xx][next.yy]!=0) { if(!vis[next.zz][next.xx][next.yy]) { vis[next.zz][next.xx][next.yy] = 1; Q.push(next); } } } } printf("NO\n"); } int main() { int c,n,m,i,j,k; char s; scanf("%d",&c); while(c--) { memset(Map,0,sizeof(Map)); scanf("%d%d%d",&n,&m,&t); for(i=1; i<=2; i++) { for(j=1; j<=n; j++) { getchar(); for(k=1; k<=m; k++) { scanf("%c",&s); if(s==‘S‘||s==‘.‘) Map[i][j][k] = 1; if(s==‘P‘) Map[i][j][k] = 3; if(s==‘#‘) Map[i][j][k] = 2; if(s==‘*‘) Map[i][j][k] = 0; } } getchar(); } //for(i=1; i<=2; i++){for(j=1; j<=n; j++) {for(k=1; k<=m; k++) printf("%d",Map[i][j][k]); printf("\n"); }printf("\n"); } Bfs(); } return 0; }
时间: 2024-10-17 06:39:49