hdu 1240(广搜障碍消失)

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.
hdu 1240(广搜障碍消失)

 

 

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

HDU 2009-5 Programming Contest

 

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  1253 1175 1072 1180 1026 

增加一维保存时间余k的值,满足这个值为0石头就消失

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 105
int r1,c1,r2,c2;
int k;
int t,r,c;

using namespace std;
struct node
{
    int r,c,time,dist;
    node()
    {

    }
    node(int r,int c,int t,int d):r(r),c(c),time(t),dist(d){}
};
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int vis[maxn][maxn][12];
char mp[maxn][maxn];
bool cm(int r,int c,int time)
{
    if(!vis[r][c][time]&&(mp[r][c]=='.'||time==0))
        return true;
    return false;
}
int bfs()
{queue<node>q;
    memset(vis,0,sizeof(vis));
    while(!q.empty())
        q.pop();
    q.push(node(r1,c1,0,0));
    vis[r1][c1][0]=1;
    node u,v;
while(!q.empty())
{u=q.front();
q.pop();
    for(int i=0;i<4;i++)
    {
        int nr=u.r+dx[i];
        int nc=u.c+dy[i];
        int nd=u.dist+1;
        int nt=(u.time+1)%k;
     if(nr>=1&&nr<=r&&nc>=1&&nc<=c&&cm(nr,nc,nt))
     {
         vis[nr][nc][nt]=1;
         q.push(node(nr,nc,nt,nd));
         if(nr==r2&&nc==c2)
            return nd;
     }

    }
}
    return -1;


}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&r,&c,&k);
        for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
            {scanf(" %c",&mp[i][j]);
            if(mp[i][j]=='Y')
            {mp[i][j]='.';
                r1=i;
            c1=j;
            }
            else if(mp[i][j]=='G')
            {mp[i][j]='.';
                r2=i;
                c2=j;
            }
            }
        int ans=bfs();
        if(ans==-1)
            printf("Please give me another chance!\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}