zoukankan      html  css  js  c++  java
  • HDU 1242 Rescue

    Rescue

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

    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
     
    Author
    CHEN, Xue
     
    Source
     
    Recommend
    Eddy
     
    思路:优先队列就可以解决
    优先级别的定义如下
    struct Node
    {

        int
    x,y;
        int
    time;
    friend
    bool operator < (const Node &a,const Node  &b)
    {

        return
    a.time > b.time;
        }
    };

     
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <queue>
    #include <cstring>
    using namespace std;
    char
    map[210][210];
    int
    hash[210][210];
    int
    bfs[4][2] = {1,0,-1,0,0,1,0,-1};
    int
    n,m;
    int
    ax,ay,rx,ry;
    struct
    Node
    {

        int
    x,y;
        int
    time;
    friend
    bool operator < (const Node &a,const Node  &b)
    {

        return
    a.time > b.time;
        }
    };

    int
    BFS()
    {

        int
    max = 0;
        priority_queue <Node> q;
        Node top;
        top.x = ax;top.y = ay;top.time = 0;
        q.push(top);
        while
    (!q.empty())
        {

            Node temp;
            temp = q.top();
            //printf("%d %d have %d ",temp.x,temp.y,temp.time);
            q.pop();
            if
    (map[temp.x][temp.y] == 'r')
            {

                   max = temp.time;
                   break
    ;
            }

            for
    (int k = 0;k < 4;k ++)
            {

                int
    x = temp.x + bfs[k][0],y = temp.y + bfs[k][1];
                int
    time = temp.time;
                if
    (map[x][y] != '#' && hash[x][y] != 1 &&
                x >= 1 && x <= n && y >= 1 && y <= m)
                {

                    hash[x][y] = 1;
                    Node xin;
                    xin.x = x;xin.y = y;
                    if
    (map[x][y] == 'x')
                       xin.time = time + 2;
                    else

                       xin.time = time + 1;
                    q.push(xin);
                }
            }
        }

        return
    max;
    }

    int
    main()
    {

        while
    (~scanf("%d%d",&n,&m))
        {

         memset(hash,0,sizeof(hash));
         for
    (int i = 1;i <= n;i ++)
           for
    (int j = 1;j <= m;j ++)
           {

              scanf(" %c",&map[i][j]);
              if
    (map[i][j] == 'a')
              {

                    ax = i;ay = j;hash[i][j] = 1;
              }
            }

        int
    sss = BFS();
        if
    (sss == 0)
            printf("Poor ANGEL has to stay in the prison all his life. ");
        else

            printf("%d ",sss);
        }

        return
    0;
    }
  • 相关阅读:
    springboot 启动报错"No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available"
    使用SpringCloud Stream结合rabbitMQ实现消息消费失败重发机制
    Java 继承
    Java 抽象类 抽象方法 使用说明
    java 构造器(构造方法)使用详细说明
    Java enum枚举在实际项目中的常用方法
    万字长文!一次性弄懂 Nginx 处理 HTTP 请求的 11 个阶段
    Nginx 如何自定义变量?
    听说你的资源被盗用了,那你知道 Nginx 怎么防盗链吗?
    Nginx 的变量究竟是怎么一回事?
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3283472.html
Copyright © 2011-2022 走看看