1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #define CL(x,y) memset(x,y,sizeof(x)) 6 using namespace std; 7 struct node 8 { 9 int x; 10 int y; 11 int z; 12 int time; 13 }; 14 queue<node> Q; 15 const int MAX = 13; 16 int cases, N, M, T; 17 char maze[2][MAX][MAX]; 18 int used[2][MAX][MAX]; 19 bool checked(int y, int z); 20 void BFS(); 21 int Move[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; 22 int main() 23 { 24 int i, j, k; 25 cin >> cases; 26 while(cases--) 27 { 28 cin >> N >> M >> T; 29 getchar(); 30 for(k = 0; k < 2; k++) 31 { 32 for(i = 0; i < N; i++) 33 { 34 for(j = 0; j < M; j++) 35 cin >> maze[k][i][j]; 36 getchar(); 37 } 38 if(k == 0) 39 getchar(); 40 } 41 CL(used, 0); 42 BFS(); 43 while(!Q.empty()) 44 Q.pop(); 45 } 46 return 0; 47 } 48 void BFS() 49 { 50 int xx, yy, zz, i; 51 node front, rear, cur; 52 front.x = 0; 53 front.y = 0; 54 front.z = 0; 55 front.time = 0; 56 used[front.x][front.y][front.z] = 1; 57 Q.push(front); 58 while(!Q.empty()) 59 { 60 cur = Q.front(); 61 Q.pop(); 62 if(cur.time > T) 63 { 64 cout << "NO" << endl; 65 return ; 66 } 67 if(maze[cur.x][cur.y][cur.z] == ‘P‘) 68 { 69 // cout << cur.time << endl; 70 cout << "YES" << endl; 71 return ; 72 } 73 for(i=0; i<4; i++) 74 { 75 xx = cur.x; 76 yy = Move[i][0]+cur.y; 77 zz = Move[i][1]+cur.z; 78 if(checked(yy,zz) && maze[xx][yy][zz]!=‘*‘ && maze[xx][yy][zz]!=‘#‘ && !used[xx][yy][zz]) 79 { 80 used[xx][yy][zz] = 1; 81 rear.x = xx;//cur.x;这里的坐标一致都没发现,66666,WA到现在 82 rear.y = yy;//cur.y; 83 rear.z = zz;//cur.z; 84 rear.time = cur.time+1; 85 Q.push(rear); 86 } 87 if(checked(yy,zz) && maze[xx][yy][zz]==‘#‘ && !used[xx][yy][zz])//两个if()语句的顺序还是有一定规范的 88 { 89 used[xx][yy][zz] = 1; 90 xx = (cur.x==1) ? 0 : 1;//如果下面的if()语句不执行,则只是改变了所在的层数而已 91 if(maze[xx][yy][zz]==‘*‘ || maze[xx][yy][zz]==‘#‘)//该路不可走 92 { 93 xx = (cur.x==1) ? 0 : 1;//变回原来的样子 94 used[xx][yy][zz] = 1; 95 } 96 else 97 { 98 rear.x = xx; 99 rear.y = yy; 100 rear.z = zz; 101 rear.time = cur.time+1; 102 Q.push(rear); 103 } 104 } 105 } 106 } 107 cout << "NO" << endl; 108 return ; 109 } 110 bool checked(int y, int z) 111 { 112 if(y>=0 && y<N && z>=0 && z<M) 113 return true; 114 return false; 115 }
两层,数组选择很重要,然后是对于结构体队列的运用,新建node front、rear、cur,进行条件的判断,两个if()语句的顺序不想考虑了,太烦
时间: 2024-10-23 09:50:39