hdu 2579 Dating with girls(2) (bfs)

Dating with girls(2)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1812    Accepted Submission(s): 505


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!".
 

 

Sample Input
1 6 6 2 ...Y.. ...#.. .#.... ...#.. ...#.. ..#G#.
 

 

Sample Output
7
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1175 1072 1728 1180 1026 
 
 1 //15MS    808K    1432 B    G++
 2 /*
 3 
 4     题意:
 5         给出一个nm图,问点Y到点G的最少步数,其中每当步数为k的整数倍时障碍物可以通行。 
 6 
 7     bfs:
 8         比较明显是一道bfs,不过有点小变形,需要一个三维数组来记录步数。 第三维记录模k的余数,
 9     此处可以减低时间复杂度和空间复杂度。避免重复且耗时更多的情况。 
10         
11 */
12 #include<iostream>
13 #include<queue>
14 #define inf 0x7ffffff
15 using namespace std;
16 struct node{
17     int x,y,cnt;
18 };
19 char g[105][105]; 
20 int step[105][105][10];
21 int n,m,k;
22 int sx,sy,ex,ey;
23 int mov[4][2]={0,1,1,0,0,-1,-1,0};
24 int bfs()
25 {
26     memset(step,-1,sizeof(step));
27     queue<node>Q;
28     node t={sx,sy,0};
29     Q.push(t);
30     while(!Q.empty()){
31         t=Q.front();
32         Q.pop();
33         if(t.x==ex && t.y==ey){
34             return t.cnt;
35         }
36         for(int i=0;i<4;i++){
37             node tt=t;
38             tt.cnt++;
39             tt.x+=mov[i][0];
40             tt.y+=mov[i][1];
41             if(tt.x>=0&&tt.x<n && tt.y>=0&&tt.y<m){ 
42                 if(g[tt.x][tt.y]=='#' && tt.cnt%k!=0) continue;
43                 if(step[tt.x][tt.y][tt.cnt%k]!=-1) continue;
44                 step[tt.x][tt.y][tt.cnt%k]=tt.cnt;
45                 Q.push(tt);
46             }
47         }
48     }
49     return -1;
50 }
51 int main(void)
52 {
53     int t;
54     scanf("%d",&t);
55     while(t--)
56     {
57         scanf("%d%d%d",&n,&m,&k);
58         for(int i=0;i<n;i++){
59             scanf("%s",g[i]);
60             for(int j=0;j<m;j++){
61                 if(g[i][j]=='Y')
62                     sx=i,sy=j;
63                 if(g[i][j]=='G')
64                     ex=i,ey=j;
65             }
66         }
67         int ans=bfs();
68         if(ans==-1) puts("Please give me another chance!");
69         else printf("%d\n",ans);
70     }
71     return 0;
72 }

 

posted @ 2013-12-26 15:40  heaventouch  阅读(270)  评论(0编辑  收藏  举报