HDU 2579 E - 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!".

题意:Y是男孩,G是女孩,#是墙,给出行和列和k,只有在bu's步数数时,才能通过墙,求Y是否能到达G

分析:需要注意的是点可以重复走,每一点如果步数对k取余相同的话,可以不入队,需要开一个三维数组开记录状态,另外,在走到墙的步数是要加一的,只须注意判断条件即可。

代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,k,x,y,c,d;
char s[110][110];
int book[110][110][20];
int to[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int x,y,step;
};
int bfs()
{
    queue<node>q;
    node now,end;
    now.x=x;
    now.y=y;
    now.step=0;
    book[now.x][now.y][0]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(s[now.x][now.y]=='G')
            return now.step;
        for(int i=0;i<4;i++)
        {
            end.x=now.x+to[i][0];
            end.y=now.y+to[i][1];
            end.step=now.step+1;
            if(end.x>=0&&end.y>=0&&end.x<n&&end.y<m&&book[end.x][end.y][end.step%k]==0) //判断
            {
                if(end.step%k==0)                       //步数能整出k 能过墙
                {
                    book[end.x][end.y][end.step%k]=1;
                    q.push(end);
                }
                else if(s[end.x][end.y]!='#')           //不能过墙
                {
                    book[end.x][end.y][end.step%k]=1;
                    q.push(end);
                }
            }
        }
    }
    return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(book,0,sizeof(book));
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
        {
            if(s[i][j]=='Y')
            {
                x=i;
                y=j;
                break;
            }
        }
        int k=bfs();
        if(k)
            printf("%d\n",k);
        else
            printf("Please give me another chance!\n");
    }
}

猜你喜欢

转载自blog.csdn.net/Vace___yun/article/details/81159952