#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 队列存储的更多相关文章

  1. 搜索 || BFS || POJ 2157 Maze

    走迷宫拿宝藏,拿到所有对应的钥匙才能开门 *解法:从起点bfs,遇到门时先放入队列中,取出的时候看钥匙够不够决定开不开门,如果不够就把它再放回队列继续往下走,当队列里只有几个门循环的时候就可以退出,所 ...

  2. 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, ...

  3. BFS(最短路+路径打印) POJ 3984 迷宫问题

    题目传送门 /* BFS:额,这题的数据范围太小了.但是重点是最短路的求法和输出路径的写法. dir数组记录是当前点的上一个点是从哪个方向过来的,搜索+,那么回溯- */ /************* ...

  4. POJ 3984 迷宫问题

    K - 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  5. 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, ...

  6. Java队列存储结构及实现

    一.队列(Queue) 队列是一种特殊的线性表,它只允许在表的前段(front)进行删除操作,只允许在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头. 对于一个队 ...

  7. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  8. POJ 3984 迷宫问题【BFS/路径记录/手写队列】

    迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义 ...

  9. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

随机推荐

  1. 常用类型转换 一.常用int和string类型转换

    常用类型转换 一.常用int类型转换1. int.parse(string) 这个类型只支持string类型 2.double doubleType = Int32.MaxValue + 1;   i ...

  2. PHP数据类型转换(字符转数字,数字转字符)

    PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: (int).(integer):转换成整形 (float).(double).(real):转换成浮点型 (string):转换成字符串 ...

  3. Java IO 文件与流基础

    Java IO 文件与流基础 @author ixenos 摘要:创建文件.文件过滤.流分类.流结构.常见流.文件流.字节数组流(缓冲区) 如何创建一个文件 #当我们调用File类的构造器时,仅仅是在 ...

  4. ubuntu 14.04 32位库

    如果是ubuntu 14.04,则请先执行: 方法1: sudo gedit /etc/apt/sources.list 然后在最后添加上: deb http://archive.ubuntu.com ...

  5. PBO

    #include <GL/glew.h> #include <GL/freeglut.h> #include <iostream> GLuint pboID[]; ...

  6. $and $not null 正则表达式

    查询MasterID大于1且MasterType等于TestType的文档: db.SysCore.find({$and:[{"MasterID":{$gt:1}},{" ...

  7. iOS 时间戳的问题

    - (NSString *)timeWithTimeIntervalString:(NSString *)timeString { // 格式化时间 NSDateFormatter* formatte ...

  8. Xcode调试之查看变量

    从其他开发语言转行进军IOS开发的小伙伴可能会有这样一件苦恼的事情,调试程序时如何查看变量值?我并不喜欢每次都要通过打印去查看变量的值,也不喜欢通过光标悬浮到变量上来显示变量的值,如果要查看变量的属性 ...

  9. Subordinates

    Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  10. Android jni编辑.so库

    引自:http://www.cnblogs.com/sevenyuan/p/4202759.html 1. 在Eclipse中创建项目:TestJNI 2. 新创建一个class:TestJNI.ja ...