zoukankan      html  css  js  c++  java
  • HDOJ1242(延时迷宫BFS+优先队列)

    Rescue

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 25485    Accepted Submission(s): 9022


    Problem Description
    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

    Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

    You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
     
    Input
    First line contains two integers stand for N and M.

    Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. 

    Process to the end of the file.
     
    Output
    For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
     
    Sample Input
    7 8
    #.#####.
    #.a#..r.
    #..#x...
    ..#..#.#
    #...##..
    .#......
    ........
     
    Sample Output
    13
    #include <iostream>
    #include <queue>
    using namespace std;
    const int MAXN=205;
    const int INF=0x3f3f3f3f;
    struct Node{
        int y,x,step;
        Node(){}
        Node(int y,int x,int step)
        {
            this->y=y;
            this->x=x;
            this->step=step;
        }
        bool operator<(const Node &no)const
        {
            return step > no.step;
        }
    };
    int n,m;
    char mz[MAXN][MAXN];
    int t[MAXN][MAXN];
    int dy[4]={0,1,0,-1};
    int dx[4]={1,0,-1,0};
    int sy,sx;
    int gy,gx;
    void bfs()
    {
        for(int i=0;i<MAXN;i++)
            for(int j=0;j<MAXN;j++)    t[i][j]=INF;
        priority_queue<Node> que;
        que.push(Node(sy,sx,0));
        t[sy][sx]=0;
        while(!que.empty())
        {
            Node now=que.top();que.pop();
            if(now.y==gy&&now.x==gx)
            {
                cout<<now.step<<endl;
                return ;
            }
            for(int i=0;i<4;i++)
            {
                int ny=now.y+dy[i];
                int nx=now.x+dx[i];
                if(0<=ny&&ny<n&&0<=nx&&nx<m&&mz[ny][nx]!='#')
                {
                    int nstep;
                    if(mz[ny][nx]=='x')    nstep=now.step+2;
                    else    nstep=now.step+1;
                    if(nstep<t[ny][nx])
                    {
                        t[ny][nx]=nstep;
                        que.push(Node(ny,nx,nstep));
                    }
                }
            }
        }
        cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    }
    
    int main()
    {
        while(cin>>n>>m)
        {
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                {
                    cin>>mz[i][j];
                    if(mz[i][j]=='a')
                    {
                        sy=i;
                        sx=j;
                    }
                    else if(mz[i][j]=='r')
                    {
                        gy=i;
                        gx=j;
                    }
                    else ;
                }
            bfs();
        }
        return 0;
    }
  • 相关阅读:
    hadoop配置支持LZO压缩格式并支持分片
    CSS 图片:如何使用 CSS 来布局图片
    CSS3 用户界面:用户界面特性来调整元素尺寸,框尺寸和外边框
    CSS3 多列:如何将文本内容设计成像报纸一样的多列布局?
    mac Pycharm:如何使用anaconda安装jieba
    pycharm如何安装jieba词频统计器?
    CSS3 动画:使元素从一种样式逐渐变化为另一种样式的效果
    CSS3 过渡:用鼠标移过逐渐改变它原有样式
    CSS3 3D 转换:使用 3D 转换来对元素进行格式化
    CSS3 2D 转换:对元素进行移动、缩放、转动、拉长或拉伸
  • 原文地址:https://www.cnblogs.com/program-ccc/p/4767718.html
Copyright © 2011-2022 走看看