HDOJ1242 Rescue (Rescue)

Source: Internet
Author: User

Rescue

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 22286 Accepted Submission (s): 7919


Problem Descriptionangel was caught by the moligpy! He was put into prison by Moligpy. The prison is described as a n * M (n, M <=) matrix. There is WALLs, ROADs, and guards in the prison.

Angel ' s friends want to save Angel. Their task Is:approach Angel. We assume that "approach Angel" are to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or his?) 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 had 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.)

InputFirst line contains-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 the Angel ' s friend.

Process to the end of the file.

OutputFor 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 have to stay in the prison all he life."

Sample Input7 8#.#####.#.a#. r.#. #x ..... #.##...##...#..............

Sample Output13 In the search process of BFS, it is not possible to exit the BFS process by judging the target location, otherwise only the minimum number of steps R to a is obtained. Since the search vertices of bfs are searched by depth, BFS searches for the least number of steps, not necessarily the optimal solution, and may take longer. Be sure to wait until the list is empty, the BFS search process is complete to obtain the optimal solution or to find the target location can not be reached. This question does not determine whether the location has been accessed, but it does not loop indefinitely. Since it is determined from a certain location (x, y) to into row, the condition is that it takes less time to go than before (x, y), and if it takes less time, the (x, y) position repeats into row, but not indefinitely.
1#include <iostream>2#include <cstdio>3#include <cstdlib>4 using namespacestd;5 #defineMAX 10000006 intmap[ -][ -];7 intt[ -][ -];8 intdir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};9 intn,m;Ten intSi,sj,di,dj; One intsign=0; AtypedefstructPointer - { -     intx, y; the     intTime ; -     structPointer *Next; -} lnode,*linklist; - linklist ptr; + voidBFS (linklist head); - intMain () + { A     inti,j; at      while(SCANF ("%d%d", &n,&m)! =EOF) -     { - GetChar (); -          for(i=0; i<n; i++) -         { -              for(j=0; j<m; J + +) in             { -scanf"%c",&map[i][j]); tot[i][j]=MAX; +                 if(map[i][j]=='a') -                 { theDi=i; *dj=J; $                 }Panax Notoginseng                 Else if(map[i][j]=='R') -                 { theSi=i; +sj=J; At[si][sj]=0; the                 } +             } - GetChar (); $         } $ linklist p; -p= (linklist)malloc(sizeof(Lnode)); -p->x=si; thep->y=SJ; -P->time=0;Wuyip->next=NULL; thesign=0; - BFS (p); Wu         if(T[di][dj]<max) cout<<t[di][dj]<<Endl; -         Elsecout<<"Poor ANGEL have to stay in the prison all he life."<<Endl; About     } $     return 0; - } - voidBFS (linklist head) - { A     intI,fx,fy; +      while(head!=NULL) the     { -          for(i=0; i<4; i++) $         { thefx=head->x+dir[i][0]; thefy=head->y+dir[i][1]; the             if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]!='#') the             { - linklist p; in                 if(sign==0) ptr=head; thep= (linklist)malloc(sizeof(Lnode)); thep->x=FX; Aboutp->y=fy; thep->time=head->time+1; the                 if(map[fx][fy]=='x') p->time++; the                 if(p->time<T[fx][fy]) +                 { -T[fx][fy]=p->Time ; theptr->next=p;Bayip->next=NULL; thePtr=ptr->Next; thesign=1; -                 } -             } the         } theHead=head->Next; the     } the}
View Code

HDOJ1242 Rescue (Rescue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.