zoukankan      html  css  js  c++  java
  • Rescue

    Rescue

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1235 Accepted Submission(s): 495
     
    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
    ZOJ Monthly, October 2003
     
    Recommend
    Eddy
     
    /*
    优先队列是反的
    */
    #include<bits/stdc++.h>
    #define N 205
    using namespace std;
    char mapn[N][N];
    int n,m,res;
    int visit[N][N];
    int dir[4][2]={1,0,-1,0,0,1,0,-1};
    struct node
    {
        int x,y;
        int val;
        bool operator <(const node &b)const
        {
            return val>b.val;
        }
    };
    int ok(int x,int y)
    {
        if(x<1||x>n||y<1||y>m||visit[x][y]||mapn[x][y]=='#')
            return 1;
        return 0;
    }
    int bfs(node start,int x,int y)
    {
        int i;
        priority_queue<node>fr;
        fr.push(start);
        visit[start.x][start.y]=1;
        //int c=1;
        while(!fr.empty())
        {
            //cout<<c++<<endl;
            start=fr.top();
            fr.pop();
            //cout<<start.x<<" "<<start.y<<endl;
            if(start.x==x&&start.y==y)
                return start.val;
            //cout<<start.val<<endl;
            for(i=0;i<4;i++)
            {
                node v=start;
                v.x+=dir[i][0];
                v.y+=dir[i][1];
                if(ok(v.x,v.y)) continue;
                v.x=v.x;
                v.y=v.y;
                v.val++;
                if(mapn[v.x][v.y]=='x')
                    v.val++;
                fr.push(v);
                visit[v.x][v.y]=1;
            }
        }
        return 0;
    }
    int main()
    {
        // freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
        while(~scanf("%d%d",&n,&m))
        {
            getchar();
            memset(visit,0,sizeof visit);
            node start;
            int ex,ey;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    scanf("%c",&mapn[i][j]);
                    if(mapn[i][j]=='a')
                    {
                        ex=i;
                        ey=j;
                    }
                    if(mapn[i][j]=='r')
                    {
                        start.x=i;
                        start.y=j;
                        start.val=0;
                    }
                }
                scanf("
    ");
            }//输入完成
            //cout<<start.x<<" "<<start.y<<endl;
            //cout<<ex<<" "<<ey<<endl;
            // for(int i=1;i<=n;i++)
            // {
                // for(int j=1;j<=m;j++)
                    // cout<<mapn[i][j];
                // cout<<endl;
            // }
            int cur=0;
            cur=bfs(start,ex,ey);
            if(!cur)
                puts("Poor ANGEL has to stay in the prison all his life.");
            else
                printf("%d
    ",cur);
        }
        return 0;
    }
  • 相关阅读:
    android阅读器开发
    Android开源库
    Android开源项目分类汇总
    java 读取显示txt内容(Swing)
    Java 图形用户界面设计 (Swing)
    Java读取txt文件,换行写txt文件
    出国旅行口语必备300句
    100-days:nine
    100-days: eight
    The Attention Merchants
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5996897.html
Copyright © 2011-2022 走看看