hdu 1240 Asteroids!bfs
2014-04-10 00:37
375 查看
hdu 1240 Asteroids!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3101 Accepted Submission(s): 2069
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 formattedaccording to the following description, and there will be no blank linesseparating data sets.
A single data set has 5 components:
Start line - A single line, "STARTN", where 1 <= N <= 10.
Slice list - A series of N slices. Eachslice is an N x N matrix representing a horizontal slice through the asteroidfield. Each position in the matrix will be one of two values:
'O' - (the letter "oh") Emptyspace
'X' - (upper-case) Asteroid present
Starting Position - A single line, "AB C", denoting the <A,B,C> coordinates of your craft's startingposition. The coordinate values will be integers separated by individualspaces.
Target Position - A single line, "D EF", denoting the <D,E,F> coordinates of your target's position. Thecoordinate 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 aninteger between 0 and N-1, inclusive.
The first coordinate in a set indicates thecolumn. Left column = 0.
The second coordinate in a set indicatesthe row. Top row = 0.
The third coordinate in a set indicates theslice. First slice = 0.
Both the Starting Position and the TargetPosition will be in empty space.
Output
For each data set, there will be exactlyone output set, and there will be no blank lines separating output sets.
A single output set consists of a singleline. If a route exists, the line will be in the format "X Y", whereX is the same as N from the corresponding input data set and Y is the leastnumber of moves necessary to get your ship from the starting position to
thetarget position. If there is no route from the starting position to the targetposition, the line will be "NO ROUTE" instead.
A move can only be in one of the six basicdirections: up, down, left, right, forward, back. Phrased more precisely, a movewill either increment or decrement a single component of your current positionvector 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
题解:
三维dfs,比较特别的是图标是一层一层的展开的,如果想要记录成map[x][y][z]的话,那么三层for循环第一层是z坐标,第二层是x,第三层是y。而给出的起始坐标和终止坐标则是按(y,x,z)给的,这个地方要记住转化。一共有六个方向,dir[6][3]用来记录。两个输出一个是输出n的值,第二个是输出从起点到终点的距离,典型的bfs。
源代码:
#include
<iostream>
#include
<stdio.h>
#include
<string>
#include
<string.h>
#include
<queue>
using namespacestd;
struct
node
{
intx,y,z;
}start,ed;
char map[11][11][11];
int n;
int visit[11][11][11];
int dir[6][3] ={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
bool judge(intx,inty,intz)
{
if(x>= n||x <0||y>= n||y < 0||z>= n||z
< 0)
returnfalse;
if(map[x][y][z]==
'X')
returnfalse;
if(visit[x][y][z]< 100000)
returnfalse;
returntrue;
}
void bfs()
{
queue<node>q;
visit[start.x][start.y][start.z]= 0;
q.push(start);
node tt,tp;
while(!q.empty())
{
tp= q.front();
q.pop();
for(inti = 0;i < 6;i++)
{
tt.x= tp.x + dir[i][0];
tt.y= tp.y + dir[i][1];
tt.z= tp.z + dir[i][2];
if(!judge(tt.x,tt.y,tt.z))
continue;
visit[tt.x][tt.y][tt.z]= visit[tp.x][tp.y][tp.z] + 1;
if(tt.x== ed.x&&tt.y == ed.y && tt.z == ed.z)
return;
q.push(tt);
}
}
}
int main()
{
charstr[10];
while(scanf("%s%d",str,&n)!=EOF)
{
charc = getchar();
for(inti = 0;i < n;i++)
for(intj = 0;j < n;j++)
{
for(intk = 0;k < n;k++)
{
scanf("%c",&map[j][k][i]);
visit[j][k][i]= 100000;
}
c= getchar();
}
scanf("%d%d%d",&start.y,&start.x,&start.z);
scanf("%d%d%d",&ed.y,&ed.x,&ed.z);
scanf("%s",str);
getchar();
if(ed.x== start.x && ed.y == start.y && ed.z == start.z)
printf("%d %d\n",n,0);
elseif(map[start.x][start.y][start.z]==
'X'||map[ed.x][ed.y][ed.z] ==
'X')
printf("NO ROUTE\n");
else
{
bfs();
if(visit[ed.x][ed.y][ed.z]< 100000)
printf("%d %d\n",n,visit[ed.x][ed.y][ed.z]);
else
printf("NO ROUTE\n");
}
}
return0;
}
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3101 Accepted Submission(s): 2069
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 formattedaccording to the following description, and there will be no blank linesseparating data sets.
A single data set has 5 components:
Start line - A single line, "STARTN", where 1 <= N <= 10.
Slice list - A series of N slices. Eachslice is an N x N matrix representing a horizontal slice through the asteroidfield. Each position in the matrix will be one of two values:
'O' - (the letter "oh") Emptyspace
'X' - (upper-case) Asteroid present
Starting Position - A single line, "AB C", denoting the <A,B,C> coordinates of your craft's startingposition. The coordinate values will be integers separated by individualspaces.
Target Position - A single line, "D EF", denoting the <D,E,F> coordinates of your target's position. Thecoordinate 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 aninteger between 0 and N-1, inclusive.
The first coordinate in a set indicates thecolumn. Left column = 0.
The second coordinate in a set indicatesthe row. Top row = 0.
The third coordinate in a set indicates theslice. First slice = 0.
Both the Starting Position and the TargetPosition will be in empty space.
Output
For each data set, there will be exactlyone output set, and there will be no blank lines separating output sets.
A single output set consists of a singleline. If a route exists, the line will be in the format "X Y", whereX is the same as N from the corresponding input data set and Y is the leastnumber of moves necessary to get your ship from the starting position to
thetarget position. If there is no route from the starting position to the targetposition, the line will be "NO ROUTE" instead.
A move can only be in one of the six basicdirections: up, down, left, right, forward, back. Phrased more precisely, a movewill either increment or decrement a single component of your current positionvector 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
题解:
三维dfs,比较特别的是图标是一层一层的展开的,如果想要记录成map[x][y][z]的话,那么三层for循环第一层是z坐标,第二层是x,第三层是y。而给出的起始坐标和终止坐标则是按(y,x,z)给的,这个地方要记住转化。一共有六个方向,dir[6][3]用来记录。两个输出一个是输出n的值,第二个是输出从起点到终点的距离,典型的bfs。
源代码:
#include
<iostream>
#include
<stdio.h>
#include
<string>
#include
<string.h>
#include
<queue>
using namespacestd;
struct
node
{
intx,y,z;
}start,ed;
char map[11][11][11];
int n;
int visit[11][11][11];
int dir[6][3] ={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
bool judge(intx,inty,intz)
{
if(x>= n||x <0||y>= n||y < 0||z>= n||z
< 0)
returnfalse;
if(map[x][y][z]==
'X')
returnfalse;
if(visit[x][y][z]< 100000)
returnfalse;
returntrue;
}
void bfs()
{
queue<node>q;
visit[start.x][start.y][start.z]= 0;
q.push(start);
node tt,tp;
while(!q.empty())
{
tp= q.front();
q.pop();
for(inti = 0;i < 6;i++)
{
tt.x= tp.x + dir[i][0];
tt.y= tp.y + dir[i][1];
tt.z= tp.z + dir[i][2];
if(!judge(tt.x,tt.y,tt.z))
continue;
visit[tt.x][tt.y][tt.z]= visit[tp.x][tp.y][tp.z] + 1;
if(tt.x== ed.x&&tt.y == ed.y && tt.z == ed.z)
return;
q.push(tt);
}
}
}
int main()
{
charstr[10];
while(scanf("%s%d",str,&n)!=EOF)
{
charc = getchar();
for(inti = 0;i < n;i++)
for(intj = 0;j < n;j++)
{
for(intk = 0;k < n;k++)
{
scanf("%c",&map[j][k][i]);
visit[j][k][i]= 100000;
}
c= getchar();
}
scanf("%d%d%d",&start.y,&start.x,&start.z);
scanf("%d%d%d",&ed.y,&ed.x,&ed.z);
scanf("%s",str);
getchar();
if(ed.x== start.x && ed.y == start.y && ed.z == start.z)
printf("%d %d\n",n,0);
elseif(map[start.x][start.y][start.z]==
'X'||map[ed.x][ed.y][ed.z] ==
'X')
printf("NO ROUTE\n");
else
{
bfs();
if(visit[ed.x][ed.y][ed.z]< 100000)
printf("%d %d\n",n,visit[ed.x][ed.y][ed.z]);
else
printf("NO ROUTE\n");
}
}
return0;
}
相关文章推荐
- hdu 1240 Asteroids! (BFS)
- HDU 1240 Asteroids!(BFS)
- HDU 1240 Asteroids! (三维BFS)
- HDU 1240——Asteroids!(三维BFS)POJ 2225——Asteroids
- HDU 1240 Asteroids! 【BFS】
- hdu 1240:Asteroids!(三维BFS搜索)
- HDU 1240 Asteroids! (三维BFS)
- HDU 1240 Asteroids!(BFS)
- POJ 2225 && HDU 1240 Asteroids!(bfs)
- hdu 1240 Asteroids!【BFS】
- HDU - 1240 Asteroids!(BFS)
- hdu1240 Asteroids! (BFS)
- hdu1240 Asteroids!--DFS & BFS
- HDU 1240 Asteroids! 三维空间BFS
- hdu 1240 Asteroids!(bfs)
- HDU-1240 Asteroids! BFS
- HDU-#1240 Asteroids!(BFS)
- hdu 1240 Asteroids!(BFS)
- HDU 1240 Asteroids!(BFS)
- hdu 1240 Asteroids! 三维 BFS