zoukankan      html  css  js  c++  java
  • hdu 1242(BFS+优先队列)

                                             Rescue

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

    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
    题意:一个公主被抓  公主有很多个朋友  在图上  a代表公主  r 代表公主的朋友 #代表墙  .代表路   x代表守卫
    打败守卫花费2秒  其它一秒   现在问你那个朋友能最快解救公主
    因为朋友不止一个  我们可以BFS从公主开始搜 由于打败守卫花的时间长  我们选择优先队列  每次去最小 
    这题目数据很水  错的代码都能过  dfs应该也能过
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<map>
     8 #include<set>
     9 #include<vector>
    10 #include<cstdlib>
    11 #include<string>
    12 #define eps 0.000000001
    13 typedef long long ll;
    14 typedef unsigned long long LL;
    15 using namespace std;
    16 const int N=1000+10;
    17 int vis[N][N];
    18 int n,m;
    19 char mp[N][N];
    20 int _x[4]={0,1,-1,0};
    21 int _y[4]={1,0,0,-1};
    22 struct node{
    23     int x,y;
    24     int time;
    25     friend bool operator < (node a,node b){
    26         return a.time>b.time;
    27     }
    28 }a,b;
    29 priority_queue<node>q;
    30 int judge(int x,int y){
    31     if(x<0||x>=n||y<0||y>=m)return 0;
    32     if(vis[x][y]==1)return 0;
    33     if(mp[x][y]=='#')return 0;
    34     return 1;
    35 }
    36 int BFS(int x,int y){
    37     a.time=0;
    38     a.x=x;a.y=y;
    39     q.push(a);
    40     vis[x][y]=1;
    41     while(!q.empty()){
    42         b=q.top();
    43         q.pop();
    44         for(int i=0;i<4;i++){
    45             int xx=b.x+_x[i];
    46             int yy=b.y+_y[i];
    47             if(judge(xx,yy)){
    48                 vis[xx][yy]=1;
    49                 if(mp[xx][yy]=='x')a.time=b.time+2;
    50                 else if(mp[xx][yy]=='r')return (b.time+1);
    51                 else
    52                     a.time=b.time+1;
    53                 a.x=xx;
    54                 a.y=yy;
    55                 q.push(a);
    56             }
    57         }
    58     }
    59     return -1;
    60 }
    61 int main(){
    62     while(scanf("%d%d",&n,&m)!=EOF){
    63         while(!q.empty())q.pop();
    64         memset(vis,0,sizeof(vis));
    65         int x,y;
    66         for(int i=0;i<n;i++)
    67         for(int j=0;j<m;j++){
    68             cin>>mp[i][j];
    69             if(mp[i][j]=='a'){
    70                 x=i;y=j;
    71                 //a.x=x;a.y=y;a.time=0;
    72             }
    73         }
    74         int ans=BFS(x,y);
    75         if(ans==-1)
    76             printf("Poor ANGEL has to stay in the prison all his life.
    " );
    77         else
    78             printf("%d
    ",ans);
    79     }
    80 }
  • 相关阅读:
    饮冰三年-人工智能-linux-03 Linux文件管理(权限管理+归档+压缩)
    饮冰三年-人工智能-linux-02 初始Linux
    饮冰三年-人工智能-linux-01通过VM虚拟机安装contes系统
    测试joinablequeue
    生产者消费者模型
    队列
    优化抢票
    昨日回顾
    抢票小程序
    守护进程
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6592029.html
Copyright © 2011-2022 走看看