Dating with girls(2)

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

题意:输入三个数,n,m,t,分别表示行,列,和k(k就是取余的数),男孩在Y处,女孩在G除,#表示墙。特别之处是如果男孩在t时刻走到了墙的位置,理论上他是不能走的,但是如果t对k取余为0,那么墙就崩塌,男孩就能穿墙而过。问男孩找到女孩所用的最小时间。

思路:这个题不是传统的广搜,传统的广搜如果有墙的话,是无论如何也不能走的,并且一个位置走过了,就标记为1。但是这个题在不同时间墙是可以崩塌的,所以原来走过的点因为墙能崩塌也可以再走。那我们要怎末标记呢?我们用t对k取的余数来判标记。因为如果这个位置余数为1走过了,下一次在遇到余数为1的就不用再走了,因为只是重复上面的过程,并且白白浪费时间,无谓地重复。所以要在传统的广搜上加一个对某个位置余数出现与否的判断。代码如下:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char a[110][110];
int n,m,k;
int dir[4][2]={0,1,1,0,0,-1,-1,0};//四个方向
int book[110][110][10];
struct node
{
    int x,y,f;
};
int angel(int x,int y)
{
    queue<node>Q;
    node p,q;
    q.x=x;
    q.y=y;
    q.f=0;
    book[x][y][0]=1;
    Q.push(q);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            int tx=p.x+dir[i][0];
            int ty=p.y+dir[i][1];
            if(tx<0||tx>=n||ty<0||ty>=m||book[tx][ty][(p.f+1)%k]==1)  //余数出现过标记为1
                continue;
            if(a[tx][ty]=='#')
            {
                if((p.f+1)%k!=0)  //这个位置的这个余数出现过。continue掉避免无畏的重复。
                continue;
            }
            book[tx][ty][(p.f+1)%k]=1;//标记一下
            q.x=tx;
            q.y=ty;
            q.f=p.f+1;
            if(a[tx][ty]=='G')
                return q.f;
            Q.push(q);
        }
    }
    return -1;
}
int main()
{
    int T,i,j,sx,sy,t;
    scanf("%d",&T);
    while(T--)
    {
        memset(book,0,sizeof(book));
        scanf("%d %d %d",&n,&m,&k);
        for(i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            for(j=0;j<m;j++)
            {
                if(a[i][j]=='Y')
                {
                    sx=i;
                    sy=j;
                }
            }
        }
        t=angel(sx,sy);
        if(t!=-1)
            printf("%d\n",t);
        else
            printf("Please give me another chance!\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值