zoukankan      html  css  js  c++  java
  • Rescue

    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

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <string>
      7 #include <vector>
      8 #include <stack>
      9 #include <queue>
     10 #include <set>
     11 #include <map>
     12 #include <iomanip>
     13 using namespace std;
     14 const int INF=0x5fffffff;
     15 const int MS=205;
     16 const double EXP=1e-8;
     17 
     18 int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
     19 int mintime[MS][MS];
     20 char plat[MS][MS];
     21 int N,M;
     22 int sx,sy,ex,ey;
     23 struct node
     24 {
     25     int x,y;
     26     int time;
     27 };
     28 node start;
     29 queue<node> que;
     30 
     31 int input()
     32 {
     33     if(scanf("%d %d",&N,&M)==EOF)
     34         return 0;
     35     for(int i=0;i<N;i++)
     36         scanf("%s",plat[i]);
     37     for(int i=0;i<N;i++)
     38     {
     39         for(int j=0;j<M;j++)
     40         {
     41             mintime[i][j]=INF;
     42             if(plat[i][j]=='a')
     43             {
     44                 ex=i;
     45                 ey=j;
     46             }
     47             else if(plat[i][j]=='r')
     48             {
     49                 sx=i;
     50                 sy=j;
     51             }
     52         }
     53     }
     54     mintime[sx][sy]=0;
     55     start.time=0;
     56     start.x=sx;
     57     start.y=sy;
     58     return 1;
     59 }
     60 
     61 void bfs(node s)
     62 {
     63     que.push(s);
     64     node hd;  //hd===head
     65     while(!que.empty())
     66     {
     67         hd=que.front();
     68         que.pop();
     69         for(int i=0;i<4;i++)
     70         {
     71             int x=hd.x+dir[i][0];
     72             int y=hd.y+dir[i][1];
     73             if(x>=0&&x<N&&y>=0&&y<M&&plat[x][y]!='#')
     74             {
     75                 node t;
     76                 t.x=x;
     77                 t.y=y;
     78                 t.time=hd.time+1;
     79                 if(plat[x][y]=='x')
     80                     t.time++;
     81                 if(t.time<mintime[x][y])
     82                 {
     83                     mintime[x][y]=t.time;
     84                     que.push(t);
     85                 }
     86             }
     87         }
     88     }
     89 }
     90 
     91 int main()
     92 {
     93    while(input())
     94    {
     95         bfs(start);
     96         if(mintime[ex][ey]<INF)
     97             printf("%d
    ",mintime[ex][ey]);
     98         else
     99             printf("Poor ANGEL has to stay in the prison all his life.
    ");
    100    }
    101     return 0;
    102 }
  • 相关阅读:
    一个可以拖拽的div
    网页设计与制作常见问题
    如何写出兼容性很好的页面
    图片4像素底边
    响应式布局简明示例
    CSS 实现背景图尺寸不随浏览器缩放而变化
    bootstrap实现pc屏幕五等分
    css中的px、em、rem 详解
    HTML 5的革新——语义化标签(一)
    jQuery实现TAB选项卡切换特效简单演示
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4301983.html
Copyright © 2011-2022 走看看