首页 > 代码库 > hdu 1240:Asteroids!(三维BFS搜索)

hdu 1240:Asteroids!(三维BFS搜索)

Asteroids!

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


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 1
O
0 0 0
0 0 0
END
START 3
XXX
XXX
XXX
OOO
OOO
OOO
XXX
XXX
XXX
0 0 1
2 2 1
END
START 5
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
0 0 0
4 4 4
END
 

 

Sample Output
1 0
3 4
NO ROUTE
 

 

Source
South Central USA 2001
 

 

Recommend
zf   |   We have carefully selected several similar problems for you:  1242 1072 1372 1238 1548 
 
  三维BFS搜索
  题意是先输入一行START N,“START”为字符串,表示开始,忽略即可。N是地图大小,为N*N*N。然后输入N*N行,每行N个字符,每N行表示一层,N层

  这道题是三维BFS搜索,类似于 hdu 1253:胜利大逃亡(基础广搜BFS) 

  注意输入的时候,先输入第一层,也就是说z坐标轴等于0,等输入完第一层再将z坐标轴变为1。也就是说z坐标轴变化的要最慢,放在第一层循环。即三层循环结构应为:
for(i=0;i<n;i++)    //输入地图
    for(j=0;j<n;j++)
        for(k=0;k<n;k++)
            cin>>a[j][k][i];

   代码:

 1 #include <iostream>
 2 #include <string.h>
 3 #include <queue> 
 4 using namespace std;
 5 char a[11][11][11];    //记录地图
 6 bool isv[11][11][11];    //记录访问过没有 
 7 int dx[6] = {0,1,0,-1,0,0};
 8 int dy[6] = {1,0,-1,0,0,0}; 
 9 int dz[6] = {0,0,0,0,-1,1};
10 int n;
11 int sx,sy,sz,ex,ey,ez;
12 struct NODE{
13     int x;
14     int y;
15     int z;
16     int step;
17 };
18 bool judge(int x,int y,int z)
19 {
20     if( x<0 || y<0 || z<0 || x>=n || y>=n || z>=n )    //出界 
21         return 1;
22     if( isv[x][y][z] )    //走过 
23         return 1;
24     if( a[x][y][z]==X )    //遇到墙 
25         return 1;
26     return 0;
27 }
28 int bfs()    //返回到达终点的时间 
29 {
30     memset(isv,0,sizeof(isv));
31     queue <NODE> q;
32     NODE cur,next;
33     cur.x = sx;
34     cur.y = sy;
35     cur.z = sz;
36     cur.step = 0;
37     isv[sx][sy][sz] = true;
38     q.push(cur);    //第一个节点入队 
39     while(!q.empty()){
40         cur = q.front();
41         q.pop();    //队首出队
42         if( cur.x==ex && cur.y==ey && cur.z==ez){
43             return cur.step;
44         }
45         for(int i=0;i<6;i++){
46             int nx = cur.x + dx[i];
47             int ny = cur.y + dy[i];
48             int nz = cur.z + dz[i];
49             if(judge(nx,ny,nz))    //判定    
50                 continue;
51             //可以走
52             next.x = nx;
53             next.y = ny;
54             next.z = nz;
55             isv[nx][ny][nz] = true;    //记录访问过 
56             next.step = cur.step + 1; 
57             q.push(next);
58         } 
59     }
60     return -1;
61 }
62 int main()
63 {
64     char str[20];
65     int i,j,k;
66     while(cin>>str>>n){
67         for(i=0;i<n;i++)    //输入地图
68             for(j=0;j<n;j++)
69                 for(k=0;k<n;k++)
70                     cin>>a[j][k][i];
71         cin>>sx>>sy>>sz;
72         cin>>ex>>ey>>ez;
73         cin>>str;
74         int step = bfs();
75         if(step!=-1)    //到达终点
76             cout<<n<< <<step<<endl;
77         else
78             cout<<"NO ROUTE"<<endl;
79     }
80     return 0;
81 }

 

Freecode : www.cnblogs.com/yym2013