Title Address: http://acm.hdu.edu.cn/showproblem.php?pid=1242
I did this topic with the bfs+ priority queue. I heard that only using BFS will time out.
Because this problem has multiple rescuers, so we start from the rescue of the BFS, find the nearest rescuers is the shortest time.
Define a struct, hold coordinates x and Y, and the time it takes to reach the current point (x, y).
struct Node { int x, y; int time; friend BOOL operator < (const node &a,const node &b) { //time-less placed in front of queue return a.time>b.time; }};
Notice that the time is low before you dequeue.
Focus on the BFS process, the current lattice named now, the next lattice passed the name of next, first assign the starting point to now and then queue.
now.x=x; now.y=y; now.time=0;//source point to the source point time is initially 0 vis[now.x][now.y]=1; Que.push (now);//The source point is enqueued
Then as long as the queue is not empty, take out the head node of the queue, determine whether it is an end point or any R, if it is R, then return the time required to reach the current node: Now.time, if it is not r, then all four directions of now will be traversed 100, if not the wall to join the queue, and the current time update: Next.time=now.time+delta. The value of delta encounters X is 2, otherwise it is 1. The coordinates that have been circulating until que.top come out represent R.
If the queue is empty and the entire map has not found R, then return-1, the representative can not be rescued.
Okay, put a code on it:
#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; int n,m;int Vis[202][202];char map[202][202];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};struct Node {int x, y; int time; friend BOOL operator < (const node &a,const node &b) {//less time placed in front of queue return a.time>b.time; }};int can_go (int x,int y) {if (x>=0&&x<n&&y>=0&&y<m&&map[x][y]!= ' # ') { return 1; } else return 0;} int bfs (int x,int y) {int i; Node Now,next; priority_queue<node>que; memset (vis,0,sizeof (VIS)); Now.x=x; Now.y=y; now.time=0;//source point to the source point time is initially 0 vis[now.x][now.y]=1; Que.push (now);//The source point is queued while (!que.empty ()) {now=que.top (); Que.pop (); if (map[now.x][now.y]== ' R ') {return now.time; } for (i=0;i<4;i++) {next.x=now.x+dir[i][0]; NEXT.Y=NOW.Y+DIR[I][1]; if (Can_go (Next.x,nexT.Y) (&&!vis[next.x][next.y]) {vis[next.x][next.y]=1; if (map[next.x][next.y]== ' x ') {next.time=now.time+2; } else {next.time=now.time+1; } que.push (next); }}} return-1;} int main () {int ans; int i,j; int x, y; while (scanf ("%d%d", &n,&m)!=eof) {for (i=0;i<n;i++) {scanf ("%s", Map[i]); } for (i=0;i<n;i++) for (j=0;j<m;j++) if (map[i][j]== ' a ') {x=i; Y=j; Break } ans=bfs (x, y); if (ans==-1) {printf ("Poor ANGEL have to stay in the prison all he life.\n"); } else {printf ("%d\n", ans); }} return 0;}
Copyright NOTICE: Welcome Reprint, please mention the source at the beginning of the article when reproduced
HDU 1242 Rescue (bfs+ priority queue)