迷宫问题 (bfs广度优先搜索记录路径)
问题描述:
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)
解题思路:
bfs,用pre记录上一个位置,最后用回溯的方法输出路径。
代码:
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
int b[][];
int a[][];
int next[][]={{,},{-,},{,},{,-}};
struct node
{
int x,y;
int pre;
}que[];
int tx,ty; void print(int s)
{
if(que[s].pre!=-)
{
print(que[s].pre);
printf("(%d, %d)\n",que[s].x,que[s].y);
}
} void bfs(int x,int y)
{
int head=,tail=;
que[tail].x=x;
que[tail].y=y;
que[tail].pre=-;
tail++;
while(head<tail)
{
for(int i=;i<;i++)
{
tx=que[head].x+next[i][];
ty=que[head].y+next[i][];
if(tx<||tx>=||ty<||ty>=)continue;
if(b[tx][ty]==&&a[tx][ty]==)
{
b[tx][ty]=;
que[tail].x=tx;
que[tail].y=ty;
que[tail].pre=head;
tail++;
}
if(tx==&&ty==)
print(head);
}
head++;
}
} int main()
{
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
scanf("%d",&a[i][j]);
}
}
memset(b,,sizeof(b));
b[][]=;
printf("(0, 0)\n");
bfs(,);
printf("(4, 4)\n");
return ;
}
迷宫问题 (bfs广度优先搜索记录路径)的更多相关文章
- 算法竞赛——BFS广度优先搜索
BFS 广度优先搜索:一层一层的搜索(类似于树的层次遍历) BFS基本框架 基本步骤: 初始状态(起点)加到队列里 while(队列不为空) 队头弹出 扩展队头元素(邻接节点入队) 最后队为空,结束 ...
- BFS广度优先搜索 poj1915
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25909 Accepted: 12244 Descri ...
- 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想
dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...
- 图的遍历BFS广度优先搜索
图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...
- 关于宽搜BFS广度优先搜索的那点事
以前一直知道深搜是一个递归栈,广搜是队列,FIFO先进先出LILO后进后出啥的.DFS是以深度作为第一关键词,即当碰到岔道口时总是先选择其中的一条岔路前进,而不管其他岔路,直到碰到死胡同时才返回岔道口 ...
- 步步为营(十六)搜索(二)BFS 广度优先搜索
上一篇讲了DFS,那么与之相应的就是BFS.也就是 宽度优先遍历,又称广度优先搜索算法. 首先,让我们回顾一下什么是"深度": 更学术点的说法,能够看做"单位距离下,离起 ...
- hdu 1664(数论+同余搜索+记录路径)
Different Digits Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Pots POJ - 3414 (搜索+记录路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22688 Accepted: 9626 Special J ...
- GraphMatrix::BFS广度优先搜索
查找某一结点的邻居: virtual int firstNbr(int i) { return nextNbr(i, n); } //首个邻接顶点 virtual int nextNbr(int i, ...
随机推荐
- window.open 打开全屏窗口
window.open新打开页面为全屏状态,各个浏览器情况不一致. window.open 弹出新窗口的命令: 'page.html' 弹出窗口的文件名: 'newwindow ...
- 项目部署到tomcat
准备工作 第一步 准备项目部署文件 准备项目中使用的数据库.sql文件. 准备项目程序(整个项目的war包文件) 第二步 安装运行环境 依次安装JDK.TOMCAT.MYSQL NAVICAT需要注意 ...
- windows上react-native run-android时Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED报错
报错如图 解决 在C:\Users\{用户名}\.gradle\wrapper\dists路径下,删除所有文件夹,重新run-android ps:网上搜了说是说是java解压缩编码格式问题什么的,感 ...
- 简单的3d变换
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- CSSposition属性
基本html文本 1. position-static #part1{ width: 200px; height: 200px; background: gold; } #part2{ pos ...
- vue----分级上传
<template> <div class="page"> <div id="filePicker">选择文件</di ...
- css学习_css补充知识
1.渐进增强,优雅降级 2.浏览器前缀 3.背景渐变 4.css 验证工具 2种方式:第2种支持验证本地的css(推荐) 5.css压缩 ----(节约空间,节省带宽) 6.旋转轮播图 案例: ...
- Multi-Projector Based Display Code ---- Download
The code providing are for your reference. Please download the code according to your hareware confi ...
- debug_backtrace
<?php one(); function one() { two(); } function two() { three(); } function three() { print_r( de ...
- linux基础命令--rmdir 删除空目录
描述 rmdir命令用于删除空目录. 语法 rmdir [OPTION]... DIRECTORY... 选项列表 选项(常用的已加粗) 说明 --ignore-fail-on-non-empty 忽 ...