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

    Rescue

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


    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
    /*
    广搜
    检查了很久 最后 被困死的时候是 0,
    if(visit[i][j]<num && visit[i][j]!=0)
    考虑了一些情况,题意很清晰,有多个r。一个a么?应该是。但是我没有处理。
    由于x的存在使得 到达各点的时间可能存在多样,也加进去比较了。
    但是之前写的dfs,没有考虑过这样的情况。
    */
    #include<stdio.h>
    #include<stdlib.h>
    #define HH 11111111
    char a[202][205];
    int map[4][2]={{1,0},{0,1},{0,-1},{-1,0}};
    int zhan[50000],len;
    int visit[202][202];
    int n,m;
    void bfs(int x,int y)
    {
        int i,x1,y1;
        zhan[++len]=x;
        zhan[++len]=y;
        visit[x][y]=0;
        while(len>0)
        {
            y=zhan[len--];
            x=zhan[len--];
            for(i=0;i<4;i++)
            {
                x1=x+map[i][0];
                y1=y+map[i][1];
                if(x1>=1&&x1<=n && y1>=1&&y1<=m)
                {
                    if(visit[x1][y1]==0 && a[x1][y1]!='#')
                    {
                        if(a[x1][y1]=='.'||a[x1][y1]=='r')
                            visit[x1][y1]=visit[x][y]+1;
                        else if(a[x1][y1]=='x')
                            visit[x1][y1]=visit[x][y]+2;
                        zhan[++len]=x1;
                        zhan[++len]=y1;
                    }
                    if(visit[x1][y1]>0 && a[x1][y1]!='#')
                    {
                        if((a[x1][y1]=='.'||a[x1][y1]=='r')&&visit[x1][y1]>visit[x][y]+1)
                        {
                            visit[x1][y1]=visit[x][y]+1;
                            zhan[++len]=x1;
                            zhan[++len]=y1;
                        }
                        if(a[x1][y1]=='x' && visit[x1][y1]>visit[x][y]+2)
                        {
                            visit[x1][y1]=visit[x][y]+2;
                            zhan[++len]=x1;
                            zhan[++len]=y1;
                        }
                    }
                }
            }
        }
    }
    int main()
    {
        int i,j,num;
        while(scanf("%d%d",&n,&m)>0)
        {
            for(i=1;i<=n;i++)
                scanf("%s",a[i]+1);
            for(i=1;i<=n;i++)
                for(j=1;j<=m;j++)
                    visit[i][j]=0;
            for(i=1;i<=n;i++)
                for(j=1;j<=m;j++)
                {
                    if(a[i][j]=='a')
                    {
                        len=0;
                        bfs(i,j);            
                    }
                }
            num=HH;
            for(i=1;i<=n;i++)
                for(j=1;j<=m;j++)
                    if(a[i][j]=='r')
                    {
                        if(visit[i][j]<num && visit[i][j]!=0)
                            num=visit[i][j];
                    }
            if(num==HH) printf("Poor ANGEL has to stay in the prison all his life.
    ");
            else printf("%d
    ",num);
        }
        return 0;
    }

    单纯的广搜,在浙大oj超时.... 蒋神却过了,思想很厉害。

    /*
    优先队列
    */
    
    #include<stdio.h>
    #include<iostream>
    #include<cstdlib>
    #include<string.h>
    #include<queue>
    #define HH 11111111
    using namespace std;
    char a[202][202];
    int visit[202][202];
    int n,m;
    int map[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
    struct node
    {
        friend bool operator< (node n1,node n2)
        {
            return n1.p>n2.p;
        }
        int p;
        int x;
        int y;
    };
    void bfs(int x,int y)
    {
        int i,x1,y1;
        priority_queue<node>b;
        while(!b.empty())
        {
            b.pop();
        }
        node tmp,tmp1;
        tmp.x=x;
        tmp.y=y;
        tmp.p=0;
        b.push(tmp);
        visit[x][y]=1;
        while(b.size()>0)
        {
            tmp=b.top();
            b.pop();
            for(i=0;i<4;i++)
            {
                x1=tmp.x+map[i][0];
                y1=tmp.y+map[i][1];
                if(x1>=1&&x1<=n && y1>=1&&y1<=m && visit[x1][y1]==0 && a[x1][y1]!='#')
                {
                    if(a[x1][y1]=='x')
                        visit[x1][y1]=tmp.p+2;
                    else if(a[x1][y1]=='.' || a[x1][y1]=='r')
                        visit[x1][y1]=tmp.p+1;
                    tmp1=tmp;
                    tmp.x=x1;
                    tmp.y=y1;
                    tmp.p=visit[x1][y1];
                    b.push(tmp);
                    tmp=tmp1;
                    if(a[x1][y1]=='r')return;
                }
            }
        }
    }
    int main()
    {
        int i,j,num;
        while(scanf("%d%d",&n,&m)>0)
        {
            for(i=1;i<=n;i++)
                scanf("%s",a[i]+1);
            memset(visit,0,sizeof(visit));
            for(i=1;i<=n;i++)
                for(j=1;j<=m;j++)
                {
                    if(a[i][j]=='a')
                    {
                        bfs(i,j);
                    }
                }
            num=HH;
            for(i=1;i<=n;i++)
                for(j=1;j<=m;j++)
                {
                    if(a[i][j]=='r' && visit[i][j]!=0 && visit[i][j]<num)
                        num=visit[i][j];
                }
            if(num==HH)
                printf("Poor ANGEL has to stay in the prison all his life.
    ");
            else
                printf("%d
    ",num);
        }
        return 0;
    }
  • 相关阅读:
    为什么基于TCP的应用需要心跳包(TCP keep-alive原理分析)
    「DDoS攻击」兴风作浪,教你如何有效防护!
    你还敢乱粘贴吗?
    TODO git如何去掉烦人的merge?
    Git修改已经push到远程的commit信息
    Oracle删除唯一索引失败提示ORA-01418:指定的索引不存在 ORACLE
    mybatis逆向生成代码 [ERROR] No plugin found for prefix 'mybatis-generator' in the current project and in the plugin groups
    MySQL 中 redo log、undo log、binlog 的总结
    VATT: Transformers for Multimodal Self-Supervised Learning from Raw Video, Audio and Text
    OPT: Omni-Perception Pre-Trainer for Cross-Modal Understanding and Generation
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3193587.html
Copyright © 2011-2022 走看看