迷宫的基本操作

Maze.h
#pragma once
#include<stdio.h>
#include"Stack.h"
#define MAX_ROW 6
#define MAX_COL 6
typedef struct position
{
	int x;
	int y;
}position;

typedef struct Maze
{
	int map[MAX_ROW][MAX_COL];
}Maze;

void InitMap(Maze* m, int map[MAX_ROW][MAX_COL]);
void PrintMap(Maze* m);
void PassMap(Maze* m, position enter, Stack* s);//走迷宫
void PassMapRec(Maze * m, position enter);//走迷宫,递归走法
int _PassMaze(Maze* m, position entry, position cur);//辅助递归
int IsValidEnter(Maze* m, position enter);//检测迷宫的入口是否有效
int IsMazeExit(Maze* m, position cur, position enter);//检测cur的位置是否为迷宫的出口
int IsPass(Maze* m, position cur);//检测该位置是否通路
void PrintPath(Stack* s);//打印走过的路径


Maze.c
#include"Maze.h"

void InitMap(Maze * m, int map[MAX_ROW][MAX_COL])
{
	int i = 0, j = 0;
	if (NULL == m)
	{
		return;
	}
	for (i = 0; i < MAX_ROW; i++)
	{
		for (j = 0; j < MAX_COL; j++)
		{
			m->map[i][j] = map[i][j];
		}
	}
}

void PrintMap(Maze * m)
{
	int i = 0, j = 0;
	if (NULL == m)
	{
		return;
	}
	for (i = 0; i < MAX_ROW; i++)
	{
		for (j = 0; j < MAX_COL; j++)
		{
			printf("%d ", m->map[i][j]);
		}
		printf("\n");
	}
}

void PassMap(Maze * m, position enter, Stack * s)
{
	//保存路径->走当前步
	//1.检测入口是否合法(非法入口->返回;合法入口->保存入口->stack)
	position next;//当前位置下一步该走的坐标
	if (!IsValidEnter(m, enter))
	{
		printf("迷宫入口有误!!!\n");
		return;
	}
	//2.取栈顶位置cur->走cur位置->检测cur是否为出口(IsExit( ),是出口->返回)
	//先检测next位置是否可以走通
	//可以走通->保存当前路径
	StackPush(s, enter);
	while (StackEmpty(s))
	{
		position cur = StackTop(s);//取栈顶元素,作为每次的入口
		m->map[cur.x][cur.y] = 2;//走过就标记为2
		if (IsMazeExit(m, cur,enter))//已经走到出口就退出
		{
			return;
		}
	    //上
		next = enter;
		next.x -= 1;
		if (Ispass(m, next))
		{
			StackPush(s, next);
			continue;
		}
		//左
		next = cur;
		next.y -= 1;
		if (Ispass(m, next))
		{
			StackPush(s, next);
			continue;
		}
		//右
		next = cur;
		next.y += 1;
		if (Ispass(m, next))
		{
			StackPush(s, next);
			continue;
		}
		//下
		next = cur;
		next.x += 1;
		if (Ispass(m, next))
		{
			StackPush(s, next);
			continue;
		}
		//说明cur步走错了->重新标记->出栈
		m->map[cur.x][cur.y] = 3;
		StackPop(s);
	}
}

void PassMapRec(Maze * m, position enter)
{
	//保存路径->走当前步
	//1.检测入口是否合法(非法入口->返回;合法入口->保存入口->stack)
	if (!IsValidEnter(m, enter))
	{
		printf("迷宫入口有误!!!\n");
		return;
	}
	_PassMaze(m, enter, enter);
}

int IsValidEnter(Maze * m, position enter)
{
	if (NULL == m)
	{
		return;
	}
	//保证入口一定在边界
	if (0 == enter.x || MAX_ROW - 1 == enter.x ||
		0 == enter.y || MAX_COL - 1 == enter.y)
	{
		return 1 == m->map[enter.x][enter.y];//有效的入口返回1,无效返回0
	}
	return 0;
}

int IsMazeExit(Maze * m, position cur, position enter)
{
	if (cur.x == enter.x && cur.y == enter.y)
	{
		return 0;
	}
	if (cur.x == 0 || cur.x == MAX_ROW - 1 || cur.y == 0 || cur.y == MAX_COL - 1)
	{
		return 1;
	}
	return 0;
}

int IsPass(Maze * m, position cur)
{
	return m->map[cur.x][cur.y] == 1;
}

void PrintPath(Stack * s)
{
	//当栈的元素个数大于一,取出栈顶元素,打印,出一个栈顶元素
	position top;
	while (StackSize(s) > 1)
	{
		top = StackTop(s);
		printf("{%d, %d}<---", top.x, top.y);
		StackPop(s);
	}
	//这时打印最后一个元素
	position top = StackTop(s);
	printf("{%d, %d}", top.x, top.y);
}

int _PassMaze(Maze * m, position entry, position cur)
{
	if (IsPass(m, cur))
	{
		position next;
		m->map[cur.x][cur.y] = 2;
		if (IsMazeExit(m, cur, entry))
		{
			return 1;
		}
		//上
		next = cur;
		next.x -= 1;
		if (_PassMaze(m, entry, next))
		{
			return 1;
		}
		//左
		next = cur;
		next.y -= 1;
		if (_PassMaze(m, entry, next))
		{
			return 1;
		}
		//右
		next = cur;
		next.y += 1;
		if (_PassMaze(m, entry, next))
		{
			return 1;
		}
		//下
		next = cur;
		next.x += 1;
		if (_PassMaze(m, entry, next))
		{
			return 1;
		}
		m->map[cur.x][cur.y] = 3;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值