hdu 1240 Asteroids! (三维bfs)
Asteroids!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2599 Accepted Submission(s): 1745
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.
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
3 4
NO ROUTE
zf
题意:三维迷宫。从起点到终点的最少步数。如果不能走到终点,输出“NO ROUTE”。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std; struct node
{
int x,y,z;
int steps;
}start,endd,next;
int dx[6]={0,0,0,0,1,-1}; //上下左右前后
int dy[6]={0,0,-1,1,0,0};
int dz[6]={1,-1,0,0,0,0};
char maps[15][15][15];
int n,res; bool cango(node &a)
{
if(a.x>=0&&a.x<n&&a.y>=0&&a.y<n&&a.z>=0&&a.z<n)
return true;
else return false;
} int bfs()
{
if(start.x==endd.x&&start.y==endd.y&&start.z==endd.z)
return res;
node cur;
queue<node> q;
while(!q.empty())
q.pop();
q.push(start);
maps[start.x][start.y][start.z]='X';
while(!q.empty())
{
cur=q.front();
q.pop();
for(int i=0;i<6;i++)
{
next.x=cur.x+dx[i];
next.y=cur.y+dy[i];
next.z=cur.z+dz[i];
if(next.x==endd.x&&next.y==endd.y&&next.z==endd.z) //
return cur.steps+1; //
if(maps[next.x][next.y][next.z]=='O'&&cango(next))
{
//printf("test: %d %d %d\n",next.x,next.y,next.z);
next.steps=cur.steps+1;
maps[next.x][next.y][next.z]='X';
q.push(next);
}
}
}
return -1;
} int main()
{
int i,j;
char st[6],ed[6];
while(scanf("%s%d",&st,&n)!=EOF)
{
getchar();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%s",maps[i][j]);
getchar();
}
scanf("%d%d%d",&start.x,&start.y,&start.z);
scanf("%d%d%d",&endd.x,&endd.y,&endd.z);
getchar();
gets(ed);
res=0;
start.steps=0;
res=bfs();
if(res>=0) printf("%d %d\n",n,res);
else printf("NO ROUTE\n");
}
return 0;
}
hdu 1240 Asteroids! (三维bfs)的更多相关文章
- hdu 1240 Asteroids!(BFS)
题目链接:点击链接 简单BFS,和二维的做法相同(需注意坐标) 题目大意:三维的空间里,给出起点和终点,“O”表示能走,“X”表示不能走,计算最少的步数 #include <iostream&g ...
- HDU 1240 Asteroids!【BFS】
题意:给出一个三维的空间,给出起点和终点,问是否能够到达终点 和上一题一样,只不过这一题的坐标是zxy输入的, 因为题目中说的是接下来的n行中分别是由n*n的矩形组成的,所以第一个n该是Z坐标,n*n ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1240 Asteroids! 题解
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1240——Asteroids!(三维BFS)POJ 2225——Asteroids
普通的三维广搜,须要注意的是输入:列,行,层 #include<iostream> #include<cstdio> #include<cstring> #incl ...
- HDU 1240 (简单三维广搜) Asteroids!
给出一个三维的迷宫以及起点和终点,求能否到大终点,若果能输出最短步数 三维的问题无非就是变成了6个搜索方向 最后强调一下xyz的顺序,从输入数据来看,读入的顺序是map[z][x][y] 总之,这是很 ...
- HDU 1240 Asteroids!(BFS)
题目链接 Problem Description You're in space.You want to get home.There are asteroids.You don't want to ...
- HDU 1240 Asteroids! 解题报告
//这道题做完我只有 三个感受 第一:坑: 第二 : 坑! 第三:还是坑! 咳咳 言归正传 WA了无数次之后才发现是输入进去时坐标时z, y, x的顺序输入的 题解 : 类似胜利大逃亡 只 ...
- HDU 1240 Asteroids!
三维广搜 #include <cstdio> #include <iostream> #include <cstring> #include <queue&g ...
随机推荐
- Append加载动态轮播
前几天遇到了些小麻烦,不过很快就解决了.之所以要记下来是因为作为一名前端的程序员,要理解页面的加载顺序是最重要的.要不然自己写程序意外的出现bug~~ 刚开始写利用Append的时候,利用火狐的fir ...
- (转)ASP.NET MVC 学习第一天
天道酬勤0322 博客园 | 首页 | 发新随笔 | 发新文章 | 联系 | 订阅 | 管理 随笔:10 文章:0 评论:9 引用:0 ASP.NET MVC 学习第一天 今天开始第一天学习as ...
- Java语言实现简单FTP软件------>FTP软件效果图预览之下载功能(二)
介绍完FTP协议后,来看看该软件完成后的效果图 客户端的主界面如上图所示,主要是分为以下几个界面: 数据输入界面:用来让用户输入服务器的地址,用户名,密码,端口号等. 站点菜单.本地菜单.远程菜单以及 ...
- 文成小盆友python-num10 socketserver 原理相关。
本节主要内容: 1.IO多路复用 2.多线程多进程 3.小知识点补充(python中作用域相关) 4.socketserver源码分析补充 一.IO多路复用 I/O多路复用指:通过一种机制,可以监视多 ...
- Membership添加验证码登录
1.在公共类ImageHelper中编写公共方法,产生随机验证码 /// <summary> /// 产生随机验证码 /// </summary> /// <return ...
- m2eclipse插件安装
一.给Eclipse安装maven的插件 m2eclipse 1 打开eclipse 2 Help -->Eclipse MarketPlace,在打开的界面搜索框中输入maven查找m2ecl ...
- Builder模式的几个要点
1.Builder模式主要用于“分步骤构建一个复杂的对象”.在这其中“分步骤”是一个稳定的算法,而复杂对象的各个部分则经常变化.2.变化点在哪里,封装哪里——Builder模式主要在于应对“复杂对象各 ...
- bat命令大全
一.简单批处理内部命令简介 1.Echo 命令 打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当前回显设置. 语法 echo [{on│off}] [message ...
- Some General concepts in ISO C
[ISO C11 Clause 3]对象(object):执行环境中数据存储的一块区域,它的内容可以用来表示值.-注释:对象可以具有特定的类型.--值(value):确定类型的对象的内容的确切含义.- ...
- Android XML文档解析(一)——SAX解析
---------------------------------------------------------------------------------------------------- ...