Dating with Girls (2) Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total submission (s): 2657 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
The classic BFS for the shortest way, but because the stone will be in and out, the need for another dimension variable to mark the state of time (in fact, only need to mark the results of the surplus can be), pay attention to this special situation, the other is nothing ...
#include <stdio.h> #include <string.h> #include <queue> using namespace std;
Char map[105][105];
BOOL vis[105][105][105];
int N,m,k,bx,by,ex,ey;
int dx[4]={-1,0,0,1},dy[4]={0,1,-1,0}; struct ZB {int x,y,time;}
X
void Slove () {memset (vis,0,sizeof (VIS));
queue< ZB > Q;
ZB bg={bx,by,0};
Q.push (BG);
Vis[bx][by][0]=1;
while (!q.empty ()) {X=q.front (); Q.pop ();
if (X.x==ex&&x.y==ey) {printf ("%d\n", x.time);
Return
} for (int i=0;i<4;++i) {ZB tp={x.x+dx[i],x.y+dy[i],x.time+1}; if (tp.x<0| | tp.x>n-1| | tp.y<0| | tp.y>m-1| |
map[tp.x][tp.y]== ' # ' &&tp.time%k!=0) {continue;
} if (!vis[tp.x][tp.y][tp.time%k]) {q.push (TP);
Vis[tp.x][tp.y][tp.time%k]=1;
}}} printf ("Please give me another chance!\n");
} int main () {int t;
Freopen ("Shuju.txt", "R", stdin);
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 i=0;i<n;++i) {for (int j=0;j<m;++j) {if (map[i][j]== ' Y ') {bx=i;by=j;
} else if (map[i][j]== ' G ') {ex=i;ey=j;
}}} slove ();
} return 0; }