• hdu 1242(搜索)


    Rescue

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


    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
     

    走卫兵守护的路花费的时间多1,考虑优先队列

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<math.h>
    #include<queue>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    
    char graph[205][205];
    bool vis[205][205];
    struct Node
    {
        int x,y;
        int step;
    };
    Node s,t;
    bool operator < (Node a,Node b)
    {
        return a.step>b.step;
    }
    int n,m;
    int dir[][2] = {{-1,0},{1,0},{0,-1},{0,1}};
    bool check(int x,int y)
    {
        if(x<0||x>=n||y<0||y>=m||graph[x][y]=='#'||vis[x][y]==true) return false;
        return true;
    }
    int bfs()
    {
        memset(vis,false,sizeof(vis));
        priority_queue<Node> q;
        q.push(s);
        vis[s.x][s.y]=true;
        s.step = 0;
        while(!q.empty())
        {
            Node now = q.top();
            q.pop();
            if(now.x==t.x&&now.y==t.y)
            {
                return now.step;
            }
            Node next;
            for(int i=0; i<4; i++)
            {
                next.x = now.x+dir[i][0];
                next.y = now.y+dir[i][1];
                if(!check(next.x,next.y)) continue;
                if(graph[next.x][next.y]=='x')
                {
                    next.step=now.step+2;
                    q.push(next);
                    vis[next.x][next.y]=1;
                }
                else if(graph[next.x][next.y]=='.')
                {
                    next.step=now.step+1;
                    q.push(next);
                    vis[next.x][next.y]=1;
                }
            }
        }
        return -1;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=0; i<n; i++)
            {
                scanf("%s",graph[i]);
                for(int j=0; j<m; j++)
                {
                    if(graph[i][j]=='r')
                    {
                        s.x=i,s.y=j;
                        graph[i][j]='.';
                    }
                    if(graph[i][j]=='a')
                    {
                        t.x=i,t.y=j;
                        graph[i][j]='.';
                    }
                }
    
            }
            int res = bfs();
            if(res==-1)
            {
                printf("Poor ANGEL has to stay in the prison all his life.
    ");
            }
            else printf("%d
    ",res);
        }
        return 0;
    }
  • 相关阅读:
    小米6刷机开启root权限
    C标准库堆内存函数
    UE4打印到HUD的Stat命令
    Blueprint Stats插件
    PC与Mobile硬件架构对比
    atomic原子编程中的Memory Order
    Windows10下开启D3D11的Debug Layer
    【数据结构】树存储结构
    【数据结构】哈希表
    【算法】查找算法
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5573719.html
Copyright © 2020-2023  润新知