考察:搜索
思路:
简单的bfs广搜即可.但是注意存在上一层是传送机下一层还是传送机的死循环点.
1 #include <iostream> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 const int N = 15,INF = 0x3f3f3f3f; 6 struct Node{ 7 int x,y,z,ti; 8 }; 9 int n,m,C,dist[2][N][N]; 10 int xx[4] = {-1,1,0,0},yy[4] = {0,0,-1,1}; 11 char mp[2][N][N]; 12 bool bfs() 13 { 14 queue<Node> q; 15 Node sta = {0,0,0,0}; 16 q.push(sta); 17 memset(dist,0x3f,sizeof dist); 18 dist[0][0][0] = 0; 19 while(q.size()) 20 { 21 Node it = q.front(); 22 q.pop(); 23 int x = it.x,y = it.y,z = it.z,ti = it.ti; 24 if(ti>C) return false; 25 if(mp[z][x][y]=='P') return true; 26 for(int i=0;i<4;i++) 27 { 28 int dx = x+xx[i],dy = y+yy[i]; 29 if(dx>=0&&dx<n&&dy>=0&&dy<m&&mp[z][dx][dy]!='*') 30 { 31 Node news = {dx,dy,z,ti+1}; 32 if(mp[z][dx][dy]=='#') news.z = news.z^1; 33 if(dist[news.z][news.x][news.y]<INF) continue; 34 if(mp[news.z][news.x][news.y]=='*') continue; 35 if(mp[news.z][news.x][news.y]=='#') continue; 36 dist[news.z][news.x][news.y] = dist[it.z][it.x][it.y]+1; 37 q.push(news); 38 } 39 } 40 } 41 return false; 42 } 43 int main() 44 { 45 int T; 46 scanf("%d",&T); 47 while(T--) 48 { 49 scanf("%d%d%d",&n,&m,&C); 50 for(int i=0;i<2;i++) 51 for(int j=0;j<n;j++) scanf("%s",mp[i][j]); 52 printf("%s\n",bfs()?"YES":"NO"); 53 } 54 return 0; 55 }