Priority queue + wide search
For the first time, the reload operator is not written by me ........................
Code, Refer to Daniel's ......
..................
The original priority queue + BFS drops like this
# Include <iostream> # include <string> # include <algorithm> # include <queue> using namespace STD; char s [201] [201]; int n, m, vis [200] [200], ans; int dir [4] [2] = {}, {}, {-}, {0, -1 }}; bool es; struct node {int X, Y, DIS; node (INT _ x = 0, int _ y = 0, int _ Dis = 0 ): X (_ x), y (_ y), DIS (_ dis) {}; // automatically initialize friend bool operator <(const node & A, const node & B) // overload operator {return. dis> B. dis ;}}; node F, E; void DFS () {priority_queue <node> q; F. Dis = 0; q. Push (f); vis [F. X] [F. Y] = 1; while (! Q. empty () {node T = Q. top (); q. pop (); For (int K = 0; k <4; k ++) {int I = T. X + dir [k] [1]; Int J = T. Y + dir [k] [0]; if (I <= N & I> 0 & J <= M & J> 0 &&! Vis [I] [J] & S [I] [J]! = '#') {Vis [I] [J] = 1; if (s [I] [J] = 'X') Q. push (node (I, j, T. DIS + 2); If (s [I] [J] = '. ') Q. push (node (I, j, T. DIS + 1); If (s [I] [J] = 'A') {es = 1; ans = T. DIS + 1; return ;}}}return;} int main () {While (CIN >>n> m) {memset (VIS, 0, sizeof (VIS); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) {CIN> S [I] [J]; If (s [I] [J] = 'R') // locate the start point {f. X = I; F. y = J;} else if (s [I] [J] = 'A') {e. X = I; E. y = J ;}} es = 0; DFS (); If (ES) cout <ans <Endl; else cout <"Poor Angel has to stay in the prison all his life. "<Endl;} return 0 ;}