HDU 1242 rescue (BFS + priority queue)

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.