Dating with Girls (2) Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2656 Accepted Submission (s): 741
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 Test instructions: Go from Y to G, take one second for each step. Is the way to go, #是墙, and ordinary BFS, the stone here # in time%k = = 0 can disappear, even if you can walk. If not equal to 0, it will appear again.
Analytical:
From the beginning to the end, BFS, different from the point in the diagram, may go several times, respectively, at different times.
vis[I [j] [T%k]=1: the coordinates (I,J) walk through the T-%k time, then the t%k will not have to go again.
#include <iostream> #include <algorithm> #include <queue> #include <algorithm> #include <
Cstring> #define MAXN using namespace std;
int n, m, K;
int SX, SY;
int vis[maxn][maxn][11];
Char MAP[MAXN][MAXN];
int dir[4][2] = {0, 1, 0,-1, 1, 0,-1, 0};
struct node {int x, y, step;};
void BFs () {memset (Vis, 0, sizeof (VIS));
queue<node>q;
Node St, Ed; St.x = SX; St.y = sy;
St.step = 0;
Q.push (ST);
while (!q.empty ()) {st = Q.front ();
Q.pop ();
if (map[st.x][st.y] = = ' G ') {printf ("%d\n", st.step);
return;
} for (int i = 0; i < 4; ++i) {ed = st;
Ed.x + = dir[i][0];
Ed.y + = dir[i][1];
ed.step++;
int d = ed.step% K; if (ed.x < 0 | | ed.x >= N | | Ed.y < 0 | | ed.y >= m | | (Map[ed.x][ed.y] = = ' # ' && D))
Continue
if (Vis[ed.x][ed.y][d]) continue; Q.push (Ed);
VIS[ED.X][ED.Y][D] = 1;
}} printf ("Please give me another chance!\n");
} int main () {int T;
scanf ("%d", &t);
while (t--) {scanf ("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; ++i) {scanf ("%s", Map[i]);
for (int j = 0; j < m; ++j) {if (map[i][j] = = ' Y ') sx = i, sy = j;
}} BFS ();
} return 0;
}