程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDOJ1242 Rescue(營救),hdoj1242rescue

HDOJ1242 Rescue(營救),hdoj1242rescue

編輯:C++入門知識

HDOJ1242 Rescue(營救),hdoj1242rescue


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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved