1 #include<cstdio> //hdu2102
2 #include<cstring>
3 #include<queue>
4 using namespace std;
5 const int INF=0x03f3f3f3f;
6 int n,m,t;
7 char map[2][12][12];
8 int hash[2][12][12];
9 int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
10
11 struct In
12 {
13 int x,y,z,t;
14 };
15 In e;
16 int bfs()
17 {
18 In p,cur;
19 int x1,y1,z1,t1,i;
20 queue<In> q;
21 p.x = p.y= p.z= p.t= 0;
22 hash[0][0][0] = 0;
23 q.push(p);
24 while (!q.empty())
25 {
26 cur = q.front();
27 if ( cur.x == e.x && cur.y == e.y && cur.z == e.z && cur.t <= t)
28 return 1;
29 q.pop();
30 for ( i = 0 ; i < 4; i ++)
31 {
32 x1 = cur.x ;
33 y1 = cur.y + dir[i][0];
34 z1 = cur.z + dir[i][1];
35 t1 = cur.t + 1;
36 if ( y1 >= 0 && y1 < n && z1 >=0 && z1 < m && t1 <= t && map[x1][y1][z1]!= '*' )
37 {
38 if ( map[x1][y1][z1] == '#')
39 {
40 if ( map[1-x1][y1][z1] == '#' || map[1-x1][y1][z1] == '*')
41 continue;
42 else if (t1 <= hash[1-x1][y1][z1])
43 {
44 hash[1-x1][y1][z1] = t1;
45 p.x = 1-x1;
46 p.y = y1;
47 p.z = z1;
48 p.t = t1;
49 q.push(p);
50 // printf("%d %d %d %d\n",p.x,p.y,p.z,p.t);
51 }
52 }
53 else
54 {
55 if ( t1 < hash[x1][y1][z1])
56 {
57 hash[x1][y1][z1] = t1;
58 p.x = x1;
59 p.y = y1;
60 p.z = z1;
61 p.t = t1;
62 q.push(p);
63 // printf("%d %d %d %d\n",p.x,p.y,p.z,p.t);
64 }
65 }
66 }
67 }
68 }
69 return 0;
70 }
71 int main()
72 {
73 int ca;
74 int i,j,k;
75 scanf("%d",&ca);
76 while ( ca --)
77 {
78 scanf("%d%d%d",&n,&m,&t);
79 for ( i = 0 ; i < 2 ; i ++)
80 for ( j = 0; j < n; j ++)
81 scanf("%s",map[i][j]);
82 memset(hash,INF,sizeof(hash));
83 for ( i = 0; i < 2 ;i ++)
84 {
85 for ( j = 0 ; j < n ; j ++ )
86 {
87 for ( k = 0 ; k < m ; k ++)
88 {
89 if (map[i][j][k]== 'P' )
90 {
91 e.x = i;
92 e.y = j;
93 e.z = k;
94 }
95 }
96 }
97 }
98 //printf("%d %d %d\n",e.x,e.y,e.z);
99 if ( bfs())
100 printf("YES\n");
101 else
102 printf("NO\n");
103 }
104 return 0;
105 }
好久没写队列了,写个找找感觉,有参考网络,还因为维度弄错,查了很长时间代码。^_^