[ACM] HDU 1242 Rescue (优先队列)

来源:互联网 发布:字体管家mac版 编辑:程序博客网 时间:2024/05/01 12:46
 

Rescue


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
 


 

Author

 

CHEN, Xue
 


 

Source

 

ZOJ Monthly, October 2003

 

解题思路:

简单的优先队列题目。复习一下优先队列用法.

代码:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <algorithm>  
  4. #include <string.h>  
  5. #include <stdlib.h>  
  6. #include <cmath>  
  7. #include <iomanip>  
  8. #include <vector>  
  9. #include <set>  
  10. #include <map>  
  11. #include <stack>  
  12. #include <queue>  
  13. #include <cctype>  
  14. using namespace std;  
  15. const int maxn=202;  
  16. char mp[maxn][maxn];  
  17. bool vis[maxn][maxn];  
  18. int dx[4]={0,0,-1,1};  
  19. int dy[4]={-1,1,0,0};  
  20. int sx,sy,ex,ey;  
  21. int n,m;  
  22.   
  23. struct Node  
  24. {  
  25.     int x,y;  
  26.     int step;  
  27. };  
  28. int step[maxn][maxn];  
  29.   
  30. bool operator<(Node a,Node b)  
  31. {  
  32.     return a.step>b.step;  
  33. }  
  34.   
  35. bool ok(int x,int y)  
  36. {  
  37.     if(x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]!='#')  
  38.         return true;  
  39.     return false;  
  40. }  
  41.   
  42. void BFS(int x,int y)  
  43. {  
  44.     step[x][y]=0;  
  45.     memset(vis,0,sizeof(vis));  
  46.     vis[x][y]=1;  
  47.     priority_queue<Node>q;  
  48.     Node tp;  
  49.     tp.x=x,tp.y=y,tp.step=0;  
  50.     q.push(tp);  
  51.     while(!q.empty())  
  52.     {  
  53.         Node first=q.top();  
  54.         if(first.x==ex&&first.y==ey)  
  55.             break;  
  56.         q.pop();  
  57.         for(int i=0;i<4;i++)  
  58.         {  
  59.             int nextx=first.x+dx[i];  
  60.             int nexty=first.y+dy[i];  
  61.             if(!vis[nextx][nexty]&&ok(nextx,nexty))  
  62.             {  
  63.                 vis[nextx][nexty]=1;  
  64.                 tp.x=nextx;  
  65.                 tp.y=nexty;  
  66.                 if(mp[nextx][nexty]=='x')  
  67.                 {  
  68.                     step[nextx][nexty]=first.step+2;  
  69.                     tp.step=first.step+2;  
  70.                 }  
  71.                 else  
  72.                 {  
  73.                     step[nextx][nexty]=first.step+1;  
  74.                     tp.step=first.step+1;  
  75.                 }  
  76.                 q.push(tp);  
  77.             }  
  78.         }  
  79.     }  
  80. }  
  81.   
  82. int main()  
  83. {  
  84.     while(scanf("%d%d",&n,&m)!=EOF)  
  85.     {  
  86.         for(int i=1;i<=n;i++)  
  87.             for(int j=1;j<=m;j++)  
  88.         {  
  89.             cin>>mp[i][j];  
  90.             if(mp[i][j]=='r')  
  91.                 sx=i,sy=j;  
  92.             else if(mp[i][j]=='a')  
  93.                 ex=i,ey=j;  
  94.         }  
  95.         BFS(sx,sy);  
  96.         if(!vis[ex][ey])  
  97.             printf("Poor ANGEL has to stay in the prison all his life.\n");  
  98.         else  
  99.             printf("%d\n",step[ex][ey]);  
  100.     }  
  101.     return 0;  
  102. }  

 

优先队列用法:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ///优先队列是默认int从大到小priority_queue<int>q1,也可以定义为从小到大priority_queue<int,vector<int>,greater<int> >q2;  
  2. 也可以自定义优先级,重载<  
  3.   
  4. #include <iostream>  
  5. #include <functional>  
  6. #include <queue>  
  7. using namespace std;  
  8.   
  9. ///自定义优先级,两种写法,按照优先级从大到小的顺序  
  10. /* 
  11. struct node 
  12. { 
  13.     friend bool operator<(node n1,node n2) 
  14.     { 
  15.         return n1.priority<n2.priority; 
  16.     } 
  17.     int priority; 
  18.     int value; 
  19. };*/  
  20.   
  21. struct node  
  22. {  
  23.     int priority;  
  24.     int value;  
  25. };  
  26. bool operator<(node a,node b)  
  27. {  
  28.     return a.priority<b.priority;  
  29. }  
  30.   
  31.   
  32. int main()  
  33. {  
  34.     const int len=5;  
  35.     int i;  
  36.     int a[len]={3,5,9,6,2};  
  37.     //优先队列中从大到小输出  
  38.     priority_queue<int>q1;  
  39.     for(i=0;i<len;i++)  
  40.         q1.push(a[i]);  
  41.     for(int i=0;i<len;i++)  
  42.     {  
  43.         cout<<q1.top();  
  44.         q1.pop();  
  45.     }  
  46.     cout<<endl;  
  47.     //优先队列中从小到大输出  
  48.   
  49.     /** 
  50.     priority_queue<int,vector<int>,greater<int> >q2; 
  51.     for(i=0;i<len;i++) 
  52.         q2.push(a[i]); 
  53.     for(i=0;i<len;i++) 
  54.     { 
  55.         cout<<q2.top(); 
  56.         q2.pop(); 
  57.     }*/  
  58.   
  59.     priority_queue<int,vector<int>,greater<int> >q;  
  60.     for(int i=0;i<len;i++)  
  61.         q.push(a[i]);  
  62.     while(!q.empty())  
  63.     {  
  64.         cout<<q.top();  
  65.         q.pop();  
  66.     }  
  67.     cout<<endl;  
  68.     //按照某个优先级输出,该代码中为priority值大的先输出  
  69.     priority_queue<node>q3;  
  70.     node b[len];  
  71.     b[0].priority=6;b[0].value=1;  
  72.     b[1].priority=9;b[1].value=5;  
  73.     b[2].priority=2;b[2].value=3;  
  74.     b[3].priority=8;b[3].value=2;  
  75.     b[4].priority=1;b[4].value=4;  
  76.     for(i=0;i<len;i++)  
  77.         q3.push(b[i]);  
  78.     cout<<"优先级"<<'\t'<<"值"<<endl;  
  79.     for(i=0;i<len;i++)  
  80.     {  
  81.         cout<<q3.top().priority<<'\t'<<q3.top().value<<endl;  
  82.         q3.pop();  
  83.     }  
  84.     return 0;  
  85. }  


运行:

96532
23569

优先级  值
9       5
8       2
6       1
2       3
1       4


 

0 0
原创粉丝点击