给出一个三维的迷宫以及起点和终点,求能否到大终点,若果能输出最短步数

三维的问题无非就是变成了6个搜索方向

最后强调一下xyz的顺序,从输入数据来看,读入的顺序是map[z][x][y]

总之,这是很基础的一道题

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; struct Point
{
char type;
int x, y, z;
int steps;
}start, end; char map[][][];
int dir[][] = {{,,}, {-,,}, {,,}, {,-,}, {,,}, {,,-}};
char buff[];
int n; bool islegal(int x, int y, int z)
{
return (x>= && x<n && y>= && y<n && z>= && z<n && map[z][x][y]!='X');
} void BFS(void)
{
queue<Point> qu;
start.steps = ;
qu.push(start);
while(!qu.empty())
{
Point now = qu.front();
if(now.x==end.x && now.y==end.y && now.z==end.z)
{
printf("%d %d\n", n, now.steps);
return;
}
for(int i = ; i < ; ++i)
{
int xx = now.x + dir[i][];
int yy = now.y + dir[i][];
int zz = now.z + dir[i][];
if(islegal(xx, yy, zz))
{
Point next;
next.x = xx, next.y = yy, next.z = zz, next.steps = now.steps + ;
map[zz][xx][yy] = 'X';
qu.push(next);
}
}
qu.pop();
}
printf("NO ROUTE\n");
} int main(void)
{
#ifdef LOCAL
freopen("1240in.txt", "r", stdin);
#endif while(cin >> buff >> n)
{
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j)
cin >> map[i][j]; cin >> start.x >> start.y >> start.z;
cin >> end.x >> end.y >> end.z;
cin >> buff;
BFS();
}
return ;
}

代码君

HDU 1240 (简单三维广搜) Asteroids!的更多相关文章

  1. HDU 1253 (简单三维广搜) 胜利大逃亡

    奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz 而且我也优化过了的啊,尼玛还是一直爆! 先把代码贴上睡觉去了,明天再来弄 //#define LOCAL #include <ios ...

  2. hdu 1240(三维广搜)

    题意: 有一个n*n*n的三维空间. 给你起始坐标和终点坐标.要你从起点到终点,问最少需要多少步走出去.如果走不出去则输出"NO ROUTE". 空间中 'O' 表示这个点可以走, ...

  3. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  4. UVa 1600 Patrol Robot(三维广搜)

    A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumn ...

  5. PAT L3-004 肿瘤诊断(三维广搜)

    在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积. 输入格式: 输入第一行给出4个正整数:M.N.L.T,其中M和N是每张切片的尺寸(即每张切片 ...

  6. 一道简单的广搜题:Knight Moves

    这本来是要用双向宽度搜索的,但是我用简单的广搜也成功了,L<=300,也不会超时?? 另外一个问题就是,我本来想用原来的代码交,结果80分??将边界条件从小于L改成小于等于L,就对了.我可能不会 ...

  7. hdu 1175 连连看 (广搜,注意解题思维,简单)

    题目 解析见代码 #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以广搜到的最短的路不一定是所要的路线 //所以应该把所有的路径都搜索出来,找到最短的转折数, ...

  8. Combine String HDU - 5707 dp or 广搜

    Combine String HDU - 5707 题目大意:给你三个串a,b,c,问a和b是不是恰好能组成c,也就是a,b是不是c的两个互补的子序列. 根据题意就可以知道对于c的第一个就应该是a第一 ...

  9. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

随机推荐

  1. Sqli-labs less 39

    Less-39 和less-38的区别在于sql语句的不一样:SELECT * FROM users WHERE id=$id LIMIT 0,1 也就是数字型注入,我们可以构造以下的payload: ...

  2. java基础知识回顾之javaIO类---FileInputStream和FileOutputStream字节流复制图片

    package com.lp.ecjtu; import java.io.FileInputStream; import java.io.FileNotFoundException; import j ...

  3. C# WINFORM 捕获全局异常

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Thr ...

  4. CamShift算法

    拟采用的方法,CamShift算法,即"Continuously Apative Mean-Shift"算法,是一种运动跟踪算法.它主要通过视频图像中运动物体的颜色信息来达到跟踪的 ...

  5. JDK环境变量解析

    设置环境变量 在java 中需要设置三个环境变量(1.5之后不用再设置classpath了,但个人强烈建议继续设置以保证向下兼用问题)JDK安装完成之后我们来设置环境变量:右击“我的电脑”,选择“属性 ...

  6. C# 使用WIN32API设置外部程序窗口无边框

    使用代码 var wnd = win32.FindWindowA(null, "窗口标题"); Int32 wndStyle = win32.GetWindowLong(wnd, ...

  7. RxJava+Retrofit+MVP构建的App——聚合资讯

    RtfRxMVP 聚合资讯APP,提供热点资讯,天气预报以及笑话精选服务,使用 Retrofit + RxJava + MVP 构建代码. Hello U 这是我的一个练习项目,第一次尝试运用 MVP ...

  8. Linear Regression

    大学时候学物理实验的时候接触过线性回归,现在忘记了...还得重新拾起来.学习不扎实耽误了多少时光... sigh Suppose that you time a program as a functi ...

  9. 293. Flip Game

    题目: You are playing the following Flip Game with your friend: Given a string that contains only thes ...

  10. HighCharts开发说明

    一.HighCharts开发说明: HighCharts开发实际上配置HighCharts每个部分,比如配置标题(title),副标题(subtitle)等,其中每个部分又有更细的参数配置,比如标题下 ...