#复杂迷宫求解(2)

#头文件

#pragma once
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define ROW 6 
#define COL 6
typedef struct Maze
{
	int _map[ROW][COL];
}Maze;
typedef struct Position
{
	int _x;
	int _y;
}Position;
//Stack.h


#define MAX 100
typedef Position DataType;

typedef struct Stack
{
	int top;
	DataType stack[MAX];
}Stack;

void StackInit(Stack* s);
void StackPush(Stack* s, DataType data);
void StackPop(Stack* s);
DataType GetStackTop(Stack*s);
int GetStackSize(Stack*s);
int StackEmpty(Stack *s);

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

// 打印迷宫 
void PrintMaze(Maze* m);

// 检测是否为有效的入口 
int IsValidEnter(Maze* m, Position enter);

// 检测pos是否在出口的位置 
int IsExit(Position pos, Position enter);

// 检测cur的下一步next是否为通路: 
// next位置为1 || next位置保存数据 大于 cur位置保存数据 
int IsNextPass(Maze* m, Position next, Position cur);

// 保存最短的路径 
void SaveShortPath(Stack* path, Stack* shortPath);

// 走迷宫 
void _GetMazeShortPath(Maze* m, Position cur, Position enter, Stack* path, Stack* shortPath);

void PassMaze(Maze* m, Position enter, Stack* shortPath);

#操作函数

//Stack.c
#include"HardMaze.h"
void StackInit(Stack* s)
{
	s->top = 0;
}
void StackPush(Stack* s, DataType data)
{
	if (s->top == MAX)
	{
		printf("栈已满!无法入栈元素\n");
		return;
	}

	(s->top)++;
	s->stack[s->top] = data;
}
void StackPop(Stack *s)
{
	if (s->top == 0)
	{
		printf("栈为空,无法出栈元素\n");
		return;
	}
	(s->top)--;
}
DataType GetStackTop(Stack*s)
{
	if (s->top == 0)
	{
		printf("栈为空,无法获取栈顶\n");
	}
	return s->stack[s->top];
}
int GetStackSize(Stack* s)
{
	return s->top;
}
int StackEmpty(Stack *s)
{
	if (s->top == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//Maze.c
#include"HardMaze.h"
void InitMaze(Maze * m, int map[ROW][COL])
{
	int i = 0;
	int j = 0;
	assert(m);
	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < COL; j++)
		{
			m->_map[i][j] = map[i][j];
		}
	}
}
void PrintMaze(Maze* m)
{
	assert(m);
	int i = 0;
	int j = 0;
	for (i = 0; i < ROW; i++)
	{
		for (j = 0; j < COL; j++)
		{
			printf(" %d ", m->_map[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}
int IsValidEnter(Maze* m, Position enter)
{
	assert(m);
	if (enter._x == ROW - 1 || enter._x == 0 || enter._y == COL - 1 || enter._y == 0)
	{
		return 1;
	}
	else
		return 0;
}
// pos 必须在地图中 pos位置如果是1 
int IsPass(Maze* m, Position pos)
{
	assert(m);
	if (pos._x >= ROW || pos._x < 0 || pos._y >= COL || pos._y >= COL)
	{
		return 0;
	}
	else if (m->_map[pos._x][pos._y] != 1)
	{
		return 0;
	}
	else
		return 1;
}
// 检测是否在出口的位置 
int IsExit(Position pos, Position enter)
{
	if ((pos._x == ROW - 1 || pos._x == 0 || pos._y == COL - 1 || pos._y == 0) && ((pos._x != enter._x) || (pos._y) != enter._y))
	{
		return 1;
	}
	return 0;
}

int IsNextPass(Maze* m, Position next, Position cur)
{
	if (m->_map[next._x][next._y] == 1)
	{
		return 1;
	}
	else if (m->_map[next._x][next._y] > m->_map[cur._x][cur._y])
	{
		return 1;
	}
	return 0;
}
// 保存最短的路径 

void SaveShortPath(Stack* path, Stack* shortPath)
{
	int i = 0;
	if (StackEmpty(shortPath))
	{
		for (i = 0; i < GetStackSize(path); i++)
		{
			StackPush(shortPath, path->stack[i]);
		}
	}
	else if (GetStackSize(path) < GetStackSize(shortPath))
	{
		shortPath->top = 0;
		for (i = 0; i < GetStackSize(path); i++)
		{
			StackPush(shortPath, path->stack[i]);
		}
	}
}

void PassMaze(Maze* m, Position enter, Stack* shortPath)
{
	Stack path;
	if (!IsValidEnter(m, enter))
	{
		printf("迷宫的入口异常!!!\n");
		return;
	}

	StackInit(&path);
	StackInit(shortPath);
	_GetMazeShortPath(m, enter, enter, &path, shortPath);
}

void _GetMazeShortPath(Maze* m, Position cur, Position enter, Stack* path, Stack* shortPath)
{

	PrintMaze(m);

	m->_map[enter._x][enter._y] = 2;
	if (IsExit(cur, enter))
	{
		SaveShortPath(path, shortPath);
	}
	StackPush(path, cur);
	Position next = cur;
	next._x -= 1;
	if (IsNextPass(m, next, cur))
	{
	      m->_map[next._x][next._y]	= m->_map[cur._x][cur._y] + 1;
		  _GetMazeShortPath(m, next, enter, path, shortPath);
	}
	next = cur;
	next._y -= 1;
	if (IsNextPass(m, next, cur))
	{
		m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
		_GetMazeShortPath(m, next, enter, path, shortPath);
	}
	next = cur;
	next._y += 1;
	if (IsNextPass(m, next, cur))
	{
		m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
		_GetMazeShortPath(m, next, enter, path, shortPath);

	}
	next = cur;
	next._x += 1;

	if (IsNextPass(m, next, cur))
	{
		m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
		_GetMazeShortPath(m, next, enter, path, shortPath);
	}
	StackPop(path);
}
#include"HardMaze.h"
int main()

{
	int map[ROW][COL] = {
		{ 0,0,0,0,0,0 },
		{ 0,1,1,1,0,0 },
		{ 0,1,0,1,0,0 },
		{ 0,1,0,1,0,0 },
		{ 0,1,1,1,1,1 },
		{ 0,1,0,0,0,0 }
	};
	Maze m;
	Stack shortPath;
	InitMaze(&m, map);
	Position enter;
	enter._x = 5;
	enter._y = 1;
	PrintMaze(&m);
	PassMaze(&m, enter, &shortPath);
	PrintMaze(&m);
	system("pause");
	return 0;
}

这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值