Pay attention to the word "each" in this question, so there may be multiple 'R', so it is relatively simple to search for the saved person.
The priority queue is unknown at the beginning, so the priority queue is wa at the beginning.
The operations of the priority queue are similar to those of the queue, but the front () function in the queue should be replaced by the top () function. Other operations are similar.
# Include <iostream> # include <queue> # define maxn 205 char map [maxn] [maxn]; int visit [maxn] [maxn]; using namespace STD; int dir [4] [2] = {-}, {}, {0,-1}, {}; int M, N; bool OK (int I, Int J) {return map [I] [J]! = '#' & I> 0 & I <= N & J> 0 & J <= m;} struct node {int X, Y, time; friend bool operator <(const node & A, const node & B) {return. time> B. time ;}}; int BFS (int x, int y) {int I; node St, Ed; priority_queue <node> que; // defines a priority queue for memset (visit, 0, sizeof (visit); ST. X = x; ST. y = y; ST. time = 0; visit [st. x] [st. y] = 1; que. push (ST); While (! Que. empty () {ST = que. top (); que. pop (); If (Map [st. x] [st. y] = 'R') return St. time; for (I = 0; I <4; I ++) {ed. X = ST. X + dir [I] [0]; ed. y = ST. Y + dir [I] [1]; If (OK (ed. x, Ed. Y )&&! Visit [ed. x] [ed. y]) {visit [ed. x] [ed. y] = 1; if (Map [ed. x] [ed. y] = 'X') Ed. time = ST. time + 2; else ed. time = ST. time + 1; que. push (ed) ;}}return-1 ;}int main () {// freopen ("1242.txt"," r ", stdin); int I, j; int X, y, ans; while (scanf ("% d", & N, & M )! = EOF) {for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) cin> map [I] [J]; // scanf ("% s", map [I]); for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) if (Map [I] [J] = 'A') {x = I; y = J; break;} ans = BFS (x, y); If (ANS =-1) printf ("Poor Angel has to stay in the prison all his life. \ n "); else printf (" % d \ n ", ANS);} return 0 ;}
HDU 1242 rescue