• 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;
    
    }
  • 相关阅读:
    Warning: Cannot modify header information
    curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in
    PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings.
    出现Deprecated: Function ereg_replace() is deprecated in 的原因及解决方法
    APMServ5.2.6 升级PHP版本 到高版本 5.3,5.4
    Apache无法启动解决 the requested operation has failed
    用json获取拉钩网的信息
    json
    如何用组件组装一个简单的机器人
    vue-组件
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2666237.html
Copyright © 2020-2023  润新知