/*---------------------------------------------time:14:45-15:25 2012.2.13 stratege:bfs (keypoint: Defines a three-dimensional array, nu
M[X][Y][Z]; X, y is used to store the current maze coordinates, Z is used to store the time traveled and the modulus of k in the topic. Because x, y can go back and forth, so we need to open one-dimensional array, update the state Acmer:johnsonddu problem:2579 (Dating with Girls (2)) Judge status:accepted runid:5111908 language:c++ author:a312745658------------------------------------- ----------*/#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #incl
Ude <memory.h> #include <queue> #include <algorithm> using namespace std;
#define INF 0xfffffff struct Node {int x, y;
int step;
}n, M;
int direct[4][2] = {1, 0, 0,-1,-1, 0, 0, 1};
Char map[105][105];
BOOL Flag;
int num[105][105][11];
int R, C, K;
int SX, SY, DX, dy;
int tcase;
int main () {int I, j, K;
Cin >> Tcase; while (Tcase-) {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 ') sx = i, sy = j;
if (map[i][j] = = ' G ') dx = i, dy = j;
} for (i = 0; i < R; i + +) for (j = 0; J < C; J + +) for (k = 0; k < one; k + +) Num[i][j][k] = INF;
The number of steps to initialize is maximum to update the status flag = FALSE;
n.x = SX;
N.Y = sy;
N.step = 0;
Queue <Node> Q;
Q.push (n); while (!
Q.empty ()) {m = Q.front ();
Q.pop ();
if (m.x = = DX && m.y = = dy) {flag = true;
break;
}//cout << "(" << m.x << "," << m.y << ")" << Endl;
for (i = 0; i < 4; i + +) {n.x = m.x + direct[i][0];
N.Y = M.y + direct[i][1];
N.step = M.step + 1;
if (n.x >= 0 && n.y >= 0 && n.x < R && N.Y < C) {
if (map[n.x][n.y] = = ' # ' && n.step% k! = 0)//if n.step% K = = 0, the stone will disappear, can walk continue;
if (Num[n.x][n.y][n.step%k] <= n.step)//Update step number.
Continue;
Num[n.x][n.y][n.step%k] = N.step;
Q.push (n);
}}} if (flag) printf ("%d\n", m.step);
else printf ("Please give me another chance!\n");
} return 0;
}