# 题目

## Problem Description

If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is 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, then you can date with the girl.Else the girl will date with other boys. What a pity!
The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there.
There are 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 begins with three integers r and c (1 <= r , c <= 100), 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 chance!".

1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.

7

# 代码

```/*
By:OhYee
Github:OhYee
Email:oyohyee@oyohyee.com
Blog:http://www.cnblogs.com/ohyee/

かしこいかわいい？
エリーチカ！

*/

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
using namespace std;

//DEBUG MODE
#define debug 0

//循环
#define REP(n) for(int o=0;o<n;o++)

const int maxn = 105;
const int maxk = 15;
int n,m,k;
int dis[maxn][maxn][maxk];
char Map[maxn][maxn];

const int delta[] = {1,-1,0,0};

struct point {
int x,y;
int dis;
point(int a,int b,int c) {
x = a;
y = b;
dis = c;
}
};

int BFS(int s1,int s2,int v1,int v2) {
memset(dis,-1,sizeof(dis));
queue<point> Q;

Q.push(point(s1,s2,0));
dis[s1][s2] = 0;
while(!Q.empty()) {
int x = Q.front().x;
int y = Q.front().y;
int dist = Q.front().dis;
Q.pop();

REP(4) {
int xx = x + delta[o];
int yy = y + delta[3 - o];
if(xx < 0 || xx >= n || yy < 0 || yy >= m)
continue;
if(Map[xx][yy] != '#' || (Map[xx][yy] == '#' && (dist+1) % k == 0)) {
int dd = dist + 1;
if(dis[xx][yy][dd%k] == -1) {
dis[xx][yy][dd%k] = dd;
if(xx == v1 && yy == v2)
return dd;
Q.push(point(xx,yy,dd));

}
}
}
}
return -1;
}

void Do() {
scanf("%d%d%d",&n,&m,&k);
int s1,s2,v1,v2;
for(int i = 0;i < n;i++)
for(int j = 0;j < m;j++) {
scanf("\n%c\n",&Map[i][j]);
if(Map[i][j] == 'Y') {
s1 = i;
s2 = j;
}
if(Map[i][j] == 'G') {
v1 = i;
v2 = j;
}
}
int ans = BFS(s1,s2,v1,v2);
if(ans == -1)
printf("Please give me another chance!\n");
else
printf("%d\n",ans);
}

int main() {
int T;
scanf("%d",&T);
while(T--)
Do();
return 0;
}
```