zoukankan      html  css  js  c++  java
  • G

    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
    /*救人,
    你在a,要救的人在r,
    '.'是路可以走,'#'是墙,'x'是警卫,走路花费一秒,遇见警卫花费两秒。
    问最小多少秒能救人,
    能的话输出步数,不能的话输出"Poor ANGEL has to stay in the prison all his life."*/
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<stack>
    #include<bitset>
    #include<cstdlib>
    #include<cmath>
    #include<set>
    #include<list>
    #include<deque>
    #include<map>
    #include<queue>
    #define ll long long
    #define inf 0x3fffffff
    using namespace std;
    
    const int maxn = 205;
    
    int n,m,xxx,yyy;
    char a[maxn][maxn];
    int vis[maxn][maxn];
    int dir[][2]={ {1,0},{0,1},{-1,0},{0,-1} };
    
    
    struct Node
    {
        int xx,yy,time;  //横纵坐标+时间
       friend bool operator < (Node a,Node b)
        {
            return a.time > b.time;
        }
    };
    
    int check(int x,int y)
    {
        if(x<0||x>=n||y<0||y>=m||a[x][y]=='#'||vis[x][y])
            return 0;
        return 1;
    }
    /*int check(int x,int y)
    {
        if(x<0 || y<0 || x>=n || y>=m || !vis[x][y] || a[x][y] == '#')
            return 1;
        return 0;
    }*/
    
    int bfs(int x,int y)
    {
        memset(vis,0,sizeof(vis));//清空标记数组放到bfs里面!!!!!
        priority_queue<Node> q;
        while(!q.empty()) q.pop();
        q.push(Node{x,y,0});
        vis[x][y]=1;
    
        while(!q.empty())
        {
            Node u=q.top();
            q.pop();
            Node tmp;
            if(a[u.xx][u.yy]=='r')
                return u.time;
            for(int i=0;i<4;i++)
            {
                tmp.xx=u.xx+dir[i][0];
                tmp.yy=u.yy+dir[i][1];
    
                if( check(tmp.xx,tmp.yy) )
                {
                    vis[tmp.xx][tmp.yy]=1;
                    if(a[tmp.xx][tmp.yy]=='x')
                    tmp.time=u.time+2;
                    else
                    tmp.time=u.time+1;
                    q.push(tmp);
                }
            }
        }
        return -1;
    }
    
    
    int main()
    {
        memset(vis,0,sizeof(vis));
        while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            scanf("%s",&a[i]);
        }
    
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(a[i][j]=='a')
                {
                    xxx=i;
                    yyy=j;
                    break;
                }
            }
        }
    
        int ans=bfs(xxx,yyy);
        if(ans==-1)
             printf("Poor ANGEL has to stay in the prison all his life.
    ");
        else
             printf("%d
    ",ans);
    }
    
    }
    View Code
  • 相关阅读:
    Linux运维常用的几个命令介绍【转】
    Linux 删除文件后空间不释放【原创】
    使用 Xtrabackup 在线对MySQL做主从复制【转】
    用Centos7搭建小微企业Samba文件共享服务器【转】
    工作流数据表设计
    mysql函数大全
    git 分支管理
    Bootstap datetimepicker报错TypeError: intermediate value
    分分钟搞定IOS远程消息推送
    Windows10下安装OpenSSL
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7407102.html
Copyright © 2011-2022 走看看