HDU1240:Asteroids!

来源:互联网 发布:剑三捏脸数据截图 编辑:程序博客网 时间:2024/05/30 04:30

点击打开题目链接

Asteroids!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2625    Accepted Submission(s): 1765


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

Input
Input 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,0,0>. Therefore, each component of each coordinate vector will 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 will be in empty space.

 

Output
For 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 to 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 0ENDSTART 3XXXXXXXXXOOOOOOOOOXXXXXXXXX0 0 12 2 1ENDSTART 5OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXXXXXXXXXXXXXXXXXXXXXXXXXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO0 0 04 4 4END
 

Sample Output
1 03 4NO ROUTE
 

Source
South Central USA 2001
 

Recommend
zf
 


=====================================题目大意=====================================


从一个规格为 N X N X N 的空间中找到从起始点(A,B,C)到目标点(D,E,F)的最短路径长度。


=====================================算法分析=====================================


简单BFS,在普通2D搜索题的基础上稍加修改即可。


======================================代码========================================




#include<queue>#include<cstdio>#include<cstring>using namespace std;#define DIS(P)      Dis[P.lv][P.row][P.col]                                           //坐标对应的Dis值#define MAP(P)      Map[P.lv][P.row][P.col]                                           //坐标对应的Map值#define SAME(A,B)   ((A.lv==B.lv)&&(A.row==B.row)&&(A.col==B.col))                    //相同坐标#define LEGCORD(P)  ((0<=P.lv&&P.lv<N)&&(0<=P.row&&P.row<N)&&(0<=P.col&&P.col<N))     //合法坐标const int Dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};int N,Dis[15][15][15];char Map[15][15][15];struct COORD { int lv,row,col; }S,E;COORD MoveCoord(COORD Cur,int N)                                                      //坐标Cur朝方向N移动一次后得到的坐标  {COORD tmp;tmp.lv =Cur.lv +Dir[N][0];tmp.row=Cur.row+Dir[N][1];tmp.col=Cur.col+Dir[N][2];return tmp;}void BFS(){memset(Dis,-1,sizeof(Dis));DIS(S)=0;queue<COORD> q;for(q.push(S);!q.empty();q.pop()){COORD cur=q.front();  if(SAME(cur,E)) { printf("%d %d\n",N,DIS(E));  return;}for(int n=0;n<6;++n){COORD tmp=MoveCoord(cur,n);if(LEGCORD(tmp)&&MAP(tmp)!='X'&&DIS(tmp)==-1){DIS(tmp)=DIS(cur)+1;q.push(tmp);}}}printf("NO ROUTE\n");}void ReaData(){for(int i=0;i<N;++i){for(int j=0;j<N;++j){scanf("%s",Map[i][j]);}}scanf("%d%d%d",&S.col,&S.row,&S.lv);scanf("%d%d%d",&E.col,&E.row,&E.lv);scanf("%*s");}int main(){while(scanf("%*s%d",&N)==1){ReaData();BFS();}return 0;}

原创粉丝点击