zoukankan      html  css  js  c++  java
  • HDU 1242 Rescue (广搜)

    题目链接

    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

    分析:
    bfs即可,可能有多个’r’(天使的朋友),而’a’(天使)只有一个,从’a’开始搜,找到的第一个’r’即为所求
    需要注意的是这题宽搜时存在障碍物,遇到’x’点是,时间+2,如果用普通的队列就并不能保证每次出队的是时间最小的元素,所以要用优先队列。

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<queue>
    #include<string.h>
    using namespace std;
    int n,m,sx,sy;
    int flg[4][2]= {{-1,0},{1,0},{0,1},{0,-1} };
    char Map[209][209];
    int vis[209][209];
    struct Node
    {
        int x,y;
        int step;
        friend bool operator<(const Node &a,const Node &b)
        {
            return a.step>b.step;
        }
    };
    int bfs(int x,int y)
    {
        Node Now,Next;
        Now.x=x;
        Now.y=y;
        Now.step=0;
        priority_queue<Node> q;
        q.push(Now);
        while(!q.empty())
        {
            Now=q.top();
            q.pop();
            //printf("%d  %d
    ",Now.x,Now.y);
            if(Map[Now.x][Now.y]=='r')
            {
                // printf("@@@@@@@
    ");
                return Now.step;
            }
    
            for(int i=0; i<4; i++)
            {
                Next.x=Now.x+flg[i][0];
                Next.y=Now.y+flg[i][1];
                if(Next.x>=0&&Next.x<n&&Next.y>=0&&Next.y<m&&Map[Next.x][Next.y]!='#'&&vis[Next.x][Next.y]==0)
                {
                    vis[Next.x][Next.y]=1;
                    if(Map[Next.x][Next.y]=='x')
                        Next.step=Now.step+2;
                    else
                        Next.step=Now.step+1;
                    q.push(Next);
                }
            }
        }
        return -1;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            memset(vis,0,sizeof(vis));
            for(int i=0; i<n; i++)
                scanf(" %s",Map[i]);
            int flag=0;
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    if(Map[i][j]=='a')
                    {
                        sx=i;
                        sy=j;
                        flag=1;
                        // printf("%d  %d
    ",sx,sy);
                        break;
                    }
                }
                if(flag==1)
                    break;
            }
            vis[sx][sy]=1;
            int ans=bfs(sx,sy);
            if(ans==-1)
                printf("Poor ANGEL has to stay in the prison all his life.
    ");
            else
                printf("%d
    ",ans);
        }
    }
    
  • 相关阅读:
    机器学习(八) 多项式回归与模型泛化(上)
    机器学习(七) PCA与梯度上升法 (下)
    机器学习(七) PCA与梯度上升法 (上)
    机器学习(六) 梯度下降法 (下)
    机器学习(六) 梯度下降法 (上)
    机器学习(五) 线性回归法 (下)
    机器学习(五) 线性回归法 (上)
    机器学习(四) 机器学习(四) 分类算法--K近邻算法 KNN (下)
    获取字符串中img标签的url集合(转载)
    微信上传文章素材—ASP.NET MVC从View层传数据到Controller层
  • 原文地址:https://www.cnblogs.com/cmmdc/p/7611058.html
Copyright © 2011-2022 走看看