Plan
Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 4156 accepted submission (s): 936
Problem description
Yes
After the princess was captured by the Lord and rescued by the Knights again and again, unfortunately, she once again faces the test of her life. The Lord has sent a message saying that he will eat the princess at T moment because he heard the rumor that
The meat of the princess can also grow old. The elderly King was anxious to ask the warriors to save the princess. However, the princess has been used to it. She is confident that the brave knight LJ will be able to rescue her.
Current data profiling
It is reported that the princess is locked in a two-layer maze. the entrance of the maze is S (, 0). The position of the princess is represented by P. The space-time transmitter is represented by # And the wall is represented, flat.. When the knights enter
The empty transmitter will be transferred to the relative position of the other layer, but if it is switched to the wall, the knights will be killed. In the first layer, the knights can only move between the left and right sides. Each movement takes 1 moment. Inter-layer migration
Motion can only be transmitted through a time-space transmitter without any time required.
Input
The first line of input C indicates a total of C test data. The first line of each test data has three integers, n, m, and T. N, m the size of the Maze N * m (1 <= n, m <= 10 ). T. The next N * m represents the layout of the first layer of the maze, and the next N * m represents the layout of the second layer of the maze.
Output
If the knights can find the princess at T moment, they will output "yes"; otherwise, "no ".
Sample input1
5 5 14
S *#*.
.#...
.....
****.
...#.
.. *. P
#.*..
***..
...*.
*.#..
Sample outputyes simple BFs. Although the question is to rescue the princess at T moment, it is only necessary to save the princess within the time smaller than T moment, because the question does not mean that the walking side cannot go back, the time becomes less than or equal to T. The Code is as follows:
# Include <cstdio> # include <cstdlib> # include <cstring> # include <queue> using namespace STD; char map [2] [15] [15], hash [2] [15] [15]; int Sx, Sy, ex, ey, SK, Ek, s; inline bool legal (char C) {If (C = '. '| C = 's' | C =' # '| C =' * '| C = 'P') {return true ;} return false;} inline void gchar (char & C) {char t; while (t = getchar (),! Legal (t); C = T;} struct node {int X, Y, K, step;} Info; int dis [4] [2] = {0, 1, 0,-1, 1, 0,-1, 0}; bool BFS () {memset (hash, 0, sizeof (hash )); // you cannot simply judge whether a certain point has passed, but whether the point has a better solution queue <node> q; info. X = Sx, info. y = Sy, info. k = Sk, info. step = 0; hash [SK] [SX] [sy] = 1; q. push (Info); int CNT = 0; while (! Q. empty () {node Pos = Q. front (); q. pop (); If (Map [POS. k] [POS. x] [POS. y] = 'P' & Pos. step <= s) {// printf ("step = % d \ n", POS. step); Return true ;}for (INT I = 0; I <4; ++ I) {int x = POS. X + dis [I] [0], y = POS. Y + dis [I] [1], K = POS. k, step = POS. step; If (Map [k] [x] [Y]! = 0 & map [k] [x] [Y]! = '*') {If (Map [k] [x] [Y]! = '#'&&! Hash [k] [x] [Y] & step <s) {// The number of walking steps cannot reach the s step info. X = x, info. y = Y, info. k = K, info. step = Step + 1; hash [k] [x] [Y] = 1; q. push (Info);} else if (Map [k] [x] [Y] = '#' & map [(k + 1) % 2] [x] [Y]! = '*' & Amp; Map [(k + 1) % 2] [x] [Y]! = '#'&&! Hash [(k + 1) % 2] [x] [Y] & step <s) {// Convert Between graphs, however, it cannot correspond to the wall info in the next graph. X = x, info. y = Y, info. k = (k + 1) % 2, info. step = Step + 1; hash [(k + 1) % 2] [x] [Y] = 1; q. push (Info) ;}}}return false;} int main () {int t; scanf ("% d", & T); While (t --) {int n, m; scanf ("% d", & N, & M, & S); memset (MAP, 0, sizeof (MAP )); for (int K = 0; k <2; ++ K) {for (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= m; ++ J) {gchar (Map [k] [I] [J]); If (Map [k] [I] [J] = 's') {SX = I, Sy = J, SK = K;} If (Map [k] [I] [J] = 'P') {EX = I, ey = J, EK = K ;}}}} printf (BFS ()? "Yes \ n": "No \ n ");}}