Asteroids!(三维BFS)
来源:互联网 发布:移动云商城 源码下载 编辑:程序博客网 时间:2024/05/01 18:30
LINK:http://acm.hdu.edu.cn/showproblem.php?pid=1240
Problem:
Asteroids!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3506 Accepted Submission(s): 2318
You want to get home.
There are asteroids.
You don't want to hit them.
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.
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.
START 1O0 0 00 0 0ENDSTART 3XXXXXXXXXOOOOOOOOOXXXXXXXXX0 0 12 2 1ENDSTART 5OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXXXXXXXXXXXXXXXXXXXXXXXXXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO0 0 04 4 4END
1 03 4NO ROUTE
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
char map[11][11][11];
int vis[11][11][11],n;
int sx,sy,sz,ex,ey,ez;
int d[6][3]={{0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0}};
struct node
{
int x;
int y;
int z;
int sp;
};
queue<node>Q;
int bfs()
{
int i,xi,yi,zi;
node now,next;
now.x=sx;
now.y=sy;
now.z=sz;
now.sp=0;
Q.push(now);
vis[sx][sy][sz]=1;
while(!Q.empty())
{
now=Q.front();
if(now.x==ex&&now.y==ey&&now.z==ez)
return now.sp;
Q.pop();
for(i=0;i<6;i++)
{
xi=now.x+d[i][0];
yi=now.y+d[i][1];
zi=now.z+d[i][2];
if(xi>=0&&xi<n&&yi>=0&&yi<n&&zi>=0&&zi<n&&map[xi][yi][zi]=='O'&&!vis[xi][yi][zi])
{
vis[xi][yi][zi]=1;
next.sp=now.sp+1;
next.x=xi;
next.y=yi;
next.z=zi;
Q.push(next);
}
}
}
return -1;
}
int main()
{
char s[20];
int i,j,k,fg;
while(scanf("%s %d",s,&n)!=EOF)
{
for(k=0;k<n;k++)
for(i=0;i<n;i++)
{
getchar();
for(j=0;j<n;j++)
scanf("%c",&map[i][j][k]);
}
scanf("%d%d%d",&sx,&sy,&sz);
scanf("%d%d%d",&ex,&ey,&ez);
scanf("%s",s);
memset(vis,0,sizeof(vis));
while(!Q.empty())
Q.pop();
fg=bfs();
if(fg==-1)
printf("NO ROUTE\n");
else
printf("%d %d\n",n,fg);
}
return 0;
}
附上网上poj AC代码,求大牛们解释下上面我的代码与这代码的区别,我不懂为什么这样能过,上面代码却在poj过不了。。。
#include<stdio.h>
#include<queue>
#include<limits.h>
using namespace std;
typedef struct n
{
int x,y,z,times;
}Node;
char mass[11][11][11],c1[11];
int flag[11][11][11],N,SC,SH,SL,EH,EL,EC,tot,dic[6][3]={{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{0,0,1},{0,0,-1}};
void BFS()
{
Node n1,n2;
queue<Node>qu;
int a,b,c,i,j,k;
n1.x=SH,n1.y=SL,n1.z=SC,n1.times=0;
flag[SC][SH][SL]=1;
qu.push(n1);
memset(flag,0,sizeof(flag));
while(!qu.empty())
{
n1=qu.front();
for(i=0;i<6;++i)
{
n2.x=n1.x+dic[i][0];n2.y=n1.y+dic[i][1];n2.z=n1.z+dic[i][2];n2.times=n1.times+1;
if(flag[n2.z][n2.x][n2.y]||n2.x<0||n2.y<0||n2.z<0||n2.x>=N||n2.y>=N||n2.z>=N)//越界了或者是访问过了
{
continue;
}
if(n2.x==EH&&n2.y==EL&&n2.z==EC)
{
printf("%d %d\n",N,n2.times);
return;
}
if(mass[n2.z][n2.x][n2.y]=='X')
continue;
flag[n2.z][n2.x][n2.y]=1;
qu.push(n2);
}
qu.pop();
}
printf("NO ROUTE\n");
}
int main()
{
int a,b,c,i,j,k;
while(scanf("%s%d",c1,&N)!=EOF)
{
tot=INT_MAX;
for(i=0;i<N;++i)
{
for(j=0;j<N;++j)
scanf("%s",mass[i][j]);
}
scanf("%d%d%d%d%d%d",&SL,&SH,&SC,&EL,&EH,&EC);
scanf("%s",c1);
if(SL==EL&&SH==EH&&SC==EC)
{
printf("%d %d\n",N,0);
continue;
}
BFS();
}
}
- Asteroids!(三维BFS)
- Hdu1240 Asteroids!(三维BFS)
- HDU 2225 Asteroids!(三维BFS)
- hdu1240 Asteroids!(三维bfs水)
- HDU 1240 Asteroids! (三维BFS)
- HDU 1240 Asteroids! (三维BFS)
- HDOJ 1240 Asteroids!(三维BFS)
- hdu 1240 Asteroids!(三维bfs)
- hdu 1240 Asteroids! (三维bfs)
- HDU1240 Asteroids!-题目意思详解( 三维BFS)
- HDU 1240——Asteroids!(三维BFS)POJ 2225——Asteroids
- HDU1240(BFS)Asteroids!
- Asteroids!(bfs)
- ACM-三维BFS之Asteroids!——hdu1240
- POJ2225 Asteroids! (三维的BFS求最短路)
- hdu 1240 poj 2225 Asteroids! 三维bfs 解题报告
- hdu 1240 Asteroids!(BFS)
- HDU-#1240 Asteroids!(BFS)
- 计算机笔试/面试题
- RTX 实时操作系统简介
- 红黑树
- Cocos2d3x环境搭建参考博客
- JAVA大数值问题总结
- Asteroids!(三维BFS)
- 三分钟学会 JavaScript 单元测试
- 为什么选择RTX
- POJ 3261 SA
- 单片机小白学步系列(六) 单片机最小系统——麻雀虽小,五脏俱全
- HDU - 5038 Grade
- Handler将Runnable放入线程队列,removecallback()移除Runnable
- C++ primer 第十六章 继承构造函数
- 数据库命名规范