• ZOJ


    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<stdio.h>
      2 #include<stdlib.h>
      3 #include<string.h>
      4 #include<math.h>
      5 #include<algorithm>
      6 #include<queue>
      7 #include<stack>
      8 #include<deque>
      9 #include<iostream>
     10 using namespace std;
     11 typedef long long  LL;
     12 const double pi=acos(-1.0);
     13 const double e=exp(1);
     14 
     15 struct con{
     16     int x;
     17     int y;
     18 };
     19 int Map[210][210];
     20 int n,m,t;
     21 int check[50000];
     22 int times[210][210];
     23 int way[4][2]={0,1,0,-1,1,0,-1,0};
     24 
     25 void BFS(int a,int b)
     26 {
     27     int i,p,j,min1=99999999;
     28     queue<struct con> qq;
     29     struct con mid,head;
     30     head.x=a;
     31     head.y=b;
     32    // check[Map[head.x][head.y]]=1;
     33     qq.push(head);
     34     while(!qq.empty())
     35     {
     36         head=qq.front();
     37         qq.pop();
     38         if(Map[head.x][head.y]==-1)
     39         {
     40             if(times[head.x][head.y]<min1)
     41                 min1=times[head.x][head.y];
     42         }
     43         for(i=0;i<=3;i++)
     44         {
     45             mid.x=head.x+way[i][0];
     46             mid.y=head.y+way[i][1];
     47 
     48             if(mid.x>=0&&mid.y>=0&&mid.x<n&&mid.y<m)
     49             {
     50                 if(Map[mid.x][mid.y]==0&&times[head.x][head.y]+2<times[mid.x][mid.y])
     51                 {
     52                     times[mid.x][mid.y]=times[head.x][head.y]+2;
     53                     qq.push(mid);
     54                 }
     55                 else if(Map[mid.x][mid.y]==-2)
     56                     ;
     57                 else if(Map[mid.x][mid.y]!=0&&times[head.x][head.y]+1<times[mid.x][mid.y])
     58                 {
     59                     times[mid.x][mid.y]=times[head.x][head.y]+1;
     60                     qq.push(mid);
     61                 }
     62             }
     63         }
     64     }
     65     if(min1==99999999)
     66         printf("Poor ANGEL has to stay in the prison all his life.
    ");
     67     else
     68         printf("%d
    ",min1);
     69     return ;
     70 }
     71 int main()
     72 {
     73     int i,p,j;
     74     char c;
     75     int a,b;
     76     while(scanf("%d%d",&n,&m)!=EOF)
     77     {
     78         p=1;
     79         memset(check,0,sizeof(check));
     80         memset(times,0x3f3f3f3f,sizeof(times));
     81         for(i=0;i<n;i++)
     82             for(j=0;j<m;j++)
     83             {
     84                 scanf(" %c",&c);
     85                 if(c=='r')
     86                 {
     87                     a=i;
     88                     b=j;
     89                     Map[i][j]=p++;
     90                     times[i][j]=0;
     91                 }
     92                 else if(c=='a')
     93                     Map[i][j]=-1;
     94                 else if(c=='#')
     95                     Map[i][j]=-2;
     96                 else if(c=='x')
     97                     Map[i][j]=0;
     98                 else if(c=='.')
     99                     Map[i][j]=p++;
    100             }
    101 
    102         BFS(a,b);
    103     }
    104     return 0;
    105 }
    View Code
  • 相关阅读:
    我谈编程语言竞争
    从基础学起----xuld版高手成长手记[1]
    自己动手开发语言.笔记@2014-1-13
    删除 QQ 最新版右键菜单 通过QQ发送文件到手机
    客观评价C#的优点和缺点
    一个会做你就是高手的问题
    计划开发的语言及一些细节求吐槽
    面向接口设计思想
    计划添加的复杂语法
    面向对象中的设计陷阱
  • 原文地址:https://www.cnblogs.com/daybreaking/p/9381291.html
Copyright © 2020-2023  润新知