zoukankan      html  css  js  c++  java
  • ZOJ 649 Rescue(优先队列+bfs)

    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

    题意:要求天使的朋友在最短的时间内救出天使,a代表天使,r代表天使的朋友,.代表路,#代表墙,x代表小兵,

    每走一步需要一个时间,遇到小兵杀了他要一个时间,移到小兵的位置有要一个时间;

    输出最短的时间,

    如果救不出来就输出“Poor ANGEL has to stay in the prison all his life.”

    /*
    题解:利用优先队列+bfs
    这题一直错,原因竟然是。。。读入有多组数据,我只读了一组
    */
    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<vector>
    using namespace std;
    int f[202][202];
    char mp[202][202];
    int n,m,sx,sy;
    struct node
    {
        int x,y;
        node(int a,int b){x=a; y=b;}
    };
    int dr[4][2]={{1,0},{0,1},{-1,0},{0,-1} };
    struct cmp
    {
        bool operator()(node a,node b)
        {
            return f[a.x][a.y]>f[b.x][b.y];
        }
    };
    int  bfs()
    {
        priority_queue<node,vector<node>,cmp> Q;
        memset(f,0,sizeof(f));
        Q.push(node(sx,sy));
        f[sx][sy]=1;
        while(!Q.empty())
        {
            node p=Q.top();
            Q.pop();
            for(int i=0;i<4;i++)
            {
                int xx=p.x+dr[i][0];
                int yy=p.y+dr[i][1];
                if (xx>=n || xx<0 || yy>=m || yy<0) continue;
                if (mp[xx][yy]=='#' || f[xx][yy]>0) continue;
                f[xx][yy]=f[p.x][p.y]+1;
                if (mp[xx][yy]=='x')   f[xx][yy]++;
                Q.push(node(xx,yy));
                if(mp[xx][yy]=='r') return f[xx][yy]-1;
            }
        }
        return -1;
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            for(int i=0;i<n;i++)
            {
                scanf("%s",&mp[i]);
                for(int j=0;j<m;j++)
                 if (mp[i][j]=='a') sx=i,sy=j;
            }
            int ans=bfs();
              if (ans==-1) printf("Poor ANGEL has to stay in the prison all his life.
    ");
               else printf("%d
    ",ans);
        }
    
        return 0;
    }
  • 相关阅读:
    SpringBoot集成beetl模板快速入门
    小记---idea springboot 报错没有get或者set方法
    android在style中使用自定义属性 error: style attribute not found.
    ieda使用 在jsp页面中,有时候会出现不能智能显示方法 idea pageContext.setAttribute
    ieda使用 在jsp页面中,有时候会出现不能智能显示方法 idea pageContext.setAttribute
    logback的使用和logback.xml详解,在Spring项目中使用log打印日志
    classpath和classpath*区别
    SpringBoot项目在IntelliJ IDEA中实现热部署
    idea 警告:Warning:java: 源值1.5已过时, 将在未来所有发行版中删除
    linux 删除文件后 df 查看磁盘空间并没有释放
  • 原文地址:https://www.cnblogs.com/stepping/p/5764869.html
Copyright © 2011-2022 走看看