Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2102
Problem description after the poor 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 devil has sent out a message saying that he will eat the princess at T moment, because he heard the rumor that the meat of the princess can survive forever. 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.
According to the investigation, 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, and the space-time transmission is represented, the wall is represented by *, and flat is used.. As soon as the Knights enter the time-space transmitter, they will be transferred to the relative position of the other layer. However, if they are 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 movement can only pass through the time-space transmitter, and does not require any time. 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 Input
15 5 14S*#*..#........****....#...*.P#.*..***.....*.*.#..
Sample output
YES
Relatively pitfall:
/* 1. When you encounter '#', the shipper is required. There is no second option.
2. It is possible that the start point and the end point are in the same layer. The example is in two layers. The start and end can be anywhere.
3. When both sides are a portal, or when one side is a portal and the other is a wall, the two conditions are dead.
4. Pay attention to the blank rows between the two maps during input. Special processing is required.
*/
The Code is as follows:
# Include <cstdio> # include <cstring> # include <queue> # include <algorithm> # include <iostream> using namespace STD; # define M 17int XX [4] = {, 0,-1}; int YY [4] = {,-}; int x, y, z; int n, m, tTime; char map [2] [m] [m]; int vis [2] [m] [m]; // mark struct node {int X, y, Z; int time ;}; // Q. push (Front); void BFS () {int I; int dx, Dy, DZ; queue <node> q; node front, rear; memset (VIS, 0, sizeof (VIS); front. X = x, front. y = Y, front. z = z; f Ront. Time = 0; vis [Z] [x] [Y] = 1; q. Push (Front); // if this definition is outside, you must clear the while (! Q. empty () {front = Q. front (); q. pop (); for (I = 0; I <4; I ++) {dx = front. X + XX [I]; DY = front. Y + YY [I]; DZ = front. z; // determine whether to cross the border, whether the target point is a wall, and whether to time out if (dx> = 0 & DX <n & dy> = 0 & dy <M &&! Vis [dz] [dx] [dy] & map [dz] [dx] [dy]! = '*' & Front. time <tTime) {If (Map [dz] [dx] [dy] = '#'&&! Vis [dz] [dx] [dy]) {// It indicates that the upload is successful. vis [dz] [dx] [dy] = 1; if (front. z = 1) // if it is in the upper layer, it is uploaded to the lower layer DZ = 0; else if (front. z = 0) // if it is in the lower layer, it is uploaded to the upper layer DZ = 1;} vis [dz] [dx] [dy] = 1; if (Map [dz] [dx] [dy] = 'P') // find Princess {printf ("Yes \ n"); return;} rear. X = DX, rear. y = Dy; rear. z = DZ, rear. time = front. time + 1; q. push (rear) ;}}} printf ("NO \ n") ;}int main () {int C; int I, j, k; while (~ Scanf ("% d", & C) {While (c --) {// while (! Q. empty () // Q. pop (); // scanf ("% d", & N, & M, & tTime); CIN> N> m> tTime; for (k = 0; k <2; k ++) {for (I = 0; I <n; I ++) {// scanf ("% s ", map [k] [I]); For (j = 0; j <m; j ++) {CIN> map [k] [I] [J]; if (Map [k] [I] [J] = 's') {x = I, y = J, Z = K ;}}}} for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {If (Map [0] [I] [J] = '#' & map [1] [I] [J] = '*') {// if there is a wall above the transmitter, it will crash .... // You should also exclude this situation. Map [0] [I] [J] = '*';} if (Map [0] [I] [J] = '*' & map [1] [I] [J] = '#') {// If a wall is under the transmitter, it will also crash and exclude map [1] [I] [J] = '*';} if (Map [0] [I] [J] = '#' & map [1] [I] [J] = '#') {// If the transmitter is still above the transmitter, it will be in an endless loop. // in this case, we set the upper and lower locations as the wall. map [0] [I] [J] = '*', map [1] [I] [J] = '*' ;}} BFS ();}} return 0 ;}