立体最短路径,广搜(POJ2251)
题目链接:http://poj.org/problem?id=2251
参考了一下大神们的解法。也有用深搜的。然而,之前不久看到一句话,最短路径——BFS。
参考:http://blog.csdn.net/lyy289065406/article/details/6647938
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue> using namespace std; char map[][][];///存放地图
bool hash[][][];///这条路是否走过 struct point{
int x; ///l
int y; ///r
int z; ///c
int step;///移动的步数
}; int main()
{
int l,r,c;
while(scanf("%d%d%d",&l,&r,&c),l&&r&&c)
{
int i,j,k;
point start; memset(hash,false,sizeof(hash)); for(i=;i<l;i++)
{
for(j=;j<r;j++)
{
scanf("%s",map[i][j]);
for(k=;k<c;k++)
{
if(map[i][j][k]=='S')
{
///对起点初始化
start.x=i;
start.y=j;
start.z=k;
start.step=; hash[i][j][k]=true;///这条路走过了
}
}
}
} queue<point> bfs; bfs.push(start);///入队 bool found=false; ///是否找到最优解 while(!bfs.empty())
{
point tmp=bfs.front(); bfs.pop(); if(map[tmp.x][tmp.y][tmp.z]=='E')
{
found=true;
printf("Escaped in %d minute(s).\n",tmp.step);
break;
} tmp.step++; point tmp2; if(tmp.x>) ///向下走
{
tmp2=tmp;
tmp2.x--;
if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
{
///如果下一层不是障碍物并且没走过,则往下走,标记该点,入队
hash[tmp2.x][tmp2.y][tmp2.z]=true;
bfs.push(tmp2);
}
} ///向上走
if(tmp.x<l-)
{
tmp2=tmp;
tmp2.x++;
if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
{
hash[tmp2.x][tmp2.y][tmp2.z]=true;
bfs.push(tmp2);
}
} ///向北走
if(tmp.y>)
{
tmp2=tmp;
tmp2.y--;
if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
{
hash[tmp2.x][tmp2.y][tmp2.z]=true;
bfs.push(tmp2);
}
} ///向南走
if(tmp.y<r-)
{
tmp2=tmp;
tmp2.y++;
if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
{
hash[tmp2.x][tmp2.y][tmp2.z]=true;
bfs.push(tmp2);
}
} ///向西走
if(tmp.z>)
{
tmp2=tmp;
tmp2.z--;
if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
{
hash[tmp2.x][tmp2.y][tmp2.z]=true;
bfs.push(tmp2);
}
} ///向东走
if(tmp.z<c-)
{
tmp2=tmp;
tmp2.z++;
if(map[tmp2.x][tmp2.y][tmp2.z]!='#'&&hash[tmp2.x][tmp2.y][tmp2.z]==false)
{
hash[tmp2.x][tmp2.y][tmp2.z]=true;
bfs.push(tmp2);
}
} }
if(!found)
printf("Trapped!\n"); }
return ;
}
立体最短路径,广搜(POJ2251)的更多相关文章
- 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- nyoj 523 双向广搜
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...
- 解救小哈——bfs广搜
问题描述: 小哈去玩迷宫,结果迷路了,小哼去救小哈.迷宫由n行m列的单元格组成(n和m都小于等于50),每个单元格要么是空地,要么是障碍物. 问题:帮小哼找到一条从迷宫的起点通往小哈所在位置的最短路径 ...
- hdu 1253 胜利大逃亡 (广搜)
题目链接 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个ABC的立方体,可以被表示成A个 ...
- 69.广搜练习: 最少转弯问题(TURN)
[问题描述] 给出一张地图,这张地图被分为n×m(n,m<=100)个方块,任何一个方块不是平地就是高山.平地可以通过,高山则不能.现在你处在地图的(x1,y1)这块平地,问:你至少需要拐几个弯 ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- java TestNG测试报告美化
测试报告 执行完测试用例之后,会在项目的test-output(默认目录)下生成测试报告 打开index.html文件,测试结果摘要,包括:套件名.测试用例成功数.测试用例失败数.测试用例忽略数和te ...
- ansible部署,规划
部署管理服务器 第一步:先检查有没有ssh服务 [root@iZm5eeyc1al5vzh8bo57zyZ ~]# rpm -qf /etc/init.d/sshd openssh-server-5. ...
- oracle wm_concat函数 列转行 分组函数
(1)select mark, wm_concat(status) from DISSENT_INFO t GROUP BY mark; 查出来的数据 mark status 222 ...
- 一行CMD命令kill(杀)掉你的进程
查看进程 pi@raspberry:~ $ ps -ef | grep python3 UID PID PPID C STIME TTY TIME CMD pi 4678 4666 0 11:57 p ...
- c++从txt中读取数据,数据并不是一行路径(实用)
#include <iostream>#include <fstream>#include <string> using namespace std; //输出空行 ...
- 6-----BBS论坛
BBS论坛(六) 6.1.优化json数据的返回 (1)新建utils/restful.py # utils/restful.py from flask import jsonify class Ht ...
- hadoop - hbase 单机版的安装部署
1. 下载安装包. http://www.apache.org/dyn/closer.lua/hbase/ 选择版本,单机进入以下页面 2. tar xfz hbase-1.2.6-bin.tar.g ...
- SpringBoot初始教程之Servlet、Filter、Listener配置
1.介绍通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个jar ...
- 2019.03.19 读书笔记 string与stringbuilder的性能
1 string与stringbuilder 并不是stringbuilder任何时候都在性能上占优势,在少量(大约个位数)的字符串时,并不比普通string操作快. string慢的原因不是stri ...
- maya 安装失败
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...