C语言数据结构:迷宫路径问题探究

问题描述
给定一个M*N的迷宫,给出起点终点坐标,每次只能向前后左右移动一格,求一条从起点到终点的路径(所求路径必须是简单路径,即路径不重复)
为了方便理解,我们先给定一个简单迷宫int mg[6][6]=
{
{1,1,1,1,1,1},
{1,0,0,0,1,1},
{1,0,1,0,0,1},
{1,0,0,0,1,1},
{1,1,0,0,0,1},
{1,1,1,1,1,1}

}; 规定0为可走方块,1为不可走方块(即墙壁)
设起点为(1,1),终点为(4,4)。(行列坐标皆从0开始)
简单说一下栈和队列,它们都是存放数据的容器,通常用于存放临时数据。栈实际上是一个后进先出表,队列是一个先进先出表。
下面先采用顺序栈来求解该问题:

准备阶段:

#include<stdio.h>
#include<stdbool.h>		\\该头文件可将bool类型添加进c语言中
typedef struct
{
	int i;					\\方块行标
	int j;					\\方块列标
	int di;				\\方位变量
} box;
typedef struct
{
	box data[25];\\存放路径坐标
	int top;			\\指向栈顶
} stktype;		\\定义一个顺序栈类型
int mg[6][6]={{1,1,1,1,1,1},{1,0,0,0,1,1},{1,0,1,0,0,1},{1,0,0,0,1,1},{1,1,0,0,0,1},{1,1,1,1,1,1}};

函数定义:

bool mgpath(int xi,int yi,int xe,int ye)	 //xi,yi为起点,xe,ye为终点
{
	int i,j,di,k,find;
	stktype st;			\\定义顺序栈
	st.top=-1;
	st.top++;				\\元素进栈
	st.data[st.top].i=xi;
	st.data[st.top].j=yi;
	st.data[st.top].di=-1;
	mg[xi][yi]=-1;		\\将其值置为-1,避免重复走
	while(st.top>-1)  \\栈不为空时开始循环
	{
		i=st.data[st.top].i;
		j=st.data[st.top].j;
		di=st.data[st.top].di;
		if(i==xe && j==ye) 		\\如果取出的栈顶元素恰好为终点
		{
			printf("迷宫路径如下:\n");
			for(k=0;k<=st.top;k++)
			{
				printf("\t(%d,%d)",st.data[k].i,st.data[k].j);
				if((k+1)%5==0)
					printf("\n");
			}
			printf("\n");
			return true;		//找到一条路径则返回true
		}
		//否则:
		find = 0;
		while(di<4 && find==0)//找下一个可走方块 
		{
			di++;
			switch(di) 	 //为了方便,规定一个方块的上下左右方位分别是0,2,3,1
			{
				case 0:i=st.data[st.top].i-1;j=st.data[st.top].j;break;
				case 1:i=st.data[st.top].i;j=st.data[st.top].j+1;break;
				case 2:i=st.data[st.top].i+1;j=st.data[st.top].j;break;
				case 3:i=st.data[st.top].i;j=st.data[st.top].j-1;break;
			}
			if(mg[i][j]==0) find=1;//找到了下一个可走方块i,j 
		}
		if(find==1)	//如果能找到下一个可走方块 
		{
			st.data[st.top].di=di;//修改原栈顶元素di的值 
			st.top++;//下一个可走方块进栈 
			st.data[st.top].i=i;st.data[st.top].j=j;st.data[st.top].di=-1;
			mg[i][j]=-1;
		}
		else//没找到可走方块 
		{
			mg[st.data[st.top].i][st.data[st.top].j]=0;
			st.top--;//退栈 
		}
	}
}


函数实现:

void main()
{
	if(!mgpath(1,1,4,4))
		printf("该迷宫问题无解"); 
}

结果:

**迷宫路径如下:
        (1,1)   (1,2)   (1,3)   (2,3)   (3,3)
        (4,3)   (4,4)
--------------------------------
Process exited after 2.621 seconds with return value 0
请按任意键继续. . .**

该算法实际上是先寻找当前方块相邻的可走方块(按0、1、2、3的顺序)并将其进栈,一直进行下去直到找到终点,就把栈中元素依次输出。如果一个方块周围皆不可走,则退栈,重新搜索。由这种结构思想限制,该算法只能找到一条路径,如果想找到多条路径,那么就要用到队列。

顺序队实现

#include<stdio.h>
#include<stdbool.h>
typedef struct
{
	int i;
	int j;
	int pre;		//本路径中上一方块在队列中的下标 
} box;
typedef struct
{
	box data[25];
	int front,rear;			//队头队尾指针 
} qutype;						//顺序队 
int mg[6][6]={{1,1,1,1,1,1},{1,0,0,0,1,1},{1,0,1,0,0,1},{1,0,0,0,1,1},{1,1,0,0,0,1},{1,1,1,1,1,1}};
bool mgpath(int xi,int yi,int xe,int ye)
{
	int a,i,j,di,find=0;
	qutype qu;
	qu.front=qu.rear=-1;
	qu.rear++;
	qu.data[qu.rear].i=xi;qu.data[qu.rear].j=yi;			//xi,yi 进队 
	qu.data[qu.rear].pre=-1;
	mg[xi][yi]=-1;
	while(qu.front != qu.rear && !find)//队列非空开始循环 
	{
		qu.front++;//出队 
		i=qu.data[qu.front].i;j=qu.data[qu.front].j;
		if(i==xe && j==ye)//找到了出口,输出路径 
		{
			find=1;
			while(qu.front > 0) 输出队中所有元素
			{
				
				printf("\t(%d,%d)",qu.data[qu.front].i,qu.data[qu.front].j);
				qu.front--;
				printf("\n");
			}
			return true;
		}
		//否则把当前出队方块的每个相邻的可走方块进队 
		for(di=0;di<4;di++)
		{
			switch(di)
			{
				case 0:i=qu.data[qu.front].i-1;j=qu.data[qu.front].j;break;
				case 1:i=qu.data[qu.front].i;j=qu.data[qu.front].j+1;break;
				case 2:i=qu.data[qu.front].i+1;j=qu.data[qu.front].j;break;
				case 3:i=qu.data[qu.front].i;j=qu.data[qu.front].j-1;break;
			}
			if(mg[i][j]==0)
			{
				qu.rear++;//将该相邻方块插入到队列中
				qu.data[qu.rear].i=i;qu.data[qu.rear].j=j;qu.data[qu.rear].pre=qu.front;
				mg[i][j]=-1; 
			}
		}
	}
	return false;//未找到一条路径 
} 
void main()
{
	if(!mgpath(1,1,4,4))
		printf("该迷宫问题无解"); 
}

结果:
        (4,4)
        (4,3)
        (4,2)
        (3,3)
        (2,4)
        (3,2)
        (2,3)
        (3,1)
        (1,3)
        (2,1)
        (1,2)

--------------------------------
Process exited after 0.5932 seconds with return value 0
请按任意键继续. . .

该解法整体结构与栈解法类似,但此解法将当前方块周围所有可走方块放入队列中,我们最后需从终点反向找到可走路径。易知该解法可找到多条路径,但处理过于麻烦,而且并不能找到所有路径。
那么我们想有没有一种方法可以简单的找到所有路径呢?
这时候,就要用到递归的思想了。
先给出实现代码:

#include<stdio.h>
typedef struct
{
	int i;  //当前方块行号 
	int j;  //当前方块列号
} box;
typedef struct
{
	box data[25];
	int length;
} pathtype;
int mg[6][6]={{1,1,1,1,1,1},{1,0,0,0,1,1},{1,0,1,0,0,1},{1,0,0,0,1,1},{1,1,0,0,0,1},{1,1,1,1,1,1}};
int count = 0;
void mapath(int xi,int yi,int xe,int ye,pathtype path)
{
	int di,k,i,j;
	if (xi==xe && yi==ye)  //递归出口
	{
		path.data[path.length].i = xi;
		path.data[path.length].j = yi;
		path.length++;
		printf("迷宫路径如下%d:\n",++count);
		for(k=0;k<path.length;k++)
		{
			printf("\t(%d,%d)",path.data[k].i,path.data[k].j);
			if((k+1)%5==0)
				printf("\n");
		}
		printf("\n");
	}
	else
	{
		if(mg[xi][yi]==0)  //如果是可走方块
		{
			di=0;
			while(di<4)
			{
				switch(di)
				{
					case 0:i=xi-1;j=yi;break;
					case 1:i=xi;j=yi+1;break;
					case 2:i=xi+1;j=yi;break;
					case 3:i=xi;j=yi-1;break;
				}
				path.data[path.length].i=xi;
				path.data[path.length].j=yi;
				path.length++;
				mg[xi][yi]=-1;
				mapath(i,j,xe,ye,path);    //如果找到可走方块则进行递归调用
				path.length--;   //找不到时将长度减一,回溯重新循环
				mg[xi][yi] = 0;
				di++;
			}
		}
	}
}
main()
{
	pathtype path;
	path.length=0;
	mapath(1,1,4,4,path);
}
结果:
迷宫路径如下1:
        (1,1)   (1,2)   (1,3)   (2,3)   (3,3)
        (4,3)   (4,4)
迷宫路径如下2:
        (1,1)   (1,2)   (1,3)   (2,3)   (3,3)
        (3,2)   (4,2)   (4,3)   (4,4)
迷宫路径如下3:
        (1,1)   (2,1)   (3,1)   (3,2)   (3,3)
        (4,3)   (4,4)
迷宫路径如下4:
        (1,1)   (2,1)   (3,1)   (3,2)   (4,2)
        (4,3)   (4,4)

--------------------------------
Process exited after 1.694 seconds with return value 4206656
请按任意键继续. . .

可以看出,该解法找出了所有的可走路径。
递归思想实际上是将一个大问题转化为小问题,这种方法明显减少了代码量,但却大大增加了思维量。
不过递归思想在计算机科学中非常重要,只要勤加练习,总可以熟练掌握,为自己的代码之路增加一个强有力的武器。
初入编程,如有什么错误或需要改进的地方还望各位大佬指点。

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#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"); }
问题描述: 以一个m*n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出从入口(0,0)到出口(m-1,n-1)的通路和通路总数,或得出没有通路的结论。例如下图, 0(入口) 1 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0(出口) 从入口到出口有6条不同的通路。 而下图: 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 从入口到出口则没有通路。 算法设计: 给定一个m*n的长方阵表示迷宫,设计算法输出入口到出口的通路和通路总数,或得出没有通路的结论。 算法提示: 和皇后问题与分书问题类似。可以用二维数组存储迷宫数据,对于迷宫中任一位置,均可约定有东、南、西、北四个方向可通。从当前位置a(用(x,y)表示一个位置,假定它是以向右的x轴和向下的y轴组成的平面上的一个点)出发依次尝试四个方向是否有路,若某个方向的位置b可通,则按照同样的方法继续从b出发寻找。若到达出口,则找到一条通路。 数据输入: 由文件input.txt 提供输入数据。第一行是m和n的值,空格分隔,其后共m行。每行有n个数字,数和数之间用空格分隔。 结果输出: 将计算出的所有从入口到出口的通路输出到文件output.txt 中。若没有通路,则将0写入文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值