关闭

poj2225Asteroids!(三维BFS)

370人阅读 评论(0) 收藏 举报
分类:
Asteroids!
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2875   Accepted: 1090

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:
  1. Start line - A single line, "START N", where 1 <= N <= 10.
  2. 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

  3. 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.
  4. 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.
  5. 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

题目大意: 给定一个三维的迷宫(题目给定的优点坑爹,先列,后行,再高,后面慢慢讲)。已知起点和终点,问是否能从起点到达终点,并且最短的距离又是多少。

解题思路:  
第一:不用说,对于隐士图求最短路经,BFS无疑是最先考虑的选择。本题数据量不大,直接BFS。只是这里的BFS是对三维的迷宫,以前大部分都只是二维。
第二:就是怎么建立三维迷宫(太弱了,在这里就纠结了好久)。采用三维数组G[z][x][y]分别表示z,x,y轴。意思就如下(以样例2的输入为例):

因此定义G[z][x][y]其中z高,x行,y列
故,建立三维的方式为
for(int z=0;z<n;z++){
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
       cin>>element;
       G[z][i][j]=element;
   }
 }
}

但是本题如果不仔细审题的话很容易犯做,请大家注意:
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.//最后才是高
意思就是起点与终点的坐标为(列,行,高),所以要注意以下。

最后最重要的bfs也没什么好说的呢,理解,多敲,只有深刻理解才不会依赖模版,模版不是一个什么好东西,前期学学模仿模仿还可以,最后用,还得靠自己。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <cmath>
using namespace std;
const int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};//6个方向
const int inf=0x7ffff;
struct Node{
    int z,x,y,step;    //z,x,y,step
    Node(int z=0,int x=0,int y=0,int step=0):z(z),x(x),y(y),step(step) {} //Node构造函数
}que[1000+5];
Node st,ed;
int G[15][15][15],vis[15][15][15],n,minStep;
bool isFind;
void bfs(Node st,Node ed)             //三维bfs
{
    memset(vis,0,sizeof(vis));
    int head=0,tail=0;
    que[head]=st;
    vis[st.z][st.x][st.y]=1;
    while(head<=tail){
        Node tmp=que[head++];   //出队
        if(tmp.x==ed.x&&tmp.y==ed.y&&tmp.z==ed.z){ //如果找到目标
            isFind=true;
            minStep=tmp.step;
            return;
        }
        for(int i=0;i<6;i++){
            int nowZ=tmp.z+dir[i][0];
            int nowX=tmp.x+dir[i][1];
            int nowY=tmp.y+dir[i][2];
            if(nowX>=0&&nowX<n&&nowY>=0&&nowY<n&&nowZ>=0&&nowZ<n&&G[nowZ][nowX][nowY]&&!vis[nowZ][nowX][nowY]){
                vis[nowZ][nowX][nowY]=1; 
                que[++tail]=Node(nowZ,nowX,nowY,tmp.step+1);  //符合要求,入队
            }
        }
    }
}
int main()
{

    //freopen("in.txt","r",stdin);
    char strSt[20],strEd[20],ch;
    while(scanf("%s%d",strSt,&n)==2){
        isFind=false;
        for(int z=0;z<n;z++){
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++){
                    cin>>ch;
                    if(ch=='O') G[z][i][j]=1;
                    else    G[z][i][j]=0;
            }
        }
        scanf("%d%d%d",&st.y,&st.x,&st.z);   //注意输入的是(列,行,高)
        scanf("%d%d%d",&ed.y,&ed.x,&ed.z);  //(列,行高)
        scanf("%s",strEd);
        bfs(st,ed);
        if(isFind){
            printf("%d %d\n",n,minStep);
        }else{
            printf("NO ROUTE\n");
        }
    }
    return 0;

}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:42742次
    • 积分:1229
    • 等级:
    • 排名:千里之外
    • 原创:81篇
    • 转载:9篇
    • 译文:0篇
    • 评论:3条
    最新评论