欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Dating with girls(2)

程序员文章站 2022-07-14 23:02:21
...

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. 
Dating with girls(2)

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

题意:从Y到G,#是石头,在时间为k的整数倍时会消失,求最少多长时间到G。

思路:到达一个点我可以在不同时间到达,因此过石头的时候也不一定,一个点可能需要走n次。所以需要对k求余。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
struct lou
{
    int x,y,t;
};
char s[110][110];
int xx,yy,m,n,k,r[4][2]={0,1,0,-1,1,0,-1,0},book[110][110][15];
void bfs()
{
    queue<lou>Q;
    lou p,q;
    int i;
    memset(book,0,sizeof(book));
    p.x=xx;
    p.y=yy;
    p.t=0;
    book[xx][yy][0]=1;
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        if(s[p.x][p.y]=='G')
        {
            printf("%d\n",p.t);
            return;
        }
        if(s[p.x][p.y]=='#')
        {
            if(p.t%k!=0)
                continue;
        }
        for(i=0;i<4;i++)
        {
            q.x=p.x+r[i][0];
            q.y=p.y+r[i][1];
            q.t=p.t+1;
            if(q.x<0||q.x>=n||q.y<0||q.y>=m||book[q.x][q.y][q.t%k])
                continue;
            book[q.x][q.y][q.t%k]=1;
            Q.push(q);
        }
    }
    printf("Please give me another chance!\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        int i,j;
        for(i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            for(j=0;j<m;j++)
            {
                if(s[i][j]=='Y')
                    {
                        xx=i;
                        yy=j;
                    }
            }
        }
        bfs();
    }
    return 0;
}