Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1240
asteroids!
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3549 Accepted Submission (s): 2354
Problem Descriptionyou ' re in space.
You want to get home.
There is asteroids.
You don ' t want-to-hit them. Inputinput to this problem would consist of a (Non-empty) series of up to data sets. Each data set is formatted according to the following description, and there would be no blank lines separating data s Ets.
A Single Data set have 5 components:
Start Line-a single line, "Start N", where 1 <= n <= 10.
Slice list-a series of N slices. Each slice are an n x n matrix representing a horizontal slice through the asteroid field. Each position in the matrix would be one of the 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 would 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 would be integers separated by individual spaces.
End Line-a single line, "End"
The origin of the coordinate system is <0,0,0>. Therefore, each component of each coordinate vector would be an integer between 0 and N-1, inclusive.
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 would be in empty space.
Outputfor each data set, there'll be exactly one output set, and there'll be no blank lines separating output sets.
A single output set consists of a single line. If a route exists, the line would be in the format "X Y", where X was the same as N from the corresponding input data set an D Y is the least number of moves necessary to get your ship from the starting position to the target position. If there is no route from the starting position to the target position, the line would be "no route" instead.
A move can only is in one of the six basic directions:up, down, left, right, forward, back. Phrased more precisely, a move would either increment or decrement a single component of your current position vectors by 1.
Sample inputstart 1o0 0 0 0ENDSTART 3xxxxxxxxxoooooooooxxxxxxxxx0 0 2 1ENDSTART 5OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO OOOOOOOOOOOOOOOXXXXXXXXXXXXXXXXXXXXXXXXXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO0 0 4 4END
Sample Output1 4NO ROUTE title: To go home in space, to avoid planets in the stars. Given a three-dimensional map, x represents the planetary area, O stands for blanks, and then gives the coordinates of the start and end points. The title asks whether the destination can be reached, and if it is possible to output n and the minimum number of steps, Output no ROUTE. Topic idea: Roughly is a general wide search on OK, to find the smallest number of steps, followed by the attention input, there is a "end" to end an input. See the code.
1#include <iostream>2#include <cstdio>3#include <queue>4 5 using namespacestd;6 7 intdir[6][3]={0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0};8 Charmap[ -][ -][ -];9 intSx,sy,sz,ex,ey,ez,n,flag,step;Ten structnode{ One intx,y,z,t; A }s,ss; - - voidBFS () the { -Queue<node>q,qq; -s.x=SX; -s.y=Sy; +s.z=sz; -s.t=0; + Q.push (s); A if(sx==ex&&sy==ey&&sz==ez) at { -flag=1; -step=0; - return ; - } - while(!q.empty ()) in { -s=Q.front (); to Q.pop (); + //cout<< "1" <<endl; - for(intI=0;i<6; i++) the { * intx=s.x+dir[i][0]; $ inty=s.y+dir[i][1];Panax Notoginseng intz=s.z+dir[i][2]; - if(x>=0&&x<n&&y>=0&&y<n&&z>=0&&z<n&&map[z][y][x]!='X') the { +ss.x=x; Ass.y=y; thess.z=Z; +ss.t=s.t+1; -map[z][y][x]='X'; $ if(x==ex&&y==ey&&z==ez) $ { -flag=1; -step=ss.t; the return ; - }Wuyi Q.push (ss); the } - } Wu } - } About $ - intMain () - { - Charstr[ the],ch[ the]; A + while(SCANF ("%s%d", str,&n)! =EOF) the { - //flag=0; $ for(intI=0; i<n;i++) the for(intj=0; j<n;j++) thescanf"%s", Map[i][j]); thescanf"%d%d%d",&sx,&sy,&sz); thescanf"%d%d%d",&ex,&ey,&ez); -scanf"%s", ch); inflag=0; the BFS (); the if(flag==1) Aboutprintf ("%d%d\n", n,step); the Else theprintf ("NO route\n"); the } + return 0; -}
HDU 1240 Asteroids! (BFS)