HDU-1240 Asteroids!解题报告(BFS)

假期第二天,继续学习。

题目描述

(很短但是看不懂)
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

看不懂?让我大概简述一下:每次输入包括字符串START和整数n,后面有n个层,每层为n*n的矩阵,O代表空,X代表不能不能通过。下面两行分别是起始的x y z和终点的x y z。你从起始点出发,找出要去终止点的最小步数,按照“n 步数”的格式输出,若没有,则输出“NO ROUTE”,每个输出占一行。

重点提要

这道题是一道比较普通的广搜。但是有一个特点是他的地图是三维,所以需要注意一点:如果你的输入是像我一样的:

for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				for(int k=0;k<n;k++)
				{
					cin>>mat[i][j][k];
				}
			}
		}

则注意:此时,你的i并不是代表x,而是代表z,而k代表x,j仍然是y。注意到这点,剩下的就不难了。

完整代码

#include <iostream>
#include <queue>
#include <map>
#include <cstring>
using namespace std;
int xx[]={1,-1,0,0,0,0};
int yy[]={0,0,1,-1,0,0};
int zz[]={0,0,0,0,1,-1};
typedef struct{
	int x,y,z,steps;
}condition;


int main()
{
	//freopen("out.txt","w",stdout);
	char single[100];
	while(~scanf("%s",single))
	{
		int n;
		cin>>n;
		bool vis[15][15][15]={0}; 
		char mat[15][15][15];
		memset(mat,'\0',sizeof(mat)); 
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				for(int k=0;k<n;k++)
				{
					cin>>mat[i][j][k];
				}
			}
		}
		int sx,sy,sz;
		int ex,ey,ez;
		cin>>sx>>sy>>sz>>ex>>ey>>ez;
		condition start={sx,sy,sz,0};
		queue<condition> Q;
		Q.push(start);
		int flag=0;
		//cout<<"*"<<"\n";
		while(!Q.empty())
		{
			
			//cout<<Q.front().x<<" "<<Q.front().y<<" "<<Q.front().z<<" "<<Q.front().steps<<endl;
			if(Q.front().x==ez&&Q.front().y==ey&&Q.front().z==ex)
			{
				flag=1;
				cout<<n<<" "<<Q.front().steps<<endl;
				//cout<<"*"<<"\n"; 
				break;
			}
			else
			{
				for(int i=0;i<6;i++)
				{
					int dx,dy,dz;
					dx=Q.front().x+xx[i];
					dy=Q.front().y+yy[i];
					dz=Q.front().z+zz[i];
					if(mat[dx][dy][dz]=='O'&&vis[dx][dy][dz]==0&&dx>=0&&dy>=0&&dz>=0&&dx<n&&dy<n&&dz<n)
					{
						vis[dx][dy][dz]=1;
						Q.push({dx,dy,dz,Q.front().steps+1});
					}
					
				}
				Q.pop();
			}
		}
		if(flag==0) cout<<"NO ROUTE"<<endl;
		scanf("%s",single);
	} 
	return 0;
}
发布了33 篇原创文章 · 获赞 1 · 访问量 928
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览