C++ 迷宫问题的回溯解法

// DataStruTest1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stack>
#include <iostream>
#include<stack>
using namespace std;

struct offsets{
	int a;
	int b;
	char * dir;
};

const offsets moves[8] = {{-1,0,"北"},{-1,1,"东北"},{0,1,"东"},{1,1,"东南"},{1,0,"南"},{1,-1,"西南"},{0,-1,"西"},{-1,-1,"西北"}};
const int m=5,n=6;
int Maze[m+2][n+2] = {    1,1,1,1,1,1,1,1,
	                                       0,0,0,1,1,1,0,1,
	                                       1,1,1,0,1,1,0,1,
	                                       1,0,0,0,1,0,1,1,
	                                       1,1,1,0,0,1,0,1,
	                                       1,1,0,1,0,1,0,0,
	                                       1,1,1,1,1,1,1,1  };
int mask[m+2][n+2] = {0};
stack<offsets>  paths;

bool SeekPathRecurse(int x , int y);
bool SeekPathNoRecurse(int x=1 , int y=1);

int _tmain(int argc, _TCHAR* argv[])
{
	//Use the recurse method.
	for(int i=0;i<m+2;i++)
		for(int j=0;j<n+2;j++)
			mask[i][j]=0;
	mask[1][1] = 1;
	SeekPathRecurse(1 , 1);
	while(paths.empty()==false)
	{
		offsets tmpMove = paths.top();
		paths.pop();
		cout<<"("<<tmpMove.a<<" , "<<tmpMove.b<<")"<<tmpMove.dir<<endl;
	}
	cout<<endl;
	
	//Use the non-recurse method.
	for(int i=0;i<m+2;i++)
		for(int j=0;j<n+2;j++)
			mask[i][j]=0;
	mask[1][1] = 1;
	SeekPathNoRecurse(1 , 1);

	system("pause");
	return 0;
}

bool SeekPathRecurse(int x , int y)
{
	int s,t;
	offsets tmpoff;
	char * dir;
	if(x==m && y==n)
		return true;
	for(int i=0;i<8;i++)
	{
		s = x+moves[i].a;
		t = y+moves[i].b;
		dir = moves[i].dir;
		if(Maze[s][t]==0 && mask[s][t]==0)
		{
			mask[s][t] = 1;
			if(SeekPathRecurse(s,t))
			{
				//cout<<"("<<s<<","<<t<<"),"<<dir<<endl;
				tmpoff.a = s;
				tmpoff.b = t;
				tmpoff.dir = dir;
				paths.push(tmpoff);
				return true;
			}
		}
	}
	if(x==1 && y==1)
		cout<<"There is no path!"<<endl;
	return false;
}

bool SeekPathNoRecurse( int x , int y )
{
	stack<offsets> mystack;
	offsets tmpoff;
	int s,t,p,q;
	int d;
	char * dir;
	tmpoff.a = 1;
	tmpoff.b = 1;
	tmpoff.dir = "东";
	mystack.push(tmpoff);
	while(mystack.empty()==false)
	{
		tmpoff = mystack.top();
		mystack.pop();
		s = tmpoff.a;
		t = tmpoff.b;
		d=2;
		while(d<8)
		{
			p = s+moves[d].a;
			q = t+moves[d].b;
			dir = moves[d].dir;
			if(p==m && q==n)
			{
				//这里是输出结果,mystack里面存储的是倒序的路径
				stack<offsets> tmpstack;
				while(mystack.empty()==false)
				{
					offsets tmpMove = mystack.top();
					mystack.pop();
					tmpstack.push(tmpMove);
				}
				//所以需要用另外一个栈进行逆序一下输出
				while(tmpstack.empty()==false)
				{
					offsets tmpMove = tmpstack.top();
					tmpstack.pop();
					cout<<"("<<tmpMove.a<<" , "<<tmpMove.b<<")"<<tmpMove.dir<<endl;
				}
				return true;
			}
			if(Maze[p][q]==0 && mask[p][q]==0)
			{
				mask[p][q] = 1;
				tmpoff.a = s;
				tmpoff.b = t;
				tmpoff.dir = dir;
				mystack.push(tmpoff);
				s = p;
				t = q;
				d = 0;
			}
			else
				d++;
		}	
	}
}


结果如下:

《C++ 迷宫问题的回溯解法》

 

 

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/ybhou/article/details/8819154
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞