C语言[栈]求解迷宫[所有路径]

本文介绍了使用C++实现的基于深度优先搜索(DFS)的多路径寻找算法,通过LinkStack和自定义Maze结构体进行迷宫路径搜索。程序首先读取一个4x4或9x9的迷宫地图文件,然后从指定起点开始,寻找所有到达终点的路径。当找到一条路径时,会打印出该路径并更新迷宫地图,直至找到所有可能的路径。该算法在给定的测试用例中表现出良好的性能,能够有效解决迷宫路径查找问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 运行结果截图(八方向可走)
    所有路径

  • 手写栈 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 MazeDFSAll(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 stack = CreatStack();

	Point* point = (Point*)malloc(sizeof(struct Point));
	point->x = x1;
	point->y = y1;
	point->dis = -1;
	Push(stack, point);
	maze->data[x1][y1] = '*';//将入口的迷宫值置为*,避免重复走到该方块

	int count = 1;
	bool _find = false;
	int nx=0, ny=0;
	bool flag = false;

	while (!Empty(stack))
	{
		Point* curPoint = Top(stack);
		int curX = curPoint->x;
		int curY = curPoint->y;
		int curDis = curPoint->dis;

		if (curX == x2 && curY == y2)//找到了出口,输出该路径
		{
			flag = true;
			printf("迷宫路径%d为:\n", count++);
			PrintMaze(maze);
			printf("\n");
			
			Pop(stack);
			maze->data[curX][curY] = 0;
			free(curPoint);
		}
		else {
			_find = false;
			while (curDis < 8 && !_find)
			{
				curDis++;
				nx = curX + direction[curDis][0];
				ny = curY + direction[curDis][1];
				if (curDis < 8 && maze->data[nx][ny] == 0) _find = true;
			}

			if (_find)
			{
				//curPoint->dis = curDis; //?
				Top(stack)->dis = curDis;

				Point* temp = (Point*)malloc(sizeof(struct Point));
				temp->x = nx;
				temp->y = ny;
				temp->dis = -1;

				Push(stack, temp);
				maze->data[nx][ny] = '*';
				//PrintMaze(maze);
			}
			else
			{
				curPoint = Top(stack);
				Pop(stack);
				maze->data[curPoint->x][curPoint->y] = 0;
				free(curPoint);
				//PrintMaze(maze);
			}
		}
	}

	if (flag == 1)
	{
		printf("!!");
		return true;
	}
	return false;

}


int main()
{
	Maze* maze = InitMaze(4);
	ReadFile(maze, "./maze1.txt");
	MazeDFSAll(1, 1, 4, 4, maze);

	//Maze* maze = InitMaze(9);
	//ReadFile(maze, "./maze.txt");
	//MazeDFSAll(1, 1, 9, 8, maze);

	return 0;
}
  • 测试用例 maze.txt
0 1 0 0 
1 0 0 1 
0 1 0 0 
1 0 1 0 
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 
#include #include #define N1 9 #define N2 8 #define T N1*N2 #define M 4 char B[N1+1][N2+1]; int count=0; //记录路径条数 typedef struct node1 { int a1; int a2; }find,direct[M+1]; typedef struct { int b1; int b2; int id; }site; typedef struct //顺序 { site ht[T]; int top; }Stack; void Push(Stack *s,int a,int b) { s->top++; s->ht[s->top].b1=a; s->ht[s->top].b2=b; } void Gettop(Stack * s,int *a,int *b) { *a=s->ht[s->top].b1; *b=s->ht[s->top].b2; } void create(char *a) //从文件读出迷宫(正确) { int i=0,j=0,p=1; char x; FILE *fp; fp=fopen("in.txt","r"); if(fp==NULL) { printf("文件不能打开!\n"); exit(0); } x=fgetc(fp); while(x!=EOF) { if(x=='0') { i++; a[i]=x; } if(x=='1') { i++; a[i]=x; } x=fgetc(fp); } printf(" ~~~~~~~生成迷宫~~~~~~~\n"); x=fgetc(fp); while(p<=T) //用二维数组b记录迷宫每个位置是否可行 { for(i=1;i<=N1;i++) for(j=1;j<=N2;j++) { B[i][j]=a[p]; p++; } } printf(" "); printf("■■■■■■■■■■■■\n"); printf(" ■"); printf(" ■\n"); for(i=1;i<=N1;i++) { printf(" "); printf("■ "); for(j=1;jht[s1->top].id=id; B[x][y]='*'; while(s1->top>0) { Gettop(s1,&x,&y); id=s1->ht[s1->top].id; if(x==B1&&y==B2) { count++; fprintf(fp,"%d%c%c",count,':',' '); printf("第 %d 条路径(长度为%d):\n",count,s1->top); s1->ht[s1->top].id=0; for(i=1;itop;i++) { printf("(%d,%d,%d)->",s1->ht[i].b1,s1->ht[i].b2,s1->ht[i].id); fprintf(fp,"%c%d%c%d%c%d%c%c",'(',s1->ht[i].b1,',',s1->ht[i].b2,',',s1->ht[i].id,')',' '); if(i==0) fprintf(fp,"%c%c%c%c",'\n',' ',' ',' '); if(i%8==0) printf("\n"); } fprintf(fp,"%c",'\n'); printf("结束!\n\n"); if(s1->toptop=s1->top; min=s1->top; for(i=1;itop;i++) s2->ht[i]=s1->ht[i]; } B[x][y]='0'; s1->top--; //退(s1->top--) Gettop(s1,&x,&y); id=s1->ht[s1->top].id; } fun=0; while(idht[s1->top].b1; y=s1->ht[s1->top].b2; x=x+p[id].a1; y=y+p[id].a2; if(x==0||y==0||x>N1||y>N2) continue; if(B[x][y]=='0') { fun=1; break; } } if(fun==1) //找到通路 { s1->ht[s1->top].id=id; Push(s1,x,y); B[x][y]='*'; s1->ht[s1->top].id=0; } else { x=s1->ht[s1->top].b1; y=s1->ht[s1->top].b2; B[x][y]='0'; s1->top--; } } if(count==0) printf(" 无路径!\n"); else { printf("\n\n\n "); printf("所有路径已存储在文件%s 中,请去查找!\n\n",filename); } return 1; } void Print(Stack *s2,char filename[]) { int i; FILE *fp; fp=fopen(filename,"a+"); if(fp==NULL) { printf("文件不能打开!\n"); exit(0); } if(count!=0) { fprintf(fp,"%s","最短路径为:"); fprintf(fp,"%c",'\n'); printf(" "); printf("%s\n","**********最短路径**********\n"); for(i=1;itop;i++) { printf("(%d,%d,%d) ->",s2->ht[i].b1,s2->ht[i].b2,s2->ht[i].id); fprintf(fp,"%c%d%c%d%c%d%c%c",'(',s2->ht[i].b1,',',s2->ht[i].b2,',',s2->ht[i].id,')',' '); if(i==0) fprintf(fp,"%c",'\n'); if(i%7==0) printf("\n"); } fprintf(fp,"%c",'\n'); printf("结束!\n"); printf("(最短路径长度: %d)\n",s2->top); } } void main() //主函数 { char a[T+1]; //二维数组b记录迷宫的每个位置 char filename1[20],filename2[20]; int x1,x2,y1,y2,k; Stack *s1,*s2; direct f1; f1[1].a1=0; f1[1].a2=1; //判断方向(右) f1[2].a1=1; f1[2].a2=0; //(下) f1[3].a1=0; f1[3].a2=-1; //(左) f1[4].a1=-1; f1[4].a2=0; //(上) s1=(Stack *)malloc(sizeof(Stack)); s2=(Stack *)malloc(sizeof(Stack)); s1->top=0; //指向顶(初始化) s2->top=0; create(a); printf("\n\n "); printf("请输入入口坐标: "); scanf("%d%d",&x1,&x2); printf(" "); printf("请输入出口坐标: "); scanf("%d%d",&y1,&y2); printf(" "); printf("请输入存储所有路径的文件名:"); scanf("%s",filename1); printf(" "); printf("请输入存储最短路径的文件名:"); scanf("%s",filename2); system("cls"); k=search(x1,x2,y1,y2,s1,s2,f1,filename1); if(k==1) Print(s2,filename2); printf("\n"); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值