Description
Input
Output
Sample Input
Sample Output
#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <queue> using namespace std; int N,M,TIME; int MAP[15][15][2]; int VIS[15][15][2]; int UP_DATE[][2] = {{-1,0},{1,0},{0,-1},{0,1}}; struct Node { int x,y,z; int time; bool check(void) const { if(x >= 1 && x <= N && y >= 1&& y <= M && !VIS[x][y][z] && MAP[x][y][z] != ‘*‘) return true; return false; } }; bool bfs(void); void check(void); int main(void) { int n; scanf("%d",&n); while(n --) { memset(VIS,0,sizeof(VIS)); scanf("%d%d%d",&N,&M,&TIME); for(int i = 0;i < 2;i ++) for(int j = 1;j <= N;j ++) for(int k = 1;k <= M;k ++) scanf(" %c",&MAP[j][k][i]); if(bfs()) printf("YES\n"); else printf("NO\n"); } return 0; } void check(void) { for(int i = 0;i < 2;i ++) { for(int j = 1;j <= N;j ++) { for(int k = 1;k <= M;k ++) printf("%c",MAP[j][k][i]); cout << endl; } cout << endl << endl; } } bool bfs(void) { queue<Node> que; Node head,next,cur; head.x = head.y = 1; head.z = 0; head.time = 0; que.push(head); while(!que.empty()) { cur = que.front(); que.pop(); for(int i = 0;i < 4;i ++) { next = cur; next.x += UP_DATE[i][0]; next.y += UP_DATE[i][1]; next.time += 1; if(!next.check()) continue; VIS[next.x][next.y][next.z] = 1; if(MAP[next.x][next.y][next.z] == ‘#‘) { if(MAP[next.x][next.y][!next.z] == ‘.‘ && !VIS[next.x][next.y][!next.z]) { next.z = !next.z; VIS[next.x][next.y][!next.z] = 1; } else if(MAP[next.x][next.y][!next.z] == ‘P‘ && next.time <= TIME) return true; else continue; } else if(MAP[next.x][next.y][next.z] == ‘P‘ && next.time <= TIME) return true; que.push(next); } } return false; }
原文:http://www.cnblogs.com/xz816111/p/4366069.html