Click to open link
Plan A
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total Submission (s): 10939 Accepted Submission (s): 2667
Problem description Poor princess in the devil after Time and again by the Knights rescued back, now, unfortunately, she once again face the test of life. The devil has already sent a message that the princess will be eaten at T-time because he listens to rumors that eating the princess's flesh can also be immortal. The old king was so worried that he called on the world Warrior to save the princess. But the princess has long been accustomed to, she is convinced that Intelligent Knight LJ will certainly be able to rescue her.
Now according to the spy, the princess was locked in a two-storey maze, the entrance to the maze is S (0,0,0), the location of the princess with P, space-time transmission machine with #, the wall with *, flat with. As soon as the Knights entered the space-time transmitter they would be transferred to the opposite position of the other, but if the position was to be transferred to the wall, the Knights would be killed. The Knights can only move around in the first layer, and each shift takes 1 moments. The movement of the layers can only pass through the time-space transfer machine and does not require any time.
The first line of input, C, indicates a total of C test data, with three integer n,m,t on the previous line of each test data. N,m the size of the maze n*m (1 <= n,m <=10). T as you mean. The next n*m indicates the first layer of the maze arrangement, after the n*m to indicate the second floor of the maze layout situation.
Output "YES" If the Knights can find the princess at t time, otherwise the output "NO".
Sample Input
5 14s*#*. #........****....#...*. p#.*. ***.....*.*.#..
Sample Output
YES
Sourcehdu 2007-6 Programming Contest
Direct wide search, but there are several pit points:
1. Starting point is (0,0,0)
2. Time is less than or equal to T
3. The first and second layers are "#", so this point is the same as the second layer, the "*" result.
0ms1464k#include<stdio.h> #include <string.h> #include <queue>using namespace Std;int n,m,t,sx,sy , Ex,ey,sz,ez;int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int vis[2][17][17];char g[2][17][17];struct sa{int x,y,z,s;}; int BFs () {queue<sa>q; memset (vis,0,sizeof (VIS)); SA now,next; now.z=0;now.x=0;now.y=0;now.s=0; Vis[sz][sx][sy]=1; Q.push (now); while (!q.empty ()) {Now=q.front (); Q.pop (); if (Now.z==ez&&now.x==ex&&now.y==ey) {if (now.s<=t) return 1; return-1; } for (int i=0;i<4;i++) {int xx=now.x+dir[i][0]; int yy=now.y+dir[i][1]; int zz=now.z; if (xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[zz][xx][yy]&&g[zz][xx][ yy]!= ' * ') {vis[zz][xx][yy]=1; next.z=zz;next.x=xx;next.y=yy;next.s=now.s+1; if (g[zz][xx][yy]!= ' # ') Q.push (next); else {if (now.z==1&&!vis[0][xx][yy]&&g[0][xx][yy]!= ' * ') {if (g[0][xx][yy]!= ' # ') Next.z=0,q.push (next); } if (now.z==0&&!vis[1][xx][yy]&&g[1][xx][yy]!= ' * ') { if (g[1][xx][yy]!= ' # ') Next.z=1,q.push (next); }}}}} return-1;} int main () {//freopen ("In.txt", "R", stdin); int T; scanf ("%d", &t); while (t--) {scanf ("%d%d%d", &n,&m,&t); for (int z=0;z<2;z++) for (int i=0;i<n;i++) {scanf ("%s", G[z][i]); for (int j=0;j<m;j++) {if (g[z][i][j]== ' S ') {sx=i;sy=j;sz=z;} if (g[z][i][j]== ' P ') {ex=i;ey=j;ez=z;} }} if (BFS () ==-1) printf ("no\n"); else printf ("yes\n"); } return 0;}
HDU 2102 a plan BFS