【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, ...
随机推荐
- WEB前端开发中的图片压缩
web前端开发中,图片的重要性不言而喻,而由于一些图片的大小加上现在国内的网速不给力等种种原因,我们非常有必要对网站使用的图片进行压缩,压缩图片必然会带来图片质量的损失,我们要尽可能的在质量降低很小的 ...
- Java IO 文件与流基础
Java IO 文件与流基础 @author ixenos 摘要:创建文件.文件过滤.流分类.流结构.常见流.文件流.字节数组流(缓冲区) 如何创建一个文件 #当我们调用File类的构造器时,仅仅是在 ...
- mysql表设计---时间类型
mysql 时间格式的区别 datetime 日期 +时间timestamp 时间戳 格式都是一样YYYY-MM-DD HH:MM:SS int(12)型存储php的time()时间戳,格式10位14 ...
- Delphi用ADOquery主从表例子(转总结)
http://blog.csdn.net/kandy_zheng/article/details/1639184 在sql server 的northwide 中建立主表 create table s ...
- LeetCode OJ 59. Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- xampp 搭建 web mac上
1.安装 2.修改数据库密码,删除phpmyadmin ,用navicat 控制数据库 3.修改/Applications/XAMPP/xamppfiles/etc/extra/httpd-vhos ...
- 团队项目(spring会议)
[例会时间]2014/4/11 [例会地点]一教213课堂上 [例会形式]小组讨论 [例会主持]马翔 [例会记录]兰梦 在这次会议上,我们针对我们的项目进行了分割,并分别认领各自的任务 下面是任务的认 ...
- C -小晴天老师系列——竖式乘法
C - 小晴天老师系列——竖式乘法 Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...
- why do we need virtual methods in C++?
http://stackoverflow.com/questions/2391679/why-do-we-need-virtual-methods-in-c Basic idea: when mark ...
- 什么是JDBC?
JDBC是Java数据库连接(Java DataBase Connectivity)技术的简称,提供连接各种常用数据库的能力! 1.方式一(配置文件实现): <!-- 1. 连接池实例 --&g ...