博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
迷宫问题
阅读量:4029 次
发布时间:2019-05-24

本文共 2601 字,大约阅读时间需要 8 分钟。

下面是用穷举法来走迷宫

走迷宫的规则:当前坐标是(i, j)下一步可以往四个方向行走,上下左右。

在迷宫数组 0标识可以走,1标识不能走 2 标识已经走过 3标识回退的路

穷举法走出迷宫有两种方法:

1 栈

2 递归

下面通过栈的回溯解决迷宫问题,具体实现如下:

#include
using namespace std;#include
#include
#define N 10#define M 11struct Pos{ int _row; int _col;};//获得迷宫void GetMase(int* a){ assert(a); //fout()打开文件,路径(用“\\”)+名字,"r"读取文件 FILE* fout = (FILE*)fopen("E:\\bite\\stack\\MazeMap.txt","r"); assert(fout); for (int i = 0; i < N; i++) { for (int j = 0; j < M;) { char ch = fgetc(fout);//fgetc获得文件中的字符 if (ch >= '0'&&ch <= '9') { a[i*M + j] = ch - '0'; j++; } } }}//判断迷宫是否可以走出去,利用栈存放和回溯实现bool SerachMazePath(int* a, Pos entry, stack
& paths){//该迷宫0为通路,1标识为死路,2标识已经走过,3标识回退的路 assert(a); paths.push(entry); while (!paths.empty()) { Pos cur = paths.top(); //走过的标记为2 a[cur._row*M + cur._col] = 2; //检查是否到达出口 if (cur._row == N - 1 || cur._col == M - 1) { return true; } Pos next = cur; //向左走 if (cur._col - 1 >= 0 && a[cur._row*M + cur._col - 1] == 0) { next._col--; paths.push(next); continue; } //向右走 if (cur._col + 1 < M && a[cur._row*M + cur._col + 1] == 0) { next._col++; paths.push(next); continue; } //向上走 if ((cur._row - 1) >= 0 && a[(cur._row - 1) * M + cur._col] == 0) { next._row--; paths.push(next); continue; } //向下走 if (cur._row + 1

递归实现如下:

//判断迷宫是否可以走出去,利用递归实现//递归的实现相比较栈减少在回溯时的重复比较,但递归会一直回退到入口处结束int SerachMazePath(int* a,Pos entry){//该迷宫0为通路,1标识为死路,2标识已经走过,3标识回退的路	assert(a);	Pos cur = entry;	//走过的标记为2	a[cur._row*M + cur._col] = 2;	Pos next = cur;	//检查是否到达出口	if (cur._row != N - 1 && cur._col != M - 1)	{		//向左走		if (cur._col - 1 >= 0 && a[cur._row*M + cur._col - 1] == 0)		{			next = cur;			next._col--;			if(SerachMazePath(a, next))				return 1;			a[next._row*M + next._col] = 3;		}		//向右走		if (cur._col + 1 < M && a[cur._row*M + cur._col + 1] == 0)		{			next = cur;			next._col++;			if(SerachMazePath(a, next))				return 1;			a[next._row*M + next._col] = 3;		}		//向上走		if ((cur._row - 1)>=0 && a[(cur._row - 1) * M + cur._col] == 0)		{			next = cur;			next._row--;			if(SerachMazePath(a, next))				return 1;			a[next._row*M + next._col] = 3;		}		//向下走		if (cur._row + 1

测试用例如下:

void Test1(){	int *sm = new int[N * M];	Pos entry;	stack
 paths; entry._row = 2; //迷宫入口处 entry._col = 0; GetMase(sm); cout << "是否找到通路:" << SerachMazePath(sm, entry, paths) << endl; Print(sm);}void Test2(){ int *sm = new int[N * M]; Pos entry; entry._row = 2; //迷宫入口处 entry._col = 0; GetMase(sm); Print(sm); cout << "是否找到通路:" << SerachMazePath(sm, entry) << endl; Print(sm);}

结果如下所示:

本文出自 “” 博客,请务必保留此出处

转载地址:http://jilbi.baihongyu.com/

你可能感兴趣的文章
[LeetCode By Python]7 Reverse Integer
查看>>
[LeetCode By Python]9. Palindrome Number
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]107. Binary Tree Level Order Traversal II
查看>>
[LeetCode By Python]108. Convert Sorted Array to Binary Search Tree
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By Python]167. Two Sum II - Input array is sorted
查看>>
[LeetCode BY Python]169. Majority Element
查看>>
[LeetCode By Python]172. Factorial Trailing Zeroes
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
python jieba分词模块的基本用法
查看>>
[CCF BY C++]2017.12 最小差值
查看>>
[CCF BY C++]2017-12 游戏
查看>>
如何打开ipynb文件
查看>>
[Leetcode BY python ]190. Reverse Bits
查看>>