Rescue
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 15639 accepted submission (s): 5673
Question: Give the start point and the end point through the maze to find the shortest path (note that there are multiple start points)
Simple BFS (because there are multiple starting points, perform the following operations to search for the starting point from the end point) + obtain the optimal solution from the priority queue
# Include <iostream> # include <cstdio> # include <cstring> # include <stack> # include <queue> using namespace STD; int n, m, ANS, Sx, Sy; char map [205] [205]; int vis [205] [205]; int DX [4] = {-,}; int dy [4] =, 0,-1}; struct node {friend bool operator <(const node A, const Node B) {return. depth> B. depth; // large number of low priority} int X, Y, depth;}; int BFS (int x, int y) {int I; node A, T, P; priority_queue <node> q;. X = x;. y = y;. depth = 0; Q. Push (a); While (! Q. empty () {T = Q. top (); q. pop (); for (I = 0; I <4; I ++) {int XX, YY; p. X = T. X + dx [I]; p. y = T. Y + dy [I]; If (P. x> = 0 & P. x <n & P. y> = 0 & P. Y <M &&! Vis [p. x] [p. y]) {vis [p. x] [p. y] = 1; p. depth = T. depth + 1; if (Map [p. x] [p. y] = 'X') p. depth ++; If (Map [p. x] [p. y] = 'R') return p. depth; q. push (p) ;}}return 0 ;}int main () {char a; int I, j; while (CIN >>> n >> m) {memset (VIS, 0, sizeof (VIS); 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') {SX = I; Sy = J; vis [I] [J] = 1 ;} if (Map [I] [J] = '#') vis [I] [J] = 1;} ans = BFS (sx, Sy); I F (ANS! = 0) printf ("% d \ n", ANS); else cout <"Poor Angel has to stay in the prison all his life. "<Endl;} return 0 ;}
HDU 1242 rescue (BFS + priority queue)