zoukankan      html  css  js  c++  java
  • HDU2579(bfs迷宫)

    Dating with girls(2)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3006    Accepted Submission(s): 864


    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.
     
    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
     
    思路:若重新走到某位置所需的步数模K的值不同,那么可重复进队。否则意味着进入死循环。
    #include <cstdio>
    #include <string.h>
    #include <queue>
    using namespace std;
    const int MAXN=105;
    struct Node{
        int y,x,step;
        Node(){}
        Node(int y,int x,int step)
        {
            this->y=y;
            this->x=x;
            this->step=step;
        }
    };
    char mz[MAXN][MAXN];
    int n,m,k;
    int sy,sx;
    int dy[4]={0,1,0,-1};
    int dx[4]={1,0,-1,0};
    int vis[MAXN][MAXN][MAXN];
    void bfs()
    {
        memset(vis,0,sizeof(vis));
        queue<Node> que;
        que.push(Node(sy,sx,0));
        vis[sy][sx][0%k]=1;
        while(!que.empty())
        {
            Node now=que.front();que.pop();
            if(mz[now.y][now.x]=='G')
            {
                printf("%d
    ",now.step);
                return ;
            }
            for(int i=0;i<4;i++)
            {
                int ny=now.y+dy[i];
                int nx=now.x+dx[i];
                int ns=now.step+1;
                if(0<=ny&&ny<n&&0<=nx&&nx<m&&!vis[ny][nx][ns%k])
                {
                    if(mz[ny][nx]!='#')
                    {
                        vis[ny][nx][ns%k]=1;
                        que.push(Node(ny,nx,ns));
                    }
                    else
                    {
                        if(ns%k==0)
                        {
                            vis[ny][nx][ns%k]=1;
                            que.push(Node(ny,nx,ns));
                        }
                    }
                }
            }
        }
        printf("Please give me another chance!
    ");
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d%d",&n,&m,&k);    
            for(int i=0;i<n;i++)
            {
                scanf("%*c");
                for(int j=0;j<m;j++)
                {
                    scanf("%c",&mz[i][j]);
                    if(mz[i][j]=='Y')
                    {
                        sy=i;
                        sx=j;
                    }
                }
            }
            bfs();
        }
        return 0;
    }
  • 相关阅读:
    get函数理解
    get函数理解
    Absolute C++ 第9章字符串 编程练习4
    Absolute C++ 第9章字符串 编程练习4
    Absolute C++ 第9章字符串 编程练习4
    Absolute C++ 第9章字符串 编程练习4
    C字符串使用陷阱 “=”和“==” 学习笔记
    [Android]ListView中分割线的设置
    如何在adapter 中调用activity的方法
    Android开发中Handler的经典总结
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5691763.html
Copyright © 2011-2022 走看看