C++ 数据结构算法 学习笔记(18) - 栈的企业级应用案例(2)

C++ 数据结构算法 学习笔记(18) - 栈的企业级应用案例(2)

迷宫求解

在这里插入图片描述

找迷宫通路需要使用回溯法,找迷宫通路是对回溯法的一个很好的应用,实现回溯的过程用到数据结构—

回溯法:对一个包括有很多个结点,每个结点有若干个搜索分支的问题,把原问题分解为若干个子问题求解的 算法;当搜索到某个结点发现无法再继续搜索下去时,就让搜索过程回溯(回退)到该节点的前一个结点,继续 搜索该节点外的其他尚未搜索的分支;如果发现该结点无法再搜索下去,就让搜索过程回溯到这个结点的前一 结点继续这样的搜索过程;这样的搜索过程一直进行到搜索到问题的解或者搜索完了全部可搜索分支没有解存 在为止。

完整代码

完整代码实现:

Maze.h

#ifndef __MAZE_H__
#define __MAZE_H__

#include<stdio.h>
#include<stdlib.h>

#define MAXSIZE 100

typedef struct _Position {
	int _x;
	int _y;
}Position;

#define MaxSize 128 

typedef Position ElemType;

typedef struct _SqStack {
	ElemType* base; 
	ElemType* top; 
}SqStack;


bool InitStack(SqStack& S)
{
	S.base = new ElemType[MaxSize];

	if (!S.base)
		return false;
	S.top = S.base;
	return true;
}

bool PushStack(SqStack& S, ElemType e) 
{

	if (S.top - S.base == MaxSize)
		return false;
	*(S.top++) = e;
	return true;
}

bool PopStack(SqStack& S, ElemType& e) 

{
	if (S.base == S.top) {
		return false;
	}
	e = *(--S.top); 
	return true;
}


ElemType* GetTop(SqStack& S) 
{
	if (S.top != S.base) { 
		return S.top - 1; 
	}
	else {
		return NULL;
	}
}

int GetSize(SqStack& S) {
	return (S.top - S.base);
}

bool IsEmpty(SqStack& S) {
	if (S.top == S.base) {
		return true;
	}
	else {
		return false;
	}
}

void DestoryStack(SqStack& S) {
	if (S.base) {
			free(S.base);
		S.base = NULL;
		S.top = NULL;
	}
}

#endif

Maze.cpp


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

#include <assert.h>
#include "43.Maze.h"

#define ROW 6
#define COL 6

typedef struct _Maze {
	int map[ROW][COL];
}Maze;

void InitMaze(Maze* m, int map[ROW][COL]) 
{
	for (int i = 0; i < ROW; ++i)
	{
		for (int j = 0; j < COL; ++j)
		{
			m->map[i][j] = map[i][j];
		}
	}
}
void PrintMaze(Maze* m) 
{
	for (int i = 0; i < ROW; ++i)
	{
		for (int j = 0; j < COL; ++j)
		{
			printf("%d ", m->map[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

int IsValidEnter(Maze * m, Position cur) 
{
	assert(m);
	if ((cur._x == 0 || cur._x == ROW - 1)
		|| (cur._y == 0 || cur._y == COL - 1)
		&& (m->map[cur._x][cur._y] == 1))
		return 1;
	else
		return 0;
}

int IsNextPass(Maze* m, Position cur, Position next)
{
	assert(m);

	if (((next._x == cur._x) && ((next._y == cur._y + 1) || (next._y ==
		cur._y - 1)))
		|| ((next._y == cur._y) && ((next._x == cur._x + 1) || (next._x ==
			cur._x - 1)))) {

		if (((next._x >= 0 && next._x < ROW) && (next._y >= 0 && next._y
			< COL))
			&& (m->map[next._x][next._y] == 1)) {
			return 1;
		}
	}
	return 0;
}

int IsValidExit(Maze* m, Position cur, Position enter)
{
	assert(m);

	if ((cur._x != enter._x || cur._y != enter._y)
		&& ((cur._x == 0 || cur._x == ROW - 1)
			|| (cur._y == 0 || cur._y == COL - 1)))
	{
		return 1;
	}

	else
		return 0;
}

int PassMaze(Maze* m, Position enter, SqStack* s)
{
	assert(m && IsValidEnter(m, enter) == 1);

	Position cur = enter;
	Position next;
	PushStack(*s, cur);
	m->map[cur._x][cur._y] = 2;
	//PrintMaze(m);
	while (!IsEmpty(*s)) {
		cur = *GetTop(*s);
		//printf("cur: %d %d\n",cur._x, cur._y);
		if (IsValidExit(m, cur, enter) == 1)
			return 1;

		next = cur;
		next._y = cur._y - 1;
		if (IsNextPass(m, cur, next) == 1)
		{
			PushStack(*s, next);
			m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;
			//PrintMaze(m);
			continue;
		}

		next = cur;
		next._x = cur._x - 1;
		if (IsNextPass(m, cur, next) == 1)

		{
			PushStack(*s, next);
			m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;
			//PrintMaze(m);
			continue;
		}
		next = cur;
		next._y = cur._y + 1;
		if (IsNextPass(m, cur, next) == 1)
		{
			PushStack(*s, next);
			m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;
			//PrintMaze(m);
			continue;
		}

		next = cur;
		next._x = cur._x + 1;
		if (IsNextPass(m, cur, next) == 1)
		{
			PushStack(*s, next);
			m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;
			//PrintMaze(m);
			continue;
		}
		Position tmp;
		PopStack(*s, tmp);
	}
	return 0;
}
int main()
{
	int map[ROW][COL] = { 
	0,0,1,0,0,0,
	0,0,1,1,1,0,
	0,0,1,0,0,0,
	0,1,1,1,1,0,
	0,0,1,0,1,0,
	0,0,0,0,1,0
	};
	Maze m;
	Position enter; 
	enter._x = 0;
	enter._y = 2;
	InitMaze(&m, map);
	PrintMaze(&m);
	//system("pause");
		//exit(0);
		SqStack s; 
	InitStack(s); 
	int ret = PassMaze(&m, enter, &s); 
	if (ret) {
		printf("Congraturation! You have found the exit!\n");
	}
	else {
		printf("I am not stupid, but there is really no exit!\n");
	}
	PrintMaze(&m);
	system("pause");
	return 0;
}
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值