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 ...
随机推荐
- CoreAnimation(CA)
开发者真会玩,原来我看到CA都懵了.啥是CA?原来就是Core Animation.哎,读书少啊,被虐成
- 揭开智能配置上网(微信Airkiss)的神秘面纱
本文介绍微信利用Airkiss技术对wifi设备进行智能配置上网的场景,并分析其实现的原理.这里再次说明,Airkiss只是用于配置上网,其跟微信硬件平台的通信流程和接入协议规范完全没有关系.一个wi ...
- 演示 pull解析的基本步骤(代码演示)
pull解析器: * 反序列化:将xml中的数据取出 1.导入jar包 2.创建解析器工厂 ...
- HTML列表元素
HTML定义了3类列表: 1.有序列表(通常用数字编号) 2.无序列表(通常前面加原点) 3.自定义列表(列表项目,带有集成的定义) 有序列表和无序列表均为列表中的每一项使用列表项元素(<li& ...
- CocoaPods的使用(图文并茂)OS X 10.11 系统
系统:OS X EI Capitan 版本:10.11.2 开发工具:XCode:7.2 要使用CocoaPods,那么就需要先安装哦,你安装了么?如果没安装那就请阅读我的前篇<OS X 10. ...
- python初始化父类错误
源码如下: #!/usr/bin/env python class Bird(): def __init__(self): self.hungry = True def eat(self): if s ...
- img标签使用默认图片的一种方式
基于html5提供的onerror这个时间属性.
- debian和ubuntu的sh dash bash
Ubuntu和debian 的 shell 默认安装的是 dash,而不是 bash.运行以下命令查看 sh 的详细信息,确认 shell 对应的程序是哪个:$ls -al /bin/sh dash ...
- Effective Java 21 Use function objects to represent strategies
Theory In the Java the function pointers is implemented by the declaring an interface to represent s ...
- leveldb源码分析—Recover和Repair
leveldb作为一个KV存储引擎将数据持久化到磁盘,而对于一个存储引擎来说在存储过程中因为一些其他原因导致程序down掉甚至数据文件被破坏等都会导致程序不能按正常流程再次启动.那么遇到这些状况以后如 ...