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 Simple BFS
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm> #define INF 1e9
using namespace Std;
Char maze[110][110];
int n,m,k; int Sx,sy,ex,ey;///sx,sy Record starting point, Ex,ey record end int ans[110][110][12];///ans[i][j][p] means "the time required to reach (i,j) position the remainder of the K-surplus is P" in this state,
The minimum time required to reach this state is the struct node {int x,y,time;};
int f[4][2]={1,0,-1,0,0,1,0,-1};
int BFs () {queue<node> q;
Node Now,next;
now.x=sx,now.y=sy,now.time=0;
Q.push (now); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) for (int p=0;p<=k;p++) ans[i][j][p]=in
F
ans[sx][sy][0]=0;
while (Q.size ()) {Now=q.front ();
Q.pop ();
if (Now.x==ex&&now.y==ey) return now.time;///because it is BFS, so the first time to reach the end is definitely the least time for (int i=0;i<4;i++) {
int mx=now.x+f[i][0];
int my=now.y+f[i][1]; if (mx<=0| | mx>n| | my<=0| |
MY>M) continue;
Next.x=mx,next.y=my; Next.time=now.time+1;
int kk=next.time%k;
if (maze[mx][my]!= ' # ' &&ans[mx][my][kk]>next.time) {ans[mx][my][kk]=next.time;
Q.push (next); } else if (maze[mx][my]== ' # ' &&kk==0&&ans[mx][my][kk]>next.time)///This position is a stone, then you should see the total time here is not K's
Multiples, is the word stone will disappear, can walk {ans[mx][my][kk]=next.time;
Q.push (next);
}}} return-1;
} int main () {int 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 ') {sx=i;
Sy=j;
} if (maze[i][j]== ' G ') {ex=i;
Ey=j;
}
}
} int Res=bfs ();
if (Res==-1) puts ("Please give me another chance!");
else printf ("%d\n", res);
} return 0;
}