zoukankan      html  css  js  c++  java
  • hdu Rescue

    Rescue

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 521    Accepted Submission(s): 217
     
    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
     

    分析:优先队列实现的bfs

    #include<iostream>
    #include<string>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    typedef struct S
    {
        int x,y,time;
        bool operator < (const S &a)const
        {return time>a.time;}
    }STEP;
    
    STEP now,next;
    char map[210][210];
    int t[210][210];
    int vis[210][210];
    int dx[]={0,-1,0,1};
    int dy[]={1,0,-1,0};
    int main()
    {
        int i,j,m,n,a,b,c,d,xx,yy;
        while(scanf("%d%d",&m,&n)!=EOF)
        {
            priority_queue<STEP> q;
            memset(vis,0,sizeof(vis));
            scanf("%*c");
            for(i=0;i<m;++i)
            {
                scanf("%s",map[i]);
                for(j=0;j<n;++j)
                {
                    if(map[i][j]=='a')
                    {
                        a=i;
                        b=j;
                        t[i][j]=0;
                    }
                    if(map[i][j]=='r')
                    {
                        c=i;
                        d=j;
                        t[i][j]=1;
                    }
                    if(map[i][j]=='.')
                        t[i][j]=1;
                    if(map[i][j]=='x')
                        t[i][j]=2;
                }
            }
            now.x=a;
            now.y=b;
            now.time=0;
            q.push(now);
            vis[a][b]=1;
            while(!q.empty())
            {
                now=q.top();
                q.pop();
                if(now.x==c && now.y==d)
                {
                    printf("%d\n",now.time);
                    goto L;
                }
                for(i=0;i<4;++i)
                {
                    xx=now.x+dx[i];
                    yy=now.y+dy[i];
                    if(xx==c && yy==d)
                    {
                        printf("%d\n",now.time+t[xx][yy]);
                        goto L;
                    }
                    if(!vis[xx][yy] && map[xx][yy]!='#' && xx>=0 && xx<m && yy>=0 && yy<n)
                    {
                        vis[xx][yy]=1;
                        next.x=xx;
                        next.y=yy;
                        next.time=now.time+t[xx][yy];
                        q.push(next);
                    }
                }
            }
            printf("Poor ANGEL has to stay in the prison all his life.\n");
            L:;
    
    
        }
        return 0;
    
    }
  • 相关阅读:
    BZOJ2784: [JLOI2012]时间流逝
    JZOJ 5190. 景中人 (找性质+dp+记忆化实现)
    [NOI2013] 向量内积(随机好题)
    【NOI2014】购票(斜率优化dp+树链剖分)
    「NOI2016」国王饮水记(猜结论+动态规划+斜率优化)
    「NOI2016」网格(猜结论+tarjan找桥点+乱搞)
    Codeforces 1270 H. Number of Components(找性质+线段树)
    Codeforces 1276D. Tree Elimination(树形dp)
    Codeforces 1158 F. Density of subarrays(找性质+分段dp)
    Codeforces 960G Bandit Blues (快速求第一类斯特林数)
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2666237.html
Copyright © 2011-2022 走看看