首次接觸到優先隊列是在http://acm.hdu.edu.cn/showproblem.php?pid=1242
題目意思:在一張圖中,有多個朋友要去營救一個公主,其中'.'代表此條路可以走,需時間爲1,'r'代表公主的朋友,'x'代表敵人,走這條路值時間需加1,'#'代表此條路不可走,問公主獲救最小需要多少時間。
這道題目需要注意的地方是朋友是多個的,而公主只有一個,所以不能把朋友的位置當做起點,若如此,很多路徑都會重複,必然超時。所以把公主的位置當做起點,當第一次找到某個朋友的時候用的時間即是最短時間。此時需要用到BFS,而碰到'x'和'.'所需要用到的時間是不一致的,這裏優先隊列變起到很明顯的作用。
首先,優先隊列的定義:每次從隊列中取出的是具有最高優先權的元素。
其次,優先隊列的應用很靈活,可以和其他算法很巧妙地結合,這得靠自己分析咯。
貼下這道題目代碼:
#include<iostream>
#include<queue>
using namespace std;
#define MAXN 205
char map[MAXN][MAXN];
int N,M,starti,startj;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
struct love
{
int x,y;
int time;
friend bool operator<(love a,love b){
return a.time>b.time;
}//按權值從小到大排
};
int BFS(int nowi,int nowj,int step)
{
priority_queue <love> Q;
love first,next;
first.x=nowi;
first.y=nowj;
first.time=step;
Q.push(first);
int fx,fy;
while(!Q.empty()){
first=Q.top();
Q.pop();
for(int i=0;i<4;i++){
fx=dir[i][0]+first.x;
fy=dir[i][1]+first.y;
if(fx>=0&&fx<N&&fy>=0&&fy<M&&map[fx][fy]!='#'){
if(map[fx][fy]=='r'){
return first.time+1;
}
else if(map[fx][fy]=='.'){
next.time=first.time+1;
}
else if(map[fx][fy]=='x'){
next.time=first.time+2;
}
next.x=fx;
next.y=fy;
map[next.x][next.y]='#';
Q.push(next);
}
}
}
return -1;
}
int main()
{
int i,j;
while(cin>>N>>M)
{
for(i=0;i<N;i++){
for(j=0;j<M;j++){
cin>>map[i][j];
if(map[i][j]=='a'){
starti=i;
startj=j;
}
}
}
int ans=BFS(starti,startj,0);
if(ans==-1)
printf("Poor ANGEL has to stay in the prison all his life./n" );
else
printf("%d/n",ans);
}
}