Dating with girls (2)
Time Limit:1000MS
Memory Limit:32768KB
64bit IO Format:%I64D &%i64u
The
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's 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 ' and ' 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 Test instructions: Dating girls from Y to g ask what the shortest distance is. For a walking path #为石头 where the stone can disappear when the time t%k==0, even if it can go, if t%k!=0 will appear again so we add an array to the original two-dimensional array to three bits The array is used to mark whether the T%k this time y walk through this point if the point before t%k time walk so no meaning AC code:
#include <stdio.h> #include <string.h> #include <queue> using namespace std;
int n, m, K, BX, by, ex, EY;
struct node {int x, y, t;};
Char maze[111][111];
int f[4][2] = {0,1,0,-1,1,0,-1,0};
int used[110][110][11];
int BFS () {queue<node>q;
Node now, next; now.x = bx; Now.y = by;
now.t = 0;
memset (used, 0, sizeof (used));
Used[bx][by][0] = 1;
Q.push (now); while (!q.empty ()) {now = Q.front ();
Q.pop ();
if (now.x = = Ex && now.y = = ey) {return now.t;
} for (int i = 0; i<4; i++) {int xx, yy;
xx = now.x + f[i][0];
yy = Now.y + f[i][1]; if (xx >= 1 && xx<=n&& yy >= 1 && yy<=m && (maze[xx][yy]! = ' # ' | |
(now.t + 1)% K = = 0) &&!used[xx][yy][(now.t + 1)% K]) {next.t = now.t+ 1;
Next.x = XX;
Next.y = yy;
used[xx][yy][(now.t + 1)% K] = 1;
Q.push (next);
}}} return-1;
} int main () {int I, j, T;
scanf ("%d", &t); WhilE (t--) {scanf ("%d%d%d", &n, &m, &k); for (int i = 1; I <= N;
i++) {scanf ("%s", maze[i]+1);
for (int j = 1; j<= m; j + +) {if (maze[i][j] = = ' Y ') {bx = i; by = j;}
else if (maze[i][j] = = ' G ') {ex = i; ey = j;}
}} int Flag=bfs ();
if (flag = =-1) printf ("Please give me another chance!\n");
else printf ("%d\n", flag);
} return 0; }