Poj 2251 Dungeon Master (BFS)

Source: Internet
Author: User

Link: poj 2251

The question is extended from two-dimensional space to three-dimensional space. It can be moved up, down, left, right, and back, and can only be moved to adjacent spaces each time,

It takes at least one minute to start from the start point to the end point.

# Indicates the rock, space, s indicates the starting point, and E indicates the ending point.

This question only needs to be performed in six BFS directions, and has timed out since then. Change it to DFS for WA,

It is found that after each access, it should be immediately marked as accessed, And I marked as accessed by converting the accessed point into #, that is, the rock.

# Include <cstdio >#include <cstring> # include <queue> using namespace STD; struct Stu {int I, j, k ;}; char s [35] [35] [35]; int M, N, P; int BFS (struct Stu a, struct Stu B) {int I, J, K, T [35] [35] [35] = {0}; queue <struct Stu> q; q. push (a); s [. i] [. j] [. k] = '#'; while (! Q. empty () {A = Q. front (); q. pop (); I =. i; j =. j; k =. k; if (I = B. I & J = B. J & K = B. k) // return t after the end is reached [I] [J] [k]; if (I> 0 & S [I-1] [J] [k]! = '#') {. I = I-1;. j = J;. k = K; q. push (a); t [I-1] [J] [k] = T [I] [J] [k] + 1; // calculate the time s [I-1] [J] [k] = '#'; // remember to mark it immediately after access} if (I <M-1 & S [I + 1] [J] [k]! = '#') {. I = I + 1;. j = J;. k = K; q. push (a); t [I + 1] [J] [k] = T [I] [J] [k] + 1; s [I + 1] [J] [k] = '#';} If (j> 0 & S [I] [J-1] [k]! = '#') {. I = I;. j = J-1;. k = K; q. push (a); t [I] [J-1] [k] = T [I] [J] [k] + 1; s [I] [J-1] [k] = '#';} If (j <n-1 & S [I] [J + 1] [k]! = '#') {. I = I;. j = J + 1;. k = K; q. push (a); t [I] [J + 1] [k] = T [I] [J] [k] + 1; s [I] [J + 1] [k] = '#';} If (k> 0 & S [I] [J] [k-1]! = '#') {. I = I;. j = J;. k = K-1; q. push (a); t [I] [J] [k-1] = T [I] [J] [k] + 1; s [I] [J] [k-1] = '#';} If (k <P-1 & S [I] [J] [k + 1]! = '#') {. I = I;. j = J;. k = k + 1; q. push (a); t [I] [J] [k + 1] = T [I] [J] [k] + 1; s [I] [J] [k + 1] = '#';} return-1; // if it is not possible to reach the end point, return-1} int main () {int I, J, K, sum; struct Stu X, Y; while (scanf ("% d", & M, & N, & P )! = EOF) {getchar (); If (M = 0 & n = 0 & P = 0) break; for (I = 0; I <m; I ++) {for (j = 0; j <n; j ++) {for (k = 0; k <p; k ++) {scanf ("% C", & S [I] [J] [k]); If (s [I] [J] [k] ='s ') {X. I = I; X. j = J; X. k = K;} else if (s [I] [J] [k] = 'E') {Y. I = I; Y. j = J; Y. k = K ;}} getchar () ;}getchar () ;}sum = BFS (x, y); If (sum! =-1) printf ("escaped in % d minute (s). \ n", sum); else printf ("trapped! \ 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.