Classic Algorithm-----Maze Problem (Application of Stack)

Table of contents

Preface

maze problem

Algorithm ideas

1. How to use the stack 

​Edit 2. Definition of direction

Code

Stack cpp code:

Stack header file h code:

Maze walking code:


Preface

        Today we are learning an algorithm problem, which is our common maze problem. In this issue, we use the data structure we have learned before - stack to perfectly solve this problem. Let's look at the problem below!

maze problem

        Given a maze, specify the starting point and end point, and find an effective and feasible path from the starting point to the end point, which is the maze problem.

Mazes can be stored and represented as two-dimensional arrays . 0 represents a path and 1 represents a barrier. Note that here it is stipulated that movement can be done in four directions: up, down, left, and right.

int maze[6][6] = {
		{1,1,1,1,1,1},
		{1,0,0,1,1,1},
		{1,0,0,0,0,0},
		{1,0,1,1,1,0},
		{1,0,0,0,0,1},
		{1,1,1,1,1,1}
	};

Algorithm ideas

1. How to use the stack 

        Maze problem, the two-dimensional array is surrounded by a wall of 1, and a starting point and an ending point are specified inside. If we want to find the path from the starting point to the ending point, we need a data structure to store this path. The stack can be used here. The characteristics of last-in-first-out are that every time you enter a passable point, you will store this point into the stack. When you encounter a dead end, you will return to the previous point. At this time, you will pop the stack, and then Take another path until you reach the end.

2. Definition of direction

Now that you understand how to use the stack, you need to define the direction of movement. When you reach a certain point, you must consider which direction to go first. At this time, you can determine the priority direction based on the general direction of the entrance and exit of the maze. , the entrance of the maze here is in the northwest direction of the exit, so my priority directions are east, south, west, and north in order, that is to say, I give priority to going east, followed by south, west, and north. The direction of movement can be up, down, left, and right according to the current coordinates. You only need to define a direction array and then add the direction of this array.

 Direction storage structure:

//试探方向存储结构
typedef struct {
	int xx, yy;
}Direction;
//东南西北
Direction dire[4] = { {0,1},{1,0},{0,-1},{-1,0} };

Code

Stack cpp code:

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

//数据类型
typedef struct datatype {
	int x, y, di;
}ElemType;
//节点
typedef struct node {
	ElemType data;
	struct node* next;
}Node;
//栈顶指示
typedef struct stack {
	int count;	//计数
	Node* point;
}Stack;


//创建节点
Node* create_node(ElemType data) {
	Node* new_node = (Node*)malloc(sizeof(Node));
	if (new_node) {
		new_node->data = data;
		new_node->next = NULL;
		return new_node;
	}
	else
	{
		printf("ERROR\n");
	}
}

//初始化
void stack_init(Stack* top) {
	top->count = 0;
	top->point = NULL;
}

int isEmpty(Stack* top) {
	if (top->count == 0) {
		return 1;
	}
	return 0;
}


//入栈
void push(Stack* top, ElemType data) {
	Node* new_node = create_node(data);
	if (new_node) {
		top->count++;
		if (top->count == 1) {//如果入栈是第一个节点的话
			top->point = new_node;
		}
		else
		{
			new_node->next = top->point;
			top->point = new_node;
		}
	}
	else
		return;
}


//出栈
Node* pop(Stack* top) {
	Node* pop_node = NULL;
	if (!isEmpty(top)) {
		pop_node = top->point;
		top->point = pop_node->next;
		top->count--;
	}
	return pop_node;
}


//递归输出路径
void show_path(Node* node) {
	if (!node)
		return;
	show_path(node->next);
	printf("(%d,%d)\n", node->data.x, node->data.y);
}

Stack header file h code:

#pragma once
//链表栈
//数据类型
typedef struct datatype {
	int x, y, di;
}ElemType;
//节点
typedef struct node {
	ElemType data;
	struct node* next;
}Node;
//栈顶指示
typedef struct stack {
	int count;	//计数
	Node* point;
}Stack;


void stack_init(Stack* top);
int isEmpty(Stack* top);
void push(Stack* top, ElemType data);
Node* pop(Stack* top);
void show_path(Node* node);

Maze walking code:

#include<stdio.h>
#include<assert.h>
#include"stack.h"

//试探方向存储结构
typedef struct {
	int xx, yy;
}Direction;
//东南西北
Direction dire[4] = { {0,1},{1,0},{0,-1},{-1,0} };

//判断能不能走出去,路径放入到了栈里面去
bool Findpath(int maze[][6],Stack* stack ,Direction dir[],int startx,int starty,int endx,int endy) {
	//startx,starty是起点的坐标;endx、endy是终点的坐标.
	assert(stack);
	int x, y, di;
	int line, col;
	//初始化
	maze[startx][starty] = -1;
	ElemType start = { startx,starty,-1 };
	push(stack, start);

	while (!isEmpty(stack)) {
		Node* po = pop(stack);
		ElemType temp = po->data;
		x = temp.x;
		y = temp.y;
		di = temp.di++;
		//使得栈储存了一条通路
		while (di < 4) {
			line = x + dire[di].xx;
			col = y + dire[di].yy;
			if (maze[line][col] == 0) {
				//储存上一个节点的位置,入栈
				temp = { x,y,di };
				push(stack, temp);
				x = line;
				y = col;
				maze[line][col] = -1;
				if (x == endx && y == endy) {
					//把终点的位置入栈
					temp = { x,y,-1 };
					push(stack, temp);
					return true;
				}
				else
					di = 0;
			}
			else
				di++;
		}
	}
	return false;
}



int main() {
	int maze[6][6] = {
		{1,1,1,1,1,1},
		{1,0,0,1,1,1},
		{1,0,0,0,0,0},
		{1,0,1,1,1,0},
		{1,0,0,0,0,1},
		{1,1,1,1,1,1}
	};
	Stack stack;
	stack_init(&stack);
	printf("能否出去:%d\n", Findpath(maze, &stack, dire, 1, 1, 4, 4));
	show_path(stack.point);//输出遍历的结果
}

Output result:

Okay, that’s all for this issue, see you next time!

Share a wallpaper: 

Guess you like

Origin blog.csdn.net/m0_73633088/article/details/133500192