Zoj 1649 BFS

Source: Internet
Author: User
// Zoj 1649 # include <stdio. h> # include <queue> # include <string. h> using namespace STD; # define maxn 200 # define INF 1000000 struct point {int X, Y; int step, time ;}; queue <point> q; int n, m, ax, ay; char map [maxn] [maxn]; int time [maxn] [maxn]; int dir [4] [2] ={{-1, 0 }, {}, {0,-1}, {}; int BFS (point S) {int I; q. push (s); point JK; while (! Q. empty () {JK = Q. front (); q. pop (); for (I = 0; I <4; I ++) {int x = JK. X + dir [I] [0]; int y = JK. Y + dir [I] [1]; if (x> = 0 & x <= N-1 & Y> = 0 & Y <= M-1 & map [x] [Y]! = '#') {Point t; T. X = x; T. y = y; T. step = JK. step + 1; T. time = JK. time + 1; if (Map [x] [Y] = 'X') T. time ++; If (T. time <time [x] [Y]) {time [x] [Y] = T. time; q. push (t) ;}}} return time [ax] [AY];} int main () {int I, j; // freopen ("input.txt ", "r", stdin); While (scanf ("% d", & N, & M )! = EOF) {memset (MAP, 0, sizeof (MAP); for (I = 0; I <n; I ++) scanf ("% s ", map [I]); int Sx, Sy; point start; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {time [I] [J] = inf; If (Map [I] [J] = 'A') {ax = I; ay = J ;} else if (Map [I] [J] = 'R') {SX = I; Sy = J ;}} start. X = SX; start. y = sy; start. step = 0; start. time = 0; int min = BFS (start); If (Min <inf) printf ("% d \ n", min ); else printf ("Poor Angel has to stay in the prison all his life. \ n ") ;}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.