HDU 1242 rescue

Source: Internet
Author: User

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

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.