HDU 1240 asteroids! (3D BFS)

Source: Internet
Author: User
Asteroids!

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2599 accepted submission (s): 1745

Problem descriptionyou're in space.
You want to get home.
There are asteroids.
You don't have want to hit them.

Inputinput to this problem will consist of a (non-empty) series of up to 100 data sets. each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

A single data set has 5 components:

Start line-a single line, "Start N", where 1 <= n <= 10.

Slice list-a series of N slices. Each slice is an N x n matrix representing a horizontal slice through the asteroid field. Each position in the matrix will be one of two values:

'O'-(the letter "oh") empty space

'X'-(upper-case) Asteroid present

Starting position-a single line, "a B c", denoting the <a, B, c> coordinates of your craft's starting position. the coordinate values will be integers separated by individual spaces.

Target Position-a single line, "d e f", denoting the <D, E, F> coordinates of your target's position. the coordinate values will be integers separated by individual spaces.

End Line-a single line, "end"

The origin of the coordinate system is <, 0>. Therefore, each component of each coordinate vector will be an integer between 0 and N-1, fully passive.

The first coordinate in a set indicates the column. left column = 0.

The Second coordinate in a set indicates the row. Top row = 0.

The third coordinate in a set indicates the slice. First slice = 0.

Both the starting position and the target position will be in empty space.

Outputfor each data set, there will be exactly one output set, and there will be no blank lines separating output sets.

A single output set consists of a single line. if a route exists, the line will be in the format "x y ", where X is the same as N from the corresponding input data set and Y is the least number of moves necessary to get your ship from the starting position
The target position. If there is no route from the starting position to the target position, the line will be "no route" instead.

A move can only be in one of the six basic directions: Up, down, left, right, forward, back. phrased more precisely, a move will either increment or decrement a single component of your current position vector by 1.

Sample Input

 
Start 1o0 0 00 0 0 endstart 3xxxxxxxxxoooooooooxxxxxxxxx0 0 12 2 1 endstart defaults 0 04 4 4end

Sample output

 
1 03 4NO route

Sourcesouth central USA 2001

Recommend

ZF

Question: 3D maze. The minimum number of steps from the start point to the end point. If you cannot reach the end, output "No route ".

Code:

# Include <cstdio> # include <iostream> # include <cstring> # include <queue> using namespace STD; struct node {int x, y, z; int steps;} start, endd, next; int DX [6] = {0, 1,-1}; // int dy [6] = {0,-,} before and after the upper and lower sides }; int DZ [6] = {1,-, 0}; char maps [15] [15] [15]; int N, Res; bool CANGO (node & A) {if (. x> = 0 &. x <n &. y> = 0 &. Y <n &. z> = 0 &. z <n) return true; else return false;} int BFS () {If (start. X = endd. X & start. y = endd. Y & & Amp; start. z = endd. Z) return res; node cur; queue <node> q; while (! Q. empty () Q. pop (); q. push (start); maps [start. x] [start. y] [start. z] = 'X'; while (! Q. empty () {cur = Q. front (); q. pop (); For (INT I = 0; I <6; I ++) {next. X = cur. X + dx [I]; next. y = cur. Y + dy [I]; next. z = cur. Z + DZ [I]; If (next. X = endd. X & next. y = endd. Y & next. z = endd. z) // return cur. steps + 1; // If (maps [next. x] [next. y] [next. z] = 'O' & CANGO (next) {// printf ("test: % d \ n", next. x, next. y, next. z); next. steps = cur. steps + 1; maps [next. x] [next. y] [next. z] = 'X'; q. push (next) ;}}return-1 ;}int main (){ Int I, j; char st [6], Ed [6]; while (scanf ("% S % d", & St, & N )! = EOF) {getchar (); for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) scanf ("% s", maps [I] [J]); getchar ();} scanf ("% d", & start. x, & start. y, & start. z); scanf ("% d", & endd. x, & endd. y, & endd. z); getchar (); gets (ed); Res = 0; start. steps = 0; Res = BFS (); If (RES> = 0) printf ("% d \ n", N, Res ); else printf ("No route \ 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.