zoukankan      html  css  js  c++  java
  • HDOJ1242 Rescue(营救) 搜索

    Rescue

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


    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



    在BFS的搜索过程中,不能一判断出到达目标位置就退出BFS过程,否则求出来的仅仅只是r到a的最小步数。因为BFS所搜索的顶点都是按深度进行搜索的,所以BFS先搜索到的都是步数最少的,不一定是最优解,所用的时间可能更长。一定要等到链表为空,BFS搜索过程全部结束才能得出最优解或者得出无法找到目标位置的结论。
    这一题并没有判断位置是否访问过,但是并不会无限循环下去。因为从某个位置出发判断是否要将它相邻的位置(x,y)入列,条件是这种走法比以前走到(x,y)所用的时间更少;
    如果所用的时间更少,则(x,y)位置会重复入列,但不会无限下去。



     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 using namespace std;
     5 #define MAX 1000000
     6 int Map[250][250];
     7 int T[250][250];
     8 int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
     9 int n,m;
    10 int si,sj,di,dj;
    11 int sign=0;
    12 typedef struct pointer
    13 {
    14     int x,y;
    15     int time;
    16     struct pointer *next;
    17 } LNode,*LinkList;
    18 LinkList ptr;
    19 void bfs(LinkList head);
    20 int main()
    21 {
    22     int i,j;
    23     while(scanf("%d%d",&n,&m)!=EOF)
    24     {
    25         getchar();
    26         for(i=0; i<n; i++)
    27         {
    28             for(j=0; j<m; j++)
    29             {
    30                 scanf("%c",&Map[i][j]);
    31                 T[i][j]=MAX;
    32                 if(Map[i][j]=='a')
    33                 {
    34                     di=i;
    35                     dj=j;
    36                 }
    37                 else if(Map[i][j]=='r')
    38                 {
    39                     si=i;
    40                     sj=j;
    41                     T[si][sj]=0;
    42                 }
    43             }
    44             getchar();
    45         }
    46         LinkList p;
    47         p=(LinkList)malloc(sizeof(LNode));
    48         p->x=si;
    49         p->y=sj;
    50         p->time=0;
    51         p->next=NULL;
    52         sign=0;
    53         bfs(p);
    54         if(T[di][dj]<MAX)cout<<T[di][dj]<<endl;
    55         else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    56     }
    57     return 0;
    58 }
    59 void bfs(LinkList head)
    60 {
    61     int i,fx,fy;
    62     while(head!=NULL)
    63     {
    64         for(i=0; i<4; i++)
    65         {
    66             fx=head->x+dir[i][0];
    67             fy=head->y+dir[i][1];
    68             if(fx>=0&&fx<n&&fy>=0&&fy<m&&Map[fx][fy]!='#')
    69             {
    70                 LinkList p;
    71                 if(sign==0) ptr=head;
    72                 p=(LinkList)malloc(sizeof(LNode));
    73                 p->x=fx;
    74                 p->y=fy;
    75                 p->time=head->time+1;
    76                 if(Map[fx][fy]=='x') p->time++;
    77                 if(p->time<T[fx][fy])
    78                 {
    79                     T[fx][fy]=p->time;
    80                     ptr->next=p;
    81                     p->next=NULL;
    82                     ptr=ptr->next;
    83                     sign=1;
    84                 }
    85             }
    86         }
    87         head=head->next;
    88     }
    89 }
    View Code
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    oracle数据字典(笔记)
    oracle管理表空间和数据文件(笔记)
    oracle权限管理(笔记)
    hibernate获取session的两个方法(笔记)
    hibernate一级缓存(笔记)
    hibernate主要接口和类(笔记)
    hibernate本地事务、全局事务
    hibernate:get和load方法的区别
    位权
    学习使用CGI和HTML
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/4926187.html
Copyright © 2011-2022 走看看