【搜索】POJ1242:Rescue
阿新 • • 發佈:2018-02-13
color pac cal ces str integer 解決 left rds
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.)
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.
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廣搜 + 優先隊列。
題意:從前有一名天使被囚禁了,天使的朋友要去救他,你的任務是輸出最短的救援時間。天使的朋友每秒可以走一步,地牢中有守衛,當你遇到守衛的時候需要停留一秒殺死守衛。給你地牢的地圖,上面有幾種元素,‘.‘表示路,可以通行。‘#‘代表墻,無法通行。‘r‘表示天使的朋友,代表起點。‘a‘表示天使的位置,代表終點。‘x‘表示守衛的位置。
思路:因為是求“最短路徑”的問題,正好用廣搜可以解決。但是與普通廣搜不同的是,遇到守衛的時候會多停留一秒。這就會導致隊列中優先級的不穩定,所以需要用優先隊列,讓優先級最高的節點始終在隊列最前面。
代碼:
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 char mapp[205][205]; 7 bool vis[205][205]; 8 int dx[4] = {0,1,0,-1}; 9 int dy[4] = {1,0,-1,0}; 10 int n,m; 11 int sx,sy,ex,ey; 12 struct node 13 { 14 int x,y,step; 15 friend bool operator < (node a,node b) 16 { 17 return a.step>b.step; 18 } 19 }; 20 bool check(int x,int y) 21 { 22 if(x<1||x>n||y<1||y>m) 23 return true; 24 if(vis[x][y]) 25 return true; 26 if(mapp[x][y]==‘#‘) 27 return true; 28 return false; 29 } 30 int bfs() 31 { 32 memset(vis,false,sizeof(vis)); 33 priority_queue<node> q; 34 node now,nextt; 35 now.x=sx; 36 now.y=sy; 37 now.step=0; 38 vis[now.x][now.y]=true; 39 while(!q.empty()) 40 { 41 q.pop(); 42 } 43 q.push(now); 44 while(!q.empty()) 45 { 46 now=q.top(); 47 q.pop(); 48 if(now.x==ex&&now.y==ey) 49 { 50 return now.step; 51 } 52 for(int i=0;i<4;i++) 53 { 54 int xx=now.x+dx[i]; 55 int yy=now.y+dy[i]; 56 if(check(xx,yy)) 57 { 58 continue; 59 } 60 nextt.x=xx; 61 nextt.y=yy; 62 if(mapp[xx][yy]==‘x‘) 63 { 64 nextt.step=now.step+2; 65 } 66 else 67 { 68 nextt.step=now.step+1; 69 } 70 vis[xx][yy]=1; 71 q.push(nextt); 72 } 73 } 74 return -1; 75 } 76 int main() 77 { 78 while(scanf("%d%d",&n,&m)!=EOF) 79 { 80 getchar(); 81 for(int i=1;i<=n;i++) 82 { 83 for(int j=1;j<=m;j++) 84 { 85 scanf(" %c",&mapp[i][j]); 86 if(mapp[i][j]==‘a‘) 87 { 88 ex=i; 89 ey=j; 90 } 91 else if(mapp[i][j]==‘r‘) 92 { 93 sx=i; 94 sy=j; 95 } 96 } 97 } 98 int step=bfs(); 99 if(step==-1) 100 { 101 printf("Poor ANGEL has to stay in the prison all his life.\n"); 102 } 103 else 104 { 105 printf("%d\n",step); 106 } 107 } 108 return 0; 109 }View Code
【搜索】POJ1242:Rescue