您的位置:首页 > 其它

hdu 1240 Asteroids!(bfs)

2015-08-24 15:09 323 查看

Asteroids!

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


[align=left]Problem Description[/align]
You're in space.
You want to get home.
There are asteroids.
You don't want to hit them.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

1 0
3 4
NO ROUTE

[align=left]Source[/align]
South Central USA 2001

[align=left]Recommend[/align]
zf | We have carefully selected several similar problems for you: 1072 1372 1180 1239 1254

这道题的错了好久,一直过不了测试数据,后来发现它的迷宫输入有所不同,必须先输入第一层,也就是说z坐标轴等于0,等输入完第一层再将z坐标轴变为1。也就是说z坐标轴变化的要最慢,放在第一层循环。所以做题一定要细心。

题意:先输入一行START N,“START”为字符串,表示开始,忽略即可。N是地图大小,为N*N*N。然后输入N*N行,每行N个字符,每N行表示一层,有N层,O表示可以走,X表示不能,再输入起点和终点,能走到,输出n和步数,不能,则输出“NO ROUTE”

附上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int f[6][3]= {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};  //6个方向的模拟
struct node
{
int x,y,z,t;
} s1,s2;
int n,a1,b1,c1,a2,b2,c2;
int visit[15][15][15];
char map[15][15][15];

void BFS()
{
queue <node> q;
while(!q.empty())
q.pop();
s1.x=c1;   //注意这里的变化!!
s1.y=b1;
s1.z=a1;
s1.t=0;
visit[c1][b1][a1]=0;
q.push(s1);
while(!q.empty())
{
s1=q.front();
q.pop();
if(s1.x==c2&&s1.y==b2&&s1.z==a2)
{
printf("%d %d\n",n,s1.t);
return;
}
for(int i=0; i<6; i++)
{
s2.x=s1.x+f[i][0];
s2.y=s1.y+f[i][1];
s2.z=s1.z+f[i][2];
if(s2.x>=0&&s2.x<n&&s2.y>=0&&s2.y<n&&s2.z>=0&&s2.z<n&&visit[s2.x][s2.y][s2.z])
{
//cout<<s2.x<<" "<<s2.y<<" "<<s2.z<<endl;
visit[s2.x][s2.y][s2.z]=0;
s2.t=s1.t+1;
q.push(s2);
}
}
}
printf("NO ROUTE\n");
return;
}

int main()
{
char str[10];
int k,i,j;
while(~scanf("%s %d",str,&n))
{
memset(visit,0,sizeof(visit));
for(i=0; i<n; i++)
for(j=0; j<n; j++)
for(k=0; k<n; k++)
{
cin>>map[i][j][k];
if(map[i][j][k]=='O')
visit[i][j][k]=1;
}
scanf("%d %d %d",&a1,&b1,&c1);  //起点
scanf("%d %d %d",&a2,&b2,&c2);   //终点
scanf("%s",str);     //别忘了还有一个字符串的输入
BFS();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: