迷宫问题,打印所有路径,深度搜索,dfs
#include<iostream>
using namespace std;
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, 0, 0,
};
typedef struct
{int x;int y;}Point;
int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
Point path[40];int len=-1;
void dfs(int x,int y)
{ int k,nx,ny;
maze[x][y]=1;
path[++len].x=x;path[len].y=y;
if(x==4 && y==4){//结束条件
cout<<"one case:"<<endl;
for(k=0;k<=len;k++)
cout<<path[k].x<<'\t'<<path[k].y<<endl;
}
else
for(k=0;k<4;k++)
{
nx=x+dir[k][0];ny=y+dir[k][1];//下一位置(nx,ny)
if(nx>=0 &&nx<5 &&ny>=0 && ny<5 && maze[nx][ny]==0){
dfs(nx,ny);
}
}
maze[x][y]=0;//把(nx,ny)尝试后清除当前标记
len--; }
int main()
{
dfs(0,0);
}
迷宫问题,打印所有路径,深度搜索,dfs的更多相关文章
- 数据结构之 栈与队列--- 走迷宫(深度搜索dfs)
走迷宫 Time Limit: 1000MS Memory limit: 65536K 题目描述 一个由n * m 个格子组成的迷宫,起点是(1, 1), 终点是(n, m),每次可以向上下左右四个方 ...
- Word Search(深度搜索DFS,参考)
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 洛谷P1605 迷宫 深度搜索 模板!
题目背景 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右四种方式,每次只能移 ...
- 题目--oil Deposits(油田) 基础DFS(深度搜索)
上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...
- [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- F - 蜘蛛牌(深度搜索)
Problem Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么 ...
- 【算法入门】深度优先搜索(DFS)
深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...
- 深度优先搜索(DFS)递归形式改为非递归形式
DFS将递归改为非递归这个方法的需求来自于一道三维积木组合的题目,还在苦苦调试中,暂且不提. 普通的认识对于递归向非递归的转化无非是使用栈,但是结合到深度搜索如何将栈很好利用,如何很好保存现场,都不是 ...
随机推荐
- validator参数校验
目录 validator参数校验 validator参数校验 type Req struct { Sn string `json:"sn" binding:"requir ...
- Java邮件发送中的setRecipient方法使用
一.方法setRecipient(Message.RecipientType type, Address address),是用于设置邮件的接收者. 1.有两个参数,第一个参数是接收者的类型,第二 ...
- 「BUAA OO Pre」 Pre 2总结回顾概览
「BUAA OO Pre」 Pre 2总结回顾概览 目录 「BUAA OO Pre」 Pre 2总结回顾概览 Part 0 前言 写作背景 定位 您可以在这里期望获得 您在这里无法期望获得 对读者前置 ...
- 00_UML图形分类
1. 描述对象 类图(Class Diagram) 包图(Package Diagram) 对象图(Object Diagram) 组件图(Component Diagram) 部署图(Deplo ...
- 『无为则无心』Python基础 — 61、Python中的迭代器
目录 1.迭代的概念 2.迭代器的概念 3.可迭代的对象(Iterable) 4.迭代器对象(Iterator) 5.迭代器的使用体验 (1)基本用法 (2)实际应用 1.迭代的概念 (1)什么是迭代 ...
- Java 实现Https访问工具类 跳过ssl证书验证
不多BB ,代码直接粘贴可用 import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.F ...
- git for windows下载
https://npm.taobao.org/mirrors/git-for-windows/ 国内的镜像 转载自:https://blog.csdn.net/ee230/article/detail ...
- Python 内存管理与垃圾回收
Python 内存管理与垃圾回收 参考文献:https://pythonav.com/wiki/detail/6/88/ 引用计数器为主标记清除和分代回收为辅 + 缓存机制 1.1 大管家refcha ...
- Solon 1.6.30 发布,更现代感的应用开发框架
相对于 Spring Boot 和 Spring Cloud 的项目 启动快 5 - 10 倍 qps 高 2- 3 倍 运行时内存节省 1/3 ~ 1/2 打包可以缩小到 1/2 ~ 1/10(比如 ...
- SQL从零到迅速精通【实用函数(2)】
1.对查询结果进行排序 查询stu_info表中所有学生信息,并按照成绩由高到底进行排序,输入语句如下. SELECT * FROM stu_info ORDER BY s_score DESC; ...