【bfs】 poj 3984 maze 队列存储
#include <iostream>
#include <stdio.h>
#include <cstring>
#define Max 0x7f7f7f7f
using namespace std; int map[][];
int visited[][];
int dir[][]={{,},{-,},{,-},{,}};
int ans[][];
int pre[];
struct node
{
int x;
int y;
};
node path[]; void output(int head)
{
int tmp=pre[head];
if(tmp==)
{
printf("(%d, %d)\n",path[tmp].x,path[tmp].y);
}
else
{
output(tmp);
}
printf("(%d, %d)\n",path[head].x,path[head].y);
}
void bfs()
{
memset(visited,,sizeof(visited));
memset(ans,Max,sizeof(ans));
int head=;
int tail=;
pre[head]=-;
path[head].x=;
path[head].y=;
visited[path[head].x][path[head].y]=;
while(head<tail)
{
int x=path[head].x;
int y=path[head].y;
if(x== && y==)
{
output(head);
return ;
}
for(int i=;i<;i++)
{
int xx=x+dir[i][];
int yy=y+dir[i][];
if(visited[xx][yy]== && xx>= && xx< && yy>= && yy< && map[xx][yy]!=)
{
path[tail].x=xx;
path[tail].y=yy;
pre[tail]=head;
tail++;
visited[xx][yy]=;
}
}
head++;
}
}
int main()
{
// freopen("in.txt","r",stdin);
int i,j;
for(i=;i<;i++)
{
for(j=;j<;j++)
{
scanf("%d",&map[i][j]);
}
}
bfs();
return ;
}
【bfs】 poj 3984 maze 队列存储的更多相关文章
- 搜索 || BFS || POJ 2157 Maze
走迷宫拿宝藏,拿到所有对应的钥匙才能开门 *解法:从起点bfs,遇到门时先放入队列中,取出的时候看钥匙够不够决定开不开门,如果不够就把它再放回队列继续往下走,当队列里只有几个门循环的时候就可以退出,所 ...
- Q - 迷宫问题 POJ - 3984(BFS / DFS + 记录路径)
Q - 迷宫问题 POJ - 3984 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- BFS(最短路+路径打印) POJ 3984 迷宫问题
题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...
- POJ 3984 迷宫问题
K - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 3984(DFS入门题 +stack储存路径)
POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- Java队列存储结构及实现
一.队列(Queue) 队列是一种特殊的线性表,它只允许在表的前段(front)进行删除操作,只允许在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头. 对于一个队 ...
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- POJ 3984 迷宫问题【BFS/路径记录/手写队列】
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义 ...
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
随机推荐
- 5.Hibernate实现全套增删改查和ajax异步分页
1.1 创建如下oracle数据库脚本 drop sequence seq_stu; create sequence SEQ_STU minvalue maxvalue start increment ...
- 移动端Bug管理工具——Bugtags
Bugtags介绍 产品说明 Bugtags是为改善移动产品质量而专门打造的测试平台产品. 产品功能 SDK集成简单 一行代码极速集成,完全不影响原有程序结构. 所见即所得提交问题 一键截屏,使用标签 ...
- Python之迭代器&装饰器&生成器&正则
1.迭代器 迭代器是访问数据集合的一种方式,它只能从集合的第一个元素开始顺序访问,直到最后一个元素结束.类似于linux里的cat命令,只能挨行读取文本内容,不可以跳到中间或者尾部读取(不会把所有的数 ...
- HTTP Status 400 - Required request part 'file' is not present
今天使用Spring MVC做一个文件上传的功能,在提交表单的时候出现了如下错误:
- LeetCode OJ 42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- hdu 1003,nefu 728 max sum
Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...
- fatal error RC1004: unexpected end of file found处理方法
资源编译器错误 RC1004 错误消息 遇到意外的文件结束 此错误是由于文本文件的最后一行中缺少换行符和回车符而造成的.
- RHCE备考倒计时
2014年7月31日 周四 上海考试 认证RHCE6
- Chrome 自动填充的表单是淡黄色的背景怎么办!
chrome浏览器自动填充表单的黄色背景高亮(#FAFFBD)一直困扰着我,我之前没想着理它,可是最近一个登陆框,需要用到图标,于是我草率的直接设置在input元素里面,结果问题就来了,很难看很难看, ...
- Html 嵌入 swf
1. object + embed 传统的方法 优点:浏览器兼容性好,是 Macromedia 一直以来的官方方法缺点:a.embed 标签是不符合 W3C 的规范的,无法通过验证.当然, ...