zoukankan      html  css  js  c++  java
  • HDU 1242 Rescue(BFS + 优先队列)

    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.) 

    InputFirst 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. 
    OutputFor 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>
    #include<cstring>
    using namespace std;
    const int maxn=205;
    char maze[maxn][maxn];
    bool vis[maxn][maxn];
    struct node
    {
        int x,y,step;
        bool operator < (const node &a) const//重载"<",步数少的优先级高
        {
            return step > a.step;
        }
    };
    int n,m,sx,sy,gx,gy;
    int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
    int bfs()
    {
        priority_queue<node> que;
        node cur;
        cur.step=0;
        cur.x=sx;
        cur.y=sy;
        que.push(cur);
        while(que.size())
        {
            cur=que.top();//优先队列取出第一个元素用top
            que.pop();
            if(cur.x==gx&&cur.y==gy)
                return cur.step;
            for(int i=0;i<4;i++)
            {
                node next;
                next.x=cur.x+dx[i],next.y=cur.y+dy[i];
                if(0<=next.x&&next.x<n&&0<=next.y&&next.y<m&&maze[next.x][next.y]!='#'&&!vis[next.x][next.y])
                {
                    if(maze[next.x][next.y]=='x')
                        next.step=cur.step+2;
                    else
                        next.step=cur.step+1;
                    que.push(next);
                    vis[next.x][next.y]=true;
                }
            }
        }
        return -1;
    }
    int main()
    {
        while(cin>>n>>m)
        {
            memset(vis,false,sizeof(vis));
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    {
                        cin>>maze[i][j];
                        if(maze[i][j]=='a')
                        {
                            gx=i;
                            gy=j;
                        }
                        if(maze[i][j]=='r')
                        {
                            sx=i;
                            sy=j;
                        }
                    }
            int ans=bfs();
            if(ans==-1)
                cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
            else
                cout<<ans<<endl;
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    序列化
    输入输出流——字符流部分
    输入输出流——字节流部分
    GitHub10岁之际HanLP自然语言处理包用户量跃居榜首
    hadoop大数据处理平台与案例
    dkhadoop的自然语言处理技术介绍
    hadoop大数据技术架构详解
    hadoop框架结构介绍
    Hadoop集群环境搭建步骤说明
    快速了解掌握中文自然语言处理
  • 原文地址:https://www.cnblogs.com/orion7/p/7299253.html
Copyright © 2011-2022 走看看