标签:
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16914 Accepted Submission(s): 4251
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int MAXN=15; struct Node{ int x,y,z,step; Node(){} Node(int z,int y,int x,int step) { this->x=x; this->y=y; this->z=z; this->step=step; } }; int n,m,limit; char mz[2][MAXN][MAXN]; int vis[2][MAXN][MAXN]; int dy[4]={0,1,0,-1}; int dx[4]={1,0,-1,0}; void bfs() { memset(vis,0,sizeof(vis)); queue<Node> que; que.push(Node(0,0,0,0)); vis[0][0][0]=1; while(!que.empty()) { Node now=que.front();que.pop(); if(mz[now.z][now.y][now.x]==‘P‘) { printf("YES\n"); return ; } for(int i=0;i<4;i++) { int ny=dy[i]+now.y; int nx=dx[i]+now.x; int ns=now.step+1; if(0<=ny&&ny<n&&0<=nx&&nx<m&&mz[now.z][ny][nx]!=‘*‘&&!vis[now.z][ny][nx]&&ns<=limit) { vis[now.z][ny][nx]=1; if(mz[now.z][ny][nx]==‘#‘) { int opposite=(now.z==1?0:1); if(mz[opposite][ny][nx]!=‘*‘&&mz[opposite][ny][nx]!=‘#‘)//防止进入两层相对位置均为时光机 { vis[opposite][ny][nx]=1; que.push(Node(opposite,ny,nx,now.step+1)); } } else { que.push(Node(now.z,ny,nx,now.step+1)); } } } } printf("NO\n"); } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&limit); for(int k=0;k<2;k++) { for(int i=0;i<n;i++) { scanf("%*c"); for(int j=0;j<m;j++) { scanf("%c",&mz[k][i][j]); } } scanf("%*c"); } bfs(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5685500.html