zoukankan      html  css  js  c++  java
  • HDU 1242 Rescue 营救天使

    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
     
      1 #include<cstdio>
      2 #include<string.h>
      3 #include<queue>
      4 using namespace std;
      5 int m,n,i,j,ans,map[205][205],ex,ey,bx,by;
      6 int dx[4]={-1,1,0,0};
      7 int dy[4]={0,0,-1,1};
      8 char str[205];
      9 struct stu
     10 {
     11     int x,y,step;
     12     bool friend operator <(stu a,stu b)
     13     {
     14         return a.step>b.step;
     15     }
     16 }st;
     17 int bfs()
     18 {
     19     priority_queue<stu>que;
     20     int x,y,i,time;
     21     stu next;
     22     
     23     st.x=bx;
     24     st.y=by;
     25     st.step=0;
     26     que.push(st);
     27     while(!que.empty())
     28     {
     29         
     30         st=que.top();
     31         que.pop();
     32         time=st.step;
     33         x=st.x;
     34         y=st.y;
     35         for(i=0;i<4;i++)
     36         {
     37             st.x=x+dx[i];
     38             st.y=y+dy[i];
     39             if(st.x>=0 &&st.y>=0&&st.x<m&&st.y<m&&map[st.x][st.y]!=0)
     40             {
     41                 if(st.x == ex && st.y == ey)
     42                 {
     43                     return time+1;
     44                 }
     45                 if(map[st.x][st.y] == 2)
     46                 {
     47                     st.step=time+2;
     48                 }
     49                 else 
     50                 {
     51                     st.step=time+1;
     52                 }
     53                 map[st.x][st.y]=0;
     54                 que.push(st);
     55             }
     56         }
     57     }
     58     return 0;
     59 }
     60 int main()
     61 {
     62     while(scanf("%d %d",&m,&n)!=EOF)
     63     {
     64         memset(map,0,sizeof(map));
     65         for(i = 0 ; i < m ; i++)
     66         {
     67             scanf("%s",&str);
     68             for(j = 0 ;j < n ; j++)
     69             {
     70                 if(str[j] == '.')
     71                 {
     72                     map[i][j]=1;
     73                 }
     74                 if(str[j] == 'x')
     75                 {
     76                     map[i][j]=2;
     77                 }
     78                 if(str[j] == 'a')
     79                 {
     80                     map[i][j]=1;
     81                     ex=i;
     82                     ey=j;
     83                 }
     84                 if(str[j] == 'r')
     85                 {
     86                     map[i][j]=1;
     87                     bx=i;
     88                     by=j;
     89                 }
     90             }
     91         }    
     92     /*    for( i = 0 ; i < m ; i++)
     93          {
     94             for(j =0 ; j < n ; j++)
     95              {
     96                 printf("%d  ",map[i][j]);
     97                  if(j == n-1)
     98                  {
     99                      printf("
    ");
    100                  }
    101             }
    102          }            */
    103         map[bx][by]=0;
    104         ans=bfs();
    105         if(ans)
    106             printf("%d
    ",ans);
    107         else
    108             printf("Poor ANGEL has to stay in the prison all his life.
    ");
    109         
    110     }
    111 }
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    hdu2243 考研路茫茫——单词情结【AC自动机】【矩阵快速幂】
    poj3376 Finding Palindromes【exKMP】【Trie】
    hdu4763 Theme Section【next数组应用】
    hdu2609 How many【最小表示法】【Hash】
    hdu3374 String Problem【最小表示法】【exKMP】
    poj2728 Desert King【最优比率生成树】【Prim】【0/1分数规划】
    python装饰器
    python面试题
    salt教程1-理解saltstack
    redis慢查询日志
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5719387.html
Copyright © 2011-2022 走看看