HDU 1242 rescue

Source: Internet
Author: User

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 ;}

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.