Dating with Girls (2)
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1476 Accepted Submission (s): 419
Problem Description If You had solved the problem Dating with girls (1). I Think you can solve this problem too. This problem was also about dating with girls. Now you are in a maze and the girl you want to date with is also in the maze. If you can find the girl and then you can date with the girl. Else the girl would date with other boys. What a pity!
The Maze is very strange. There is many stones in the maze. The stone'll disappear at time t if it's a multiple of K (2<= K <=), on the other time, stones'll be still th Ere.
There is only '. ' or ' # ', ' Y ', ' G ' on the map of the maze. '. ' Indicates the blank which you can move on, ' # ' indicates stones. ' Y ' indicates the your location. ' G ' indicates the girl ' s location. There is only one ' Y ' and one ' G '. Every seconds you can move left, right, up or down.
Input The first line contain an integer T. Then T cases followed. Each case is begins with three integers r and C (1 <= R, c <=), and K (2 <=k <= 10).
The next R line is the map ' s description.
Output for each cases, if you can find the girl, output the least time in seconds, else output "Please give me another cha Nce! ".
Sample Input
1 6 6 2 ... Y ..... .#.... ...#.. ...#.. .. #G #.
Sample Output
7
/* The barrier disappears when the time can be divisible by K. The meaning of the one dimension in the general two-dimensional maze-weight array is the remainder of the total time modulus K mark[i][j][t] meaning is the total time modulus K remainder time at x, y points because the remainder of a lattice represents a different state
In these states the expansion of the node may be different so it requires a three-dimensional weight, and when the remainder of the same time there is no need to continue to expand, because the next extension of the node their state we must have expanded. */#include <iostream> #include <cstdio> #include <cstdlib> #include <string> #include < cstring> #include <cmath> #include <algorithm> #include <queue> #include <stack> #include
<set> #include <iomanip> const int max=101; int direction[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//represents the upper and lower left and right four directions char map[max][max];//maze Map int mark[max][max][max];//
If the time for the T case (x, y) point is marked with an int r,c,k;//row, column, k value int x,y,xx,yy,step;//(x, y) represents the starting point where the boy is located, (XX,YY) is the end point that the girl's location using namespace std;
typedef struct NODE//Defines the struct body {int x;
int y;
int time;
}node;
Node S[max]; BOOL Judge (node X)//judgment {if (x.x<0| | x.x>=r| | x.y<0| |
X.Y>=C)//If out of bounds return false;
if (Mark[x.x][x.y][x.time%k])//If the point is already marked in return false; if (map[x.x][x.y]== ' # ' &&x.time%k!=0)//If you go to the stone but the stoneThe head does not disappear return false;
return true;
} int BFS () {node temp,a;
queue<node>que;//defines a queue a.x=x;//assigns the initial value a.y=y;
a.time=0;
Que.push (a);
mark[a.x][a.y][a.time]=1;//beginning already marked while (!que.empty ())//start deep Search {A=que.front ();
Que.pop ();
for (int i=0;i<4;i++) {temp.x=a.x+direction[i][0];
TEMP.Y=A.Y+DIRECTION[I][1];
temp.time=a.time+1;
if (judge (temp)) {if (TEMP.X==XX&&TEMP.Y==YY)//If the endpoint is searched, return temp.time;
Mark[temp.x][temp.y][temp.time%k]=1;
Que.push (temp);
}}} return-1;//return if not found-1} int main () {int t,i,j,ans;
cin>>t;
while (t--) {cin>>r>>c>>k;
for (i=0;i<r;i++) {for (j=0;j<c;j++) {cin>>map[i][j];
if (map[i][j]== ' Y ') { X=i;
Y=j;
} if (map[i][j]== ' G ') {xx=i;
Yy=j;
}}} memset (Mark,0,sizeof (Mark));
Ans=bfs ();
if (ans!=-1) cout<<ans<<endl; else cout<< "Please give me another chance!"
<<endl;
} return 0;
}