HDOJ 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): 2659    Accepted Submission(s): 744


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.
HDOJ 2579 Dating with girls(2) (BFS+三维标记好)_第1张图片
 

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 和普通BFS不同的是,不用标记空地是否走过,但是需要标记重复的路  第一次走到a.step%k时, 标记一下,那下次走到a.step%k时就不用走了,因为会重复 ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<iostream>
#include<algorithm>
#define MAXN 105
using namespace std;
struct s
{
    int x;
    int y;
    int step;
    friend bool operator<(s a,s b)
    {
        return a.step>b.step;
    }
};
int v[MAXN][MAXN][MAXN];
char map[MAXN][MAXN];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n,m,k,ans,bz;
int check(s aa)
{
    if(aa.x>=0&&aa.x<n&&aa.y>=0&&aa.y<m)
    return 0;
    return 1;
}
void bfs(int xx,int yy)
{
    memset(v,0,sizeof(v));
    priority_queue<s>q;
    s a,b;
    int i;
    a.x=xx;
    a.y=yy;
    a.step=0;
    v[0][xx][yy]=1;
    q.push(a);
    while(!q.empty())
    {
        a=q.top();
        q.pop();
        if(map[a.x][a.y]=='G')
        {
            ans=a.step;
            bz=1;
            return;
        }
        for(i=0;i<4;i++)
        {
            b=a;
            b.x=b.x+dir[i][0];
            b.y=b.y+dir[i][1];
            b.step=b.step+1;
            if(check(b))
            continue;
            if(map[b.x][b.y]=='#')
            {
                if(b.step%k==0)
                {
                    if(!v[b.step%k][b.x][b.y])
                    {
                        q.push(b);
                        v[b.step%k][b.x][b.y]=1;
                    }
                }
            }
            else
            {
                if(!v[b.step%k][b.x][b.y])
                {
                    q.push(b);
                    v[b.step%k][b.x][b.y]=1;
                }
            }
        }
    }
}
int main()
{
    int t,c,d,j,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(i=0;i<n;i++)
        scanf("%s",map[i]);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(map[i][j]=='Y')
                {
                    c=i;
                    d=j;
                    break;
                }
            }
        }
        bz=0;
        bfs(c,d);
        if(bz)
        printf("%d\n",ans);
        else
        printf("Please give me another chance!\n");
    }
    return 0;
}



你可能感兴趣的:(HDOJ 2579 Dating with girls(2) (BFS+三维标记好))