poj3984迷宫问题 广搜+最短路径+模拟队列
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
Output
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
Sample Output
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
广度搜索还是老老实实用队列,可以采用模拟队列,自己记录队头和队尾,千万不要瞎试动态规划。。。。。。惨痛的教训。
输出路径的话可以反向追踪,从终点依次找到上一个点,直至原点。
参考代码:
#include<iostream>
using namespace std;
int map[][];
int vis[][];
struct node{
int x;
int y;
int pre;
}edge[];
int front=,rear=;//队头,队尾
int dir[][]={{,-},{,},{,},{-,}};
void f(int i)//倒向追踪法
{
if(edge[i].pre!=-)
{
f(edge[i].pre);
cout<<"("<<edge[i].x<<", "<<edge[i].y<<")"<<endl;
}
}
void BFS(int x,int y)
{
edge[front].x=x;
edge[front].y=y;
edge[front].pre=-;
while(front<rear)//队列为空时终止
{
int u;
for(u=;u<;u++)
{
int x=edge[front].x+dir[u][];
int y=edge[front].y+dir[u][];
if(x<||x>=||y<||y>=||vis[x][y]==||map[x][y]==)
continue;
else
{
vis[x][y]=;
map[x][y]=;
edge[rear].x=x;//入队
edge[rear].y=y;
edge[rear].pre=front;
rear++;
}
if(x==&&y==)
f(front);
}
front++;//出队
}
}
int main()
{
int i,j;
for(i=;i<;i++)
{
for(j=;j<;j++)
{
cin>>map[i][j];
}
}
memset(vis,,sizeof(vis));
cout<<"("<<"0, 0)"<<endl;
BFS(,);
cout<<"(4, 4)"<<endl;
return ;
}
poj3984迷宫问题 广搜+最短路径+模拟队列的更多相关文章
- POJ-3984-迷宫问题-BFS(广搜)-手写队列
题目链接:id=3984">http://poj.org/problem? id=3984 这个本来是个模板题,可是老师要去不能用STL里的queue,得自己手写解决.ORZ....看 ...
- poj 3083 Children of the Candy Corn (广搜,模拟,简单)
题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...
- HDU 1728 逃离迷宫 (广搜)
题目链接 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...
- 广搜最短路径变形,(POJ3414)
题目链接:http://poj.org/problem?id=3414 解题报告: 1.每个节点都是一个独立的状态 2.这里的状态转移就是有几种出路,4种:1.倒掉a中的水,2.把a中的水倒到b中去, ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- 队列&广搜
搜索里有深搜,又有广搜,而广搜的基础就是队列. 队列是一种特殊的线性表,只能在一段插入,另一端输出.输出的那一端叫做队头,输入的那一端叫队尾.是一种先进先出(FIFO)的数据结构. 正经的队列: 头文 ...
- 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...
- POJ3984 BFS广搜--入门题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20816 Accepted: 12193 Descriptio ...
随机推荐
- iOS 自动布局详细介绍
1. 自动布局的理解 iOS自动布局很有用,可以在不同size的屏幕上运行,原先看的头痛,还是习惯用最蠢的[UIScreen mainScreen].bounds.size.width等来布局,后来实 ...
- MyBatis入门(四)---动态SQL
一.创建数据库表 1.1.创建表 USE `mybatis`; /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; C ...
- Objective-C中常用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect
本节要点:红色标记 需要记下来 1 NSRange typedef struct _NSRange { NSUInteger location; NSUInteger length ...
- mysql登录和连接 权限
在一些配置中会要求登录mysql 授权的时候注意ip地址是ip地址,localhost是localhost,在grant授权时,如果用localhost,就必须在所登录的配置文件中使用localhos ...
- docker-2 深入了解docker
docker镜像.容器.仓库的基本概念 镜像 Docker 镜像就是一个只读的模板.例如:一个镜像可以包含一个完整的 CentOS 操作系统环境,里面仅安装了 httpd或用户需要的其它应用程序. 镜 ...
- 关于zend_parse_parameters函数
PHP_FUNCTION(set_time_limit) { long new_timeout; char *new_timeout_str; int new_timeout_strlen; if ( ...
- jacob 实现Office Word文件格式转换
关于jacob用法,百度一下就会发现几乎都是复制2004年一个代码,那段代码实现的是从一个目录读取所有doc文件,然后把它转html格式. 为了便习学习和使用,我把代码看懂后精简了一下,得出不少新结论 ...
- 启动rabbitmq web管理后台插件
安装完rabbitmq server之后,访问http://server_ip:15672/ 无法打开网页, 通过netstat -ano |grep 15672 查看此端口号并没有开启 需要启用 ...
- CentOS6.5上golang环境配置
CentOS6.5上golang环境配置 一.下载和解压go环境包 >>cd /usr/local/src/ >>wget -c http://golangtc.com/sta ...
- 关于点击空白关闭弹窗的js写法推荐
$(document).mouseup(function(e){ var _con = $(' 目标区域 '); // 设置目标区域 if(!_con.is(e.target) && ...