BFS topic hdu1242 rescue

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1242

The only difference in BFS is that point X can be stuck.

The method adopted this time is different from the previous method for processing X points.

Note vis [] [] records whether to access and the shortest time from this point

# Include <stdio. h> # include <string. h> # include <stdlib. h> char map [205] [205]; int vis [205] [205]; struct node {int X, Y; int s;} DUI [80005]; int dir [4] [2] = {, 0,-, 0,-}; int n, m, s_x, s_y; void BFS () {int I, j; int head, tail; struct node next, now; memset (VIS, 0, sizeof (VIS); Head = tail = 0; DUI [tail]. X = s_x; DUI [tail]. y = s_y; DUI [tail ++]. S = 1; vis [s_x] [s_y] = 1; while (Head <tail) {now. X = DUI [head]. x; now. y = DUI [head]. Y; Now. S = DUI [head ++]. s; If (Map [now. x] [now. y] = 'X') {map [now. x] [now. y] = '. '; DUI [tail]. X = now. x; DUI [tail]. y = now. y; DUI [tail ++]. S = now. S + 1; continue;} for (I = 0; I <4; I ++) {next. X = now. X + dir [I] [0]; next. y = now. Y + dir [I] [1]; next. S = now. S + 1; if (next. x <0 | next. Y <0 | next. x> = n | next. y> = M | map [next. x] [next. y] = '#') continue; If (vis [next. x] [next. y]! = 0) continue; vis [next. x] [next. y] = next. s; DUI [tail]. X = next. x; DUI [tail]. y = next. y; DUI [tail ++]. S = next. s ;}} int main () {int I, j, ans; while (scanf ("% d", & N, & M )! = EOF) {ans = 10000000; getchar (); for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {scanf ("% C", & map [I] [J]); If (Map [I] [J] = 'A ') {s_x = I; s_y = J ;}} getchar () ;}bfs (); for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {If (Map [I] [J] = 'R ') {If (vis [I] [J] = 0) continue; else {If (ANS> vis [I] [J]) ans = vis [I] [J] ;}}} if (ANS = 10000000) printf ("Poor Angel has to stay in the prison all his life. \ n "); else printf (" % d \ n ", ans-1);} 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.