Stack structure review (pathfinding problem)

Posted by witt on Wed, 09 Mar 2022 12:18:01 +0100

I wrote it a long time ago, the sixth blog of newcomers

The default direction is to walk to the right (0) from (1,1). When preparing to walk to the right, first calculate the coordinates of the landing point after walking to the right, that is, (1,2), and then judge whether (1,2) is a wall (judge whether it is 0). If it is a wall, change the direction to test until the next landing point is not a wall. If all four directions are walls, exit the external circulation directly, Now when the downward direction (1) is measured, you can go. Then exit the inner loop, put the current position (1,1) into the stack, change the coordinates of the next foothold to the current position, that is, the new current position is (2,1), block the previous foothold (Set 1), change the direction back to the default direction (0), and return to the sentence to judge whether the next foothold is the next foothold, As before, all directions are judged until the next foothold is not a wall. After judgment, it can be found that the next foothold is (2,2). Therefore, repeat the old technique, put the current position (2,1) into the stack, and then change the coordinates of the next foothold into the current position, that is, the new current position is (2,2), block the previous foothold (Set 1), and then change the direction back to the default direction, Repeat the above steps, and then after judgment, you can find that the next foothold is (2,3), so repeat the old technique, put the current position (2,2) into the stack, and then change the coordinates of the next foothold into the current position, that is, the new current position is (2,3), and then continue to put (2,3) into the stack when you get to the exit, Then, at this time, the coordinate (3,3) is already the lower right corner of the map, so it does not meet the cycle conditions, exit the cycle and put (3,3) on the stack.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define R 3 / / exit
#define C 3
 
//Find the way in the array: position -- > row and column
struct position
{
	int row;//that 's ok
	int cols;//column
};
struct position pathStack[100];//Storage path -- > stack memory
int stackTop = -1;//Stack top tag
int** maze = NULL;//Two dimensional array to describe the map
int size = 0;//Maze size
int** makeArray(int row, int cols)
{
	int** array = (int**)malloc(sizeof(int) * row);
	for (int i = 0; i < cols; i++)
	{
		array[i] = (int*)malloc(sizeof(int) * cols);
	}
	return array;
}
//The user enters a maze
void createMaze()
{
	printf("Enter maze size:\n");
	scanf("%d", &size);
	maze = makeArray(size + 2, size + 2);//Plus 2 indicates the border
	printf("Input maze:\n");
	for (int i = 1; i <= size; i++)
	{
		for (int j = 1; j <= size; j++)
		{
			scanf("%d", &maze[i][j]);
		}
	}
	//Border: 1 means you can't go
	for (int i = 0; i <= size; i++)
	{
		maze[0][i] = maze[size + 1][i] = 1;//Two lines up and down
		maze[i][0] = maze[i][size + 1];//Left and right columns
	}
}
//Find path
int findPath()
{
	//Offset attribute description
	struct position offset[4];//0-3 indicates four directions
	//Go right
	offset[0].row = 0;
	offset[0].cols = 1;
	//Go left
	offset[2].row = 0;
	offset[2].cols = -1;
	//Go down
	offset[1].row = 1;
	offset[1].cols = 0;
	//Go up
	offset[3].row = -1;
	offset[3].cols = 0;
	//Selected entry
	struct position here = { 1,1 };//current location
	//Walk the maze: record the way
	//The ID road is marked as 1
	maze[1][1] = 1;
	int option = 0;//Next moving direction
	int endOption = 3;//Termination direction
	while (here.row != R || here.cols != C)
	{
		//Move adjacent positions
		int rowNum, colsNum;//Record subscript change
		while (option <= endOption)
		{
			//Line change = original position + offset, which is determined by the direction
			rowNum = here.row + offset[option].row;
			colsNum = here.cols + offset[option].cols;
			//Once you have determined a direction to go, you need to go to the next step
			if (maze[rowNum][colsNum] == 0)
				break;//Exit the loop and go to the other side
			//If you can't go, change the direction to test
			option++;
		}
		//Can go
		if (option <= endOption)
		{
			//Go to the next
			pathStack[++stackTop] = here;
			//Change current position
			here.row = rowNum;
			here.cols = colsNum;
			//The road mark -- > is blocked
			maze[rowNum][colsNum] = 1;
			option = 0;//Set 0 to find the next position
		}
		else//option==4; There is no place to go
		{
			//Go back to the previous step
			if (stackTop == -1)
				return 0;//No way to go means no path
			//Go back to the previous step by going out of the stack
			struct position next = pathStack[stackTop];
			stackTop--;
			//Direction processing
			if (next.row == here.row)//OK, it hasn't changed. It's left and right
			{
				//Reverse offset formula
				option = 2 + next.cols - here.cols;
			}
			else
			{
				option = 3 + next.row - here.row;
 
			}
			here = next;//The current position becomes the position after fallback
		}
	}
	if (here.row == R && here.cols == C)
	{
		pathStack[++stackTop] = here;
	}
	//Print to map after exit
	printf("\n");
	for (int i = 1; i <= size; i++)
	{
		for (int j = 1; j <= size; j++)
		{
			printf("%d ", maze[i][j]);
		}
		printf("\n");
	}
	printf("\n");
	return 1;
 
}
//Print path
void printPath()
{
	printf("Path mode:\n");
	struct position curPos;
	while (stackTop != -1)
	{
		curPos = pathStack[stackTop];
		stackTop--;
		printf("(%d ,%d)--->", curPos.row, curPos.cols);
	}
	printf("\n");
}
 
int main()
{
	createMaze();
	if (findPath())
	{
		printPath();
	}
	else
	{
		printf("No path\n");
	}
	
 
	return 0;
}

Topics: C data structure