经典算法----迷宫问题(找出所有路径)

目录

前言

问题描述

算法思路

定义方向

 回溯算法

代码实现


前言

        前面我发布了一篇关于迷宫问题的解决方法,是通过栈的方式来解决这个问题的(链接:经典算法-----迷宫问题(栈的应用)-CSDN博客),但是这个方法只可以找到一条路径,那么今天我们就进一步去讲解迷宫问题,通过回溯算法来找到全部的路径,下面就一起来看看吧!

问题描述

           给定一个迷宫,指明起点和终点,找出从起点出发到终点的有效可行路径,就是迷宫问题(maze problem)。迷宫可以以二维数组来存储表示。0表示通路,1表示障碍。注意这里规定移动可以从上、下、左、右四方方向移动,求走出迷宫的全部路径

#define m 4
#define n 4
    int maze[m + 2][n + 2] = {
		{1, 1, 1, 1, 1, 1},
		{1, 0, 0, 0, 1, 1},
		{1, 0, 1, 0, 0, 1},
		{1, 0, 0, 0, 1 ,1},
		{1, 1, 0, 0, 0, 1},
		{1, 1, 1, 1, 1, 1}
	};

 

算法思路

定义方向

同样的,每走到一个位置就要想该往哪一个方向去走,所以有东南西北这4个方向,每次往一个方向走之后就标记好当前方向和当前位置,然后同样的去进行分享的试探,当走到没路走的时候就进行原路返回。方向的定义如下:

//试探方向存储结构
typedef struct {
	int xx, yy;
}Direction;
//东南西北
Direction dire[4] = { {0,1},{1,0},{0,-1},{-1,0} };

 回溯算法

        回溯,计算机算法,回溯法也称试探法,它的基本思想是:从问题的某一种状态(初始状态)出发,搜索从这种状态出发所能达到的所有“状态”,当一条路走到“尽头”的时候(不能再前进),再后退一步或若干步,从另一种可能“状态”出发,继续搜索,直到所有的“路径”(状态)都试探过。这种不断“前进”、不断“回溯”寻找解的方法,就称作“回溯法”。递归回溯:由于回溯法是对解空间的深度优先搜索,找到结果或者没找到结果就原路返回去找下一条路。可以看出回溯算法是一种暴力算法,就是彻彻底底的一个一个找,找得到就走,找不到就回去。

对于本期的迷宫问题,我们要想找到全部的路径,就最好去用回溯算法,也就是一个一个找,毕竟实际情况走迷宫也是如此,在不知道的情况下,也只能去一个一个找。对于本题,我们可以这样子走,每走一个地方就把这个地方标记为-1,表示已经走过,当遇到死路的时候,就返回上一个位置,然后换一个方向来走,直到换到可以走得通的方向,走完这条路的话(当前走完的路所有坐标都标记为-1),我们就一直回溯换方向到其他方向能走的位置,直到整个地图全部能走的路都标记为-1,就结束回溯递归。

代码实现

#include<stdio.h>
#define m 4
#define n 4

//试探方向存储结构
typedef struct {
	int xx, yy;
}Direction;
//东南西北
Direction dire[4] = { {0,1},{1,0},{0,-1},{-1,0} };


typedef struct node {
	int x, y;
}Node;
typedef struct path {
	Node data[100];//标记路径位置的数组
	int count;//统计节点
}Path;

//输出路径
void print(Path p, int* N) {
	*N += 1;
	printf("第%d条路径:\n", *N);
	for (int i = 0; i < p.count; i++) {
		printf("(%d,%d)->", p.data[i].x, p.data[i].y);
	}
	printf("Printover!\n\n");
}

void find_path(int maze[][n+2], int* N, int x, int y, int endx, int endy, Path p) {
	//如果走到终点的时候
	if (x == endx && y == endy) {
		maze[x][y] = -1;
		//把终点位置存入到路径去
		p.data[p.count].x = x;
		p.data[p.count].y = y;
		p.count++;
		print(p, N);//输出路径
		//走完了就回到上一个位置,然后换方向走
		return;
	}
	else {
		//如果当前位置为0,也就是能走的话
		if (maze[x][y] == 0) {
			int di = 0;
			while (di < 4) {//4个方向都进行试探
				//储存当前位置
				p.data[p.count].x = x;
				p.data[p.count].y = y;
				p.count++;
				//标记为-1,表示已经走过
				maze[x][y] = -1;
				int i, j;
				//改变方向
				i = x + dire[di].xx;
				j = y + dire[di].yy;

				find_path(maze, N, i, j, endx, endy, p);//递归进入到下一个位置

				//回溯:回到上一个位置继续操作
				//当前位置给抹除掉
				p.count--;
				maze[x][y] = 0;

				di++;//改变方向
			}
		}
		//不能走的话就返回,回到上一个位置
		else
			return;
	}
}


int main() {
	int maze[m + 2][n + 2] = {
		{1, 1, 1, 1, 1, 1},
		{1, 0, 0, 0, 1, 1},
		{1, 0, 1, 0, 0, 1},
		{1, 0, 0, 0, 1 ,1},
		{1, 1, 0, 0, 0, 1},
		{1, 1, 1, 1, 1, 1}
	};
	Path mp;
	mp.count = 0;
	int N = 0;

	find_path(maze, &N, 1, 1, m, n, mp);
}

结果如下:

 以上就是本期的全部内容了,我们下次见!

分享一张壁纸: 

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个 C# 实现的迷宫路径搜索算法,可以找出所有可行路径: ```csharp using System; using System.Collections.Generic; class Maze { private int[,] maze; private int rows; private int cols; private List<List<Tuple<int, int>>> paths; public Maze(int[,] maze) { this.maze = maze; this.rows = maze.GetLength(0); this.cols = maze.GetLength(1); this.paths = new List<List<Tuple<int, int>>>(); } public void FindPaths() { List<Tuple<int, int>> path = new List<Tuple<int, int>>(); path.Add(new Tuple<int, int>(0, 0)); FindPathsHelper(0, 0, path); } private void FindPathsHelper(int row, int col, List<Tuple<int, int>> path) { if (row == rows - 1 && col == cols - 1) { paths.Add(path); return; } if (row > 0 && maze[row - 1, col] == 0 && !path.Contains(new Tuple<int, int>(row - 1, col))) { List<Tuple<int, int>> newPath = new List<Tuple<int, int>>(path); newPath.Add(new Tuple<int, int>(row - 1, col)); FindPathsHelper(row - 1, col, newPath); } if (row < rows - 1 && maze[row + 1, col] == 0 && !path.Contains(new Tuple<int, int>(row + 1, col))) { List<Tuple<int, int>> newPath = new List<Tuple<int, int>>(path); newPath.Add(new Tuple<int, int>(row + 1, col)); FindPathsHelper(row + 1, col, newPath); } if (col > 0 && maze[row, col - 1] == 0 && !path.Contains(new Tuple<int, int>(row, col - 1))) { List<Tuple<int, int>> newPath = new List<Tuple<int, int>>(path); newPath.Add(new Tuple<int, int>(row, col - 1)); FindPathsHelper(row, col - 1, newPath); } if (col < cols - 1 && maze[row, col + 1] == 0 && !path.Contains(new Tuple<int, int>(row, col + 1))) { List<Tuple<int, int>> newPath = new List<Tuple<int, int>>(path); newPath.Add(new Tuple<int, int>(row, col + 1)); FindPathsHelper(row, col + 1, newPath); } } public void PrintPaths() { foreach (var path in paths) { foreach (var p in path) { Console.Write("({0},{1}) ", p.Item1, p.Item2); } Console.WriteLine(); } } } class Program { static void Main(string[] args) { int[,] maze = { { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 1, 0, 0, 0 }, { 0, 1, 0, 0 } }; Maze mazeObj = new Maze(maze); mazeObj.FindPaths(); mazeObj.PrintPaths(); } } ``` 输出结果为: ``` (0,0) (0,1) (1,1) (1,2) (1,3) (2,3) (3,3) (0,0) (0,1) (1,1) (1,2) (2,2) (2,3) (3,3) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (0,0) (1,0) (2,0) (2,1) (2,2) (2,3) (3,3) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fitz&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值