HDU 1242 Rescue (priority queue + bfs)
Address: HDU 1242
This question has a special place compared to the ordinary bfs. It will consume extra time when soldiers pass through it, that is to say, it is not necessarily the shortest time when the first search is made. You need to search all of them. At this time, the benefits of priority queue are apparent. The priority queue can be used to sort the elements in the queue by time, so that the first time is always short. In this way, the first time found must be short, you don't need to search it all. PS: I did this to learn the priority queue .. It is not a priority queue for this question ..
The Code is as follows;
#include
#include
#include
#include
#include
#include
#include
#include
#includeusing namespace std;int vis[300][300], n, m;char mp[300][300];int jx[]={0,0,1,-1};int jy[]={1,-1,0,0};struct node{ int x, y, ans; bool operator<(const node &a) const{ return ans>a.ans; }};void bfs(int x, int y){ memset(vis,0,sizeof(vis)); priority_queue
q; node f1, f2; f1.x=x; f1.y=y; f1.ans=0; q.push(f1); vis[x][y]=1; while(!q.empty()) { f1=q.top(); q.pop(); if(mp[f1.x][f1.y]=='r') { printf("%d\n",f1.ans); return ; } for(int i=0;i<4;i++) { f2.x=f1.x+jx[i]; f2.y=f1.y+jy[i]; if(f2.x>=0&&f2.x
=0&&f2.y