Dating with girls (2)
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1418 Accepted Submission (s): 393
Problem descriptionif You has 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.
Inputthe 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.
Outputfor cases, if can find the girl, output the least time in seconds, else output "Please give me another Chan Ce! ".
Sample Input16 6 2...y.....#...#.......#.....# .... #G #.
Sample Output7
Sourcehdu 2009-5 Programming Contest
Recommendlcy Move one block per second, only up and down the problem is a search problem, because the shortest time required to arrive, with BFS will be better, it is necessary to note that the stone will disappear at some time, so that not only in the search for the current time to judge whether the plot has a stone , the path of the weight should also pay attention to the ordinary search topic when the weight is not repeated through the same parcel (will lead to the same state, thus repeating the search), and this problem in two states is the same
on the premise of the same parcel, the time to disappear from the next stone is the same.
Therefore, a three-dimensional array, VIS[MAXN][MAXN][K_MAXN] is required to mark the status code:
1#include <stdio.h>2#include <string.h>3#include <queue>4 using namespacestd;5 intN,m,k,sx,sy,ex,ey;6 structhaha7 {8 intx, y, step;9 }q,temp;Ten Charmap[111][111]; One intdir[4][2]={0,1,0,-1,1,0,-1,0}; A intvis[111][111][ One]; - voidBFS () - { the inti; -q.x=SX; -q.y=Sy; -q.step=0; +queue<structHaha>que; -memset (Vis,0,sizeof(Vis)); +vis[sx][sy][0]=1; A Que.push (q); at while(!que.empty ()) - { -temp=Que.front (); - Que.pop (); - if(temp.x==ex&&temp.y==ey) - { inprintf"%d\n", temp.step); - return ; to } + for(i=0;i<4; i++) - { the intXx,yy; *xx=temp.x+dir[i][0]; $yy=temp.y+dir[i][1];Panax Notoginseng if(xx>=0&&xx<n&&yy>=0&&yy<m&& (map[xx][yy]!='#'|| (temp.step+1)%k==0) &&!vis[xx][yy][(temp.step+1)%K]) - { theq.step=temp.step+1; +q.x=xx; Aq.y=yy; thevis[xx][yy][(temp.step+1)%k]=1; + Que.push (q); - } $ } $ } -printf"Please give me another chance!\n"); - } the intMain () - {Wuyi intI,j,cas; thescanf"%d",&CAs); - while(cas--) Wu { -scanf" %d%d%d",&n,&m,&k); About for(i=0; i<n;i++) $ { -scanf"%s", Map[i]); - for(j=0; j<m;j++) - { A if(map[i][j]=='Y') {sx=i;sy=J;} + Else if(map[i][j]=='G') {ex=i;ey=J;} the } - } $ BFS (); the } the return 0; the}
Hdu2579--dating with Girls (2)--(DFS, weight)