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

    Rescue

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


    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
     

     

    Recommend
    Eddy   |   We have carefully selected several similar problems for you:  1240 1016 1010 1072 1241 
     ---------------------------------------------------------------------------------------------------------------------------------------------------
    采用bfs搜索算法
    但是采用bfs的时候要注意遇到守卫的情况,应该延后考虑
    如果守卫先进入队列
    那么周围的会被标记为已读
    影响周围更优解法的生成
    所以要延后遇到守卫时的优先级顺序
    下面上我有注释
    而且谁都能看懂的代码
     
    ----------------------------------------------------------------------------------------------------------------------------------------------------
     
      1 //Author:LanceYu
      2 #include<iostream>
      3 #include<string>
      4 #include<cstring>
      5 #include<cstdio>
      6 #include<fstream>
      7 #include<iosfwd>
      8 #include<sstream>
      9 #include<fstream>
     10 #include<cwchar>
     11 #include<iomanip>
     12 #include<ostream>
     13 #include<vector>
     14 #include<cstdlib>
     15 #include<queue>
     16 #include<set>
     17 #include<ctime>
     18 #include<algorithm>
     19 #include<complex>
     20 #include<cmath>
     21 #include<valarray>
     22 #include<bitset>
     23 #include<iterator>
     24 #define ll long long
     25 using namespace std;
     26 const double clf=1e-8;
     27 //const double e=2.718281828;
     28 const double PI=3.141592653589793;
     29 const int MMAX=2147483647;
     30 //priority_queue<int>p;
     31 //priority_queue<int,vector<int>,greater<int> >pq;
     32 int Min,n,m,dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//四个方向 
     33 char map[201][201];
     34 struct node
     35 {
     36     int x,y,step;
     37 };
     38 int bfs(int x,int y,int x1,int y1)
     39 {
     40     Min=MMAX;
     41     int i;
     42     queue<node> q;
     43     q.push(node{x,y,0});
     44     map[x][y]='#';
     45     while(!q.empty())
     46     {
     47         node t=q.front();
     48         q.pop();
     49         if(t.x==x1&&t.y==y1)
     50             return t.step;
     51         if(map[t.x][t.y]=='X')//当发现之前是守卫的点,标记为不可走,步数+1,直接走到下一步 
     52         {
     53             map[t.x][t.y]='#';
     54             t.step++;
     55             q.push(node{t.x,t.y,t.step});
     56             continue;
     57         }
     58         for(i=0;i<4;i++)
     59         {
     60             int dx=t.x+dir[i][0];
     61             int dy=t.y+dir[i][1];
     62             if(dx>=0&&dy>=0&&dx<m&&dy<n&&map[dx][dy]=='a')//如果是终点或者正常路径,则标记为不可读,入列 
     63             {
     64                 map[dx][dy]='#';
     65                 q.push(node{dx,dy,t.step+1});
     66             }
     67             if(dx>=0&&dy>=0&&dx<m&&dy<n&&map[dx][dy]=='.')
     68             {
     69                 map[dx][dy]='#';
     70                 q.push(node{dx,dy,t.step+1});
     71             if(dx>=0&&dy>=0&&dx<m&&dy<n&&map[dx][dy]=='x')//用X作为标记,使其优先级移到队列最后 
     72             {
     73                 map[dx][dy]='X';
     74                 q.push(node{dx,dy,t.step+1});
     75                 
     76             }
     77             
     78             }
     79         }
     80     }
     81     return 0;//找不到返回0 
     82 }
     83 int main()
     84 {
     85     int a,b,a1,b1;
     86     while(scanf("%d%d",&m,&n)!=EOF)
     87     {
     88         for(int i=0;i<m;i++)
     89         {
     90             scanf("%s",map[i]);
     91             for(int j=0;j<n;j++)
     92             {
     93                 if(map[i][j]=='r')//找起点 
     94                 {
     95                     a=i;
     96                     b=j;
     97                 }
     98                 if(map[i][j]=='a')//找终点 
     99                 {
    100                     a1=i;
    101                     b1=j;
    102                 }
    103             }
    104         }
    105         int ans=bfs(a,b,a1,b1);
    106         if(!ans)//如果ans不等于0就输出ans 
    107             printf("Poor ANGEL has to stay in the prison all his life.\n");
    108         else
    109             printf("%d\n",ans);
    110     }
    111     return 0;
    112 }

    2018-11-15  23:49:18  Author:LanceYu

  • 相关阅读:
    Django models中的null和blank的区别
    3次登陆锁定与backend增删改查
    Python全栈考试(一)
    python第一天几个小游戏
    linux开发脚本自动部署及监控
    Linux awk&shell script
    linux正则表达式grep&sed
    linux网路IP.设定主机名.ssh .bash命令&通配符
    Linux:nginx(web服务),nfs服务+反向代理+负载均衡
    Linux内存dd,rpm,yum,软件安装
  • 原文地址:https://www.cnblogs.com/lanceyu/p/9966951.html
Copyright © 2011-2022 走看看