简单的迷宫(用递归实现)

动态栈的实现点击打开链接
对递归的简单了解点击打开链接 

Maze.h

#pragma once 

#include"stack.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<Windows.h>

#define MAX_ROW 4
#define MAX_COL 4

typedef struct Position
{
	int _x;
	int _y;

}Position;  

typedef struct Maze
{
	int _map[MAX_ROW][MAX_COL];

}Maze;  


//初始化迷宫
void InitMaze(Maze* m, int map[][MAX_COL]);

//检测是否为有效入口
int IsValidEntry(Maze* m, Position entry);

//检测是否可以走通
int IsPass(Maze* m, Position cur);

//检测是否为出口
int IsExit(Maze* m, Position cur, Position entry);

// 真正走迷宫函数 
int _PassMaze(Maze* m, Position entry, Position cur, Stack* s);

// 提供给用户使用的走迷宫函数,与上上个走迷宫函数的区别是:用户不用去传当前步,只需要给入口就行 
void PassMaze(Maze* m, Position entry, Stack* s);

//打印迷宫
void PrintMaze(Maze* m, int map[][MAX_COL]);
Maze.c
#include"Maze.h"
#include"stack.h"

//初始化迷宫
void InitMaze(Maze* m, int map[][MAX_COL])
{
	assert(&m);
	int i = 0;
	for (; i < MAX_ROW; i++)
	{
		int j = 0;

		for (; j < MAX_COL; j++)
		{

			m->_map[i][j] = map[i][j];


		}


	}
}

//检验是否为入口
int IsValidEntry(Maze* m, Position entry)
{
	assert(m);
	if ((entry._x == 0 || entry._y == 0) ||
		(entry._x == MAX_ROW - 1 || entry._y == MAX_COL - 1) &&
		(m->_map[entry._x][entry._y] == 1))

		return 1;
	return 0;


}


//检测是否可以走通
int IsPass(Maze* m, Position cur)
{

	assert(m);

	if ((cur._x >= 0 && cur._x <= MAX_ROW - 1) &&
		(cur._y >= 0 && cur._y <= MAX_COL - 1) &&
		(m->_map[cur._x][cur._y] == 1))

		return 1;

	return 0;


}


int IsExit(Maze* m, Position cur, Position entry)
{

	assert(m);

	if ((entry._x == 0 || entry._y == 0) ||
		(entry._x == MAX_ROW - 1 || entry._y == MAX_COL - 1) &&
		(m->_map[entry._x][entry._y] == 1) &&
		(entry._x != cur._x) && (entry._y != cur._y))

		return 1;

	return 0;


}


// 真正走迷宫函数 
int _PassMaze(Maze* m, Position entry, Position cur, Stack* s)
{
	assert(m);
	assert(s);

	Position next;

	if (IsPass(m, cur))          //检查是否为通路
	{
		StackPush(s, cur);       //先入栈

		if (IsExit(m, cur, entry))  //检验是否为出口
		{
			m->_map[cur._x][cur._y] = 2;  //如果不是则把走过的路都赋值为2
			return 1;
		}

		m->_map[cur._x][cur._y] = 2;     //如果是出口直接赋值为2




		//1.向上走

		next = cur;        //保存当前位置


		next._x -= 1;      //向上走

	

		if (_PassMaze(m, entry, next, s));  //继续调用函数自身

		   return 1;

		   

		//2.向左走
		next = cur;        //保存当前位置

		next._y -= 1;      //向上走


		if (_PassMaze(m, entry, next, s));  //继续调用函数自身

		   return 1;

		 

		//3.向右走
		next = cur;        //保存当前位置

		next._y += 1;      //向上走
		
	
		if (_PassMaze(m, entry, next, s));  //继续调用函数自身

		    return 1;


		//4.向下走
		next = cur;        //保存当前位置

		next._x += 1;      //向上走

		if (_PassMaze(m, entry, next, s));  //继续调用函数自身

		    return 1;

		//如果都走不通则标记为3并且出栈
		m->_map[cur._x][cur._y] = 3;

		StackPop(s);
	}

	return 0;
}


// 提供给用户使用的走迷宫函数,与上上个走迷宫函数的区别是:用户不用去传当前步,只需要给入口就行 
void PassMaze(Maze* m, Position entry, Stack* s)
{
	assert(m);
	assert(s);

	if (!IsValidEntry(m, entry))       //入口不合法
	{
		printf("入口不合法!!!!\n");

		return;
	
	}
	
	//入口合法

	_PassMaze(m, entry,entry,s);


}



//打印迷宫
void PrintMaze(Maze* m, int map[][MAX_COL])
{
	assert(m);

	int i = 0;
	for (; i < MAX_ROW; i++)
	{

		int j = 0;
		for (; j < MAX_COL; j++)
		{

			printf("%d  ", m->_map[i][j]);

		}
		printf("\n");
	}
	printf("\n");
}

test.c
#include"Maze.h"
#include"stack.h"

void TestMaze()
{
	Stack s;
	Position entry;
	Maze m;
	StackInit(&s,10);


	int map[4][4] = {
		{0,0,0,0},
		{0,1,0,0},
		{0,1,1,1},
		{0,1,0,0}
	};

	InitMaze(&m, map);
	PrintMaze(&m, map);
	entry._x = 3;
	entry._y = 1;
	PassMaze(&m, entry,&s);      //简单迷宫递归  

	printf("\n");

	PrintMaze(&m, map);


}


int main()
{

	Stack s;

	TestMaze();

	system("pause");
	return 0;
}



猜你喜欢

转载自blog.csdn.net/alidada_blog/article/details/80243620