Solve complex mazes -- "multi-path maze (with rings between paths)

       There are many kinds of mazes we usually see. Here we take the multi-path maze as an example to solve the maze, find the shortest path, and write it in C language. Some operations involved include:

       1.  Initialize the maze map data -- "2. Check whether the entrance of the maze is valid -- "  3.  Check whether the current position is the exit of the maze -- " 4.  Save the shortest path -- " 5.  Check whether the next step of the current position is Able to walk through -- "6. Walk the  maze --" 7.  The specific way to walk the maze -- "8.  Print the maze map data -- "9.  Print the path 

     Here's some code to do this in C:

    maze.c-->The code involves some operations on the dynamic stack

   

#define _CRT_SECURE_NO_WARNINGS 0;
#include "maze.h"
//initialize the maze
void MazeInit(maze* s, DataTypeMaze arr[MAZE_ROW][MAZE_COL])
{
	assert(s);
	int i = 0, j = 0;
	for (i = 0; i < MAZE_ROW; i++)
	{
		for (j = 0; j < MAZE_COL; j++)
		{
			s->maze[i][j] = arr[i][j];
		}
	}
}
// print the maze
void PrintMaze(maze m)
{
	int i = 0, j = 0;
	for (i = 0; i < MAZE_ROW; i++)
	{
		for (j = 0; j < MAZE_COL; j++)
		{
			printf("%d ", m.maze[i][j]);
		}
		printf("\n");
	}
}
// Check if the entrance to the maze is valid
int IsValidEnter(maze *m, PosType enter)
{
	if (m->maze[enter._x][enter._y] >= 1)
	{
		if ((enter._x == 0) || (enter._y == 0) || (enter._x == MAZE_ROW - 1) || (enter ._y == MAZE_COL - 1))
			return 1;
	}
	   return 0;
}
// Check whether the next step at the current position can go through
int IsNextPass(maze* m,PosType cur, PosType next)
{
	assert(m);
	if ((m->maze[next._x][next._y] == 1) || (m->maze[cur._x][cur._y]< m->maze[next._x][next._y]))  
		return 1;

    	return 0;
}
// Check whether the cur position is the exit of the maze (if it is the exit, return 0, otherwise return 1)
int IsMazeExit(maze* m, PosType cur, PosType enter)
{
	assert(m);
	if (cur._x==enter._x && cur._y==enter._y)
		return 0;

    	return 1;
}
//print path
void printPath(Stack Path)
{
	int i = 0;
	for (; i < stackSize(Path); i++)
	{
		printf("%d,%d-> ", Path.arr[i]);
	}
	 printf("over \n");
}
// save the shortest path
void SaveShortPath(Stack* path, Stack* shortPath)
{
	assert(path);
	assert(shortPath);
	int i = 0;
	shortPath->_top = 0;
	for (; i < stackSize(*path); i++)
	{
       StackPush(shortPath, path->arr[i]);
	}
}
// walk the maze
void PassMaze(maze* m, PosType  enter, Stack* ShortPath)
{
	assert(m);
	assert(ShortPath);
	Stack path;
	StackInit(&path);
	_PassMaze(m, enter, enter,&path,ShortPath);
}
// The specific way to walk the maze
void _PassMaze(maze *m, PosType enter,PosType cur, Stack *path,Stack* shortPath)
{
	assert(path);
	assert(shortPath);
	PosType next = cur;
	if (!IsValidEnter(m,enter))
	{
		perror("Invalid entry point");
		exit(1);
	}

	if (cur._x == 5 && cur._y == 1)
	{
		m->maze[cur._x][cur._y] = 2;
		StackPush(path, cur);
	}

	//superior
	next._x -= 1;
	if (IsNextPass(m,cur,next))
	{
		m->maze[next._x][next._y] = m->maze[cur._x][cur._y] + 1;
		StackPush(path, next);
		_PassMaze(m,enter, next, path, shortPath);
	}
	//right
	next = cur;
	next._y += 1;
	if (IsNextPass(m, cur, next))
	{
		m->maze[next._x][next._y] = m->maze[cur._x][cur._y] + 1;
		StackPush(path, next);
		_PassMaze(m, enter, next, path, shortPath);
	}

	//Left
	next = cur;
	next._y -= 1;
	if (IsNextPass(m, cur, next))
	{
		m->maze[next._x][next._y] = m->maze[cur._x][cur._y] + 1;
		StackPush(path, next);
		_PassMaze(m, enter, next, path, shortPath);
	}


	//Down
	next = cur;
	next._x += 1;
	if (IsNextPass(m, cur, next))
	{
		m->maze[next._x][next._y] = m->maze[cur._x][cur._y] + 1;
		StackPush(path, next);
		_PassMaze(m, enter, next, path, shortPath);
	}
	// Determine if the current location is an exit
	if (IsMazeExit(m,cur,enter))
	{
		if (shortPath->_top == 0 || stackSize(*path)<stackSize(*shortPath))   
		{
			if (cur._x == 0 || cur._y==0 || cur._x==MAZE_COL-1 || cur._y==MAZE_ROW-1)
			SaveShortPath(path, shortPath);
		}
	}
	else
		return;

	StackPop(path);
}
maze.h

#pragma once
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>

typedef int DataTypeMaze;
#define MAZE_ROW  6
#define MAZE_COL  6
typedef struct PosType
{
	int _x;
	int _y;
}PosType;

typedef struct maze
{
	DataTypeMaze maze[MAZE_ROW][MAZE_COL];
}maze;

typedef PosType DataType;
typedef struct stack
{
	DataType* arr;
	int _capacity; //The size of the underlying space
	int _top; //Number of valid elements
}Stack;


void StackInit(Stack* s);
void StackPush(Stack* s, DataType data);
void ExpandStack(Stack* s);
void StackPop(Stack* s);
int IsEmptyStack(Stack s);
DataType StackTop(Stack s);
void ChangePath(Stack *path, Stack *shortPath);
void printPath(Stack Path);
int stackSize(Stack s);

void MazeInit(maze* s, DataTypeMaze arr[MAZE_ROW][MAZE_COL]);
void PrintMaze(maze m);
int IsNextPass(maze* m, PosType cur, PosType next);
void _PassMaze(maze *m, PosType enter, PosType cur, Stack *path, Stack* shortPath);
void PassMaze(maze* m, PosType  enter, Stack* ShortPath);
void SaveShortPath(Stack* path, Stack* shortPath);
int IsValidEnter(maze *m, PosType enter);
int IsMazeExit(maze* m, PosType cur, PosType enter);

test.c

#define _CRT_SECURE_NO_WARNINGS 0;
#include "maze.h"

//Multi-pass network with ring maze
void testMaze()
{
	maze m;
	Stack shortPath;
	StackInit(&shortPath);
	DataType x;
	x._x = 5;
	x._y = 1;
	DataTypeMaze arr[MAZE_ROW][MAZE_COL] = { { 0,0 },{ 0, 1,1,1 },{ 0, 1,0,1 },{ 0, 1, 0, 1},{ 0,1,1,1,1,1 },{ 0, 1} };
	MazeInit(&m, arr);
	PrintMaze(m);
	PassMaze(&m, x, &shortPath);
	printf("The shortest path of the maze -- "");
	printPath(shortPath);
	printf("**********************\n");
	PrintMaze(m);
}

intmain()
{
	testMaze();
	system("pause");
	return 0;
}
    The above is the C code to solve the complex maze. The next article will write to solve the simple maze in two ways: loop and recursion! ! !


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324760591&siteId=291194637