C语言[栈]实现迷宫求解(文件读取图)

  • 手写栈 LinkStack.h
#ifndef LINKSTACK_H
#define LINKSTACK_H

typedef enum { true = 1, false = 0 } bool;
typedef int Data;

struct Node {
	Data data;
	struct Node* next;
};
typedef struct Node* PNode;
typedef struct Node* LinkStack;
LinkStack CreatStack()
{
	LinkStack top = (LinkStack)malloc(sizeof(struct Node));
	if (top != NULL)	top->next = NULL;
	else printf("Fail!");
	return top;
}
void Push(LinkStack top, Data x) {

	PNode p = (PNode)malloc(sizeof(struct Node));
	if (p == NULL)	printf("Fail!");
	else {
		p->data = x;
		p->next = top->next;
		top->next = p;
	}
}
void Pop(LinkStack top) {
	PNode p;
	if (top->next == NULL) printf("Empty!");
	else {
		p = top->next;
		top->next = p->next;
		free(p);
	}
}
Data Top(LinkStack top) {
	if (top->next == NULL) printf("Empty!");
	else return top->next->data;
}

bool Empty(LinkStack top) {
	return top->next == NULL;
}


#endif // !LINKSTACK_H

  • 图 Maze.h
#pragma warning(disable:4996)
#ifndef MAZE_H_
#define MAZE_H_
#include<conio.h>
#include <stdlib.h>
#include <stdio.h>
#include "LinkStack.h"

typedef struct MAZE
{
	int size;
	int** data;
}Maze;

void PrintMaze(Maze* maze);

Maze* InitMaze(int size)
{
	int i;
	Maze* maze = (Maze*)malloc(sizeof(Maze));
	maze->size = size + 2;
	maze->data = (int**)malloc(sizeof(int*) * maze->size);
	for (i = 0; i < maze->size; i++)
	{
		maze->data[i] = (int*)malloc(sizeof(int) * maze->size);
	}

	for (i = 0; i < maze->size; i++)
	{
		maze->data[i][0] = maze->data[0][i] = 1;
		maze->data[size+1][i] = maze->data[i][size+1] = 1;
	}
	return maze;
}

void Read(Maze* maze)
{
	int i, j;
	printf("输入迷宫结构:\n");
	for (i = 1; i < maze->size - 1; i++)
	{
		for (j = 1; j < maze->size - 1; j++)
		{
			scanf_s("%d", &maze->data[i][j]);
		}
	}
}


void ReadFile(Maze* maze,const char* file)
{
	FILE* fp = NULL;
	char buff[255];
	fp = fopen(file, "r");

	int i = 1, j = 1;	
	int n = maze->size;
	char ch = fgetc(fp);
	while (ch != EOF) {

		if (ch == '1' || ch == '0')
		{
			//putchar(ch);
			maze->data[i][j] = ch - 48;
			j++;
			if (j == n-1)
			{
				j = 1;
				i++;
			}
			if (i == n - 1) break;
		}
		ch = fgetc(fp);
	}
	fclose(fp);
	printf("读取文件%s获取迷宫(有边框)如下: \n",file);
	PrintMaze(maze);
}

void PrintMaze(Maze* maze)//打印迷宫
{
	for (int i = 0; i < maze->size; i++)
	{
		for (int j = 0; j < maze->size; j++)
		{
			if (maze->data[i][j] == 1)
				printf("%c ", 1);//方块
			else if (maze->data[i][j] == '*')
				printf(" *");
			else
				printf("  ");
		}
		printf("\n");
	}
	printf("\n");

}


#endif // !MAZE_H_

  • 主程序
#include <stdlib.h>
#include <stdio.h>
#include "LinkStack.h"
#include "Maze.h"


int MazeDFS(int x1, int y1, int x2, int y2, Maze* maze)
{
	int direction[8][2] = { {0,1}, {1,1}, {1,0}, {1,-1},{0,-1}, {-1,-1}, {-1,0}, {-1,1} };
	LinkStack X = NULL;
	LinkStack Y = NULL;
	int posx, posy;
	int prex, prey;
	int** mark;  //标记走过的点
	int j, i;
	int mov;
	mark = (int**)malloc(sizeof(int*) * maze->size);
	for (i = 0; i < maze->size; i++)
	{
		mark[i] = (int*)malloc(sizeof(int) * maze->size);
		for (j = 0; j < maze->size; j++)
			mark[i][j] = maze->data[i][j];
	}

	X = CreatStack();
	Y = CreatStack();
	mark[x1][y1] = 2;
	Push(X, x1);
	Push(Y, y1);
	while (!Empty(X))
	{
		prex = Top(X);
		prey = Top(Y);
		Pop(X);
		Pop(Y);
		mov = 0;
		//_getch();
		//printf("当前位置为 %d,%d 输入A键继续\n", prex, prey);
		//_getch();

		while (mov < 8)
		{
			posx = prex + direction[mov][0];
			posy = prey + direction[mov][1];
			int px = 0;
			int py = 0;

			if (maze->data[posx][posy] == 0 && mark[posx][posy] == 0)
			{
				mark[posx][posy] = 2;
				Push(X, prex);
				Push(Y, prey);
				prex = posx;
				prey = posy;

				//_getch();
				//printf("当前位置为 %d,%d 输入A键继续\n", prex, prey);
				//_getch();
				mov = 0;

			}
			else mov++;

			if (posx == x2 && posy == y2)
			{
				Push(X, prex);
				Push(Y, prey);
				printf("已到达,路径如下:\n");
				while (!Empty(X)) {
					printf("(%d,%d) <- ", Top(X), Top(Y));
					Pop(X);
					Pop(Y);
				}
				printf("(起点)\n");

				for (int i = 0; i < maze->size; i++)
				{
					for (int j = 0; j < maze->size; j++)
					{
						if (mark[i][j] == 1)
							printf("%c ", 1);//方块
						else if (mark[i][j] == 2)
							printf(" *");
						else
							printf("  ");
					}
					printf("\n");
				}

				return 1;
			}
		}

	}
	return 0;
}


int main()
{
	Maze* maze = InitMaze(9);
	ReadFile(maze,"./maze.txt");
	MazeDFS(1,1,9,8,maze);

	return 0;
}
  • 测试用例 Maze.h
0 0 0 1 1 1 1 1 1 
0 1 1 1 1 1 1 1 1 
0 0 0 1 0 0 1 1 1 
1 1 0 1 1 1 0 1 1 
1 1 0 1 1 1 1 0 1 
1 1 0 0 0 0 0 1 1 
1 1 0 1 0 1 0 1 1 
1 1 0 0 1 0 1 0 1 
1 1 1 0 1 0 0 0 1 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值