poj2225Asteroids!(三维BFS)

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

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.//最后才是高

#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));
vis[st.z][st.x][st.y]=1;
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;

}

• 本文已收录于以下专栏：

举报原因： 您举报文章：poj2225Asteroids!(三维BFS) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)