迷宫算法(DFS深度优先)C语言(栈)实现,附源码

#include<stdio.h>
#include <stdlib.h>
#include <stdbool.h> //C语言中的bool类型需要这个头文件
#include <assert.h>
#define MAX_len 21 
#define MAX_long 1000		//栈最大长度 


int zhuan_y1[100]={0};
int zhuan_x1[100]={0};
int zhuan[100]={0};

 int maxmap[21][21]={
// 0   1   2   3   4   5   6   7   8   9 
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1},//0
{1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1},
{1,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1},//1
{1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1},
{1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1},//2
{1,1,1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1},
{1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1},//3
{1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1},
{1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//4
{1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1},//5
{1,0,1,1,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,0,1},
{1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1},//6
{1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,0,1,1,1},
{1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1},//7
{1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1},
{1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1},//8
{1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1},
{1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},//9
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
// 0   1   2   3   4   5   6   7   8   9 
};

enum TEXT_node{Up=0,Down,left,Right};	//试探方向 

void make_map(int *map)				//打印地图 
{
	int*p=map,i,j;
	printf(" \n");
	for(i=0;i<21;i++)
	{
		for(j=0;j<21;j++)
		{
			switch(*p)
			{
				case 0 :printf("  ");break;
				case 1 :printf("墙");break;
				case 2 :printf("十");break;
			}
			p++;
		}
		printf("\n");
	}
	printf(" \n");
}

typedef struct {
	int x;		//横轴 
	char y;		//0纵轴 
	
}POINT;				//点结构体 

typedef struct {
	int next_node;		//试探方向;
	char flag_pass;		//0没走 1走了 
	
}TEXT_POINT;		//栈结构体 


TEXT_POINT node[MAX_len][MAX_len]={0};		//辅助地图 

POINT Correct[MAX_long]={0};		//模拟栈 

int head=0;		//栈头_指针 

int full ()	//0未满  1满了 
{
	if(head <= MAX_long)
		return 0;
	else return 1;
	
}
int null ()	//0未空 1空了 
{
	if(head != 0 ) 
		return 0;
	else return 1;	
}
void push(POINT n)	//进栈 
{	
    if(full()==0)
    {
//    	printf("入栈前: x:%d    y:%d    head: %d\n\n",Correct[head].x,Correct[head].y,head);
		Correct[head].x=n.x;
		Correct[head].y=n.y;
//		printf("入栈后: x:%d    y:%d    head: %d\n\n",Correct[head].x,Correct[head].y,head);
				
		head++; 	//不可以写在 Correct[head++]	否则会加两次 	
	}
}
POINT pop(char flag_return)	//出栈		0无需返回值  1需要返回点 
{
	if(null()==0)
    {
	
		
     	head--;
    	POINT i=Correct[head-1];                          
//    	printf("出栈前: x:%d    y:%d    head: %d\n\n",Correct[head].x,Correct[head].y,head);    
		Correct[head].x=0;
		Correct[head].y=0;

//		printf("出栈后: x:%d    y:%d    head: %d\n\n",Correct[head].x,Correct[head].y,head);    
		if(flag_return)
			return i;
	}
}
int *map_show(int a,int b,int c,int d)
{ 
	long int i=1000,j;
	static long int number=0;
	int e,q,m,n,p,t=0,k=0;
	POINT start={a,b};
	POINT end={c,d};
	push(start); 
	push(end); 
		
	push(start); 
	push(end); 
		
	push(start); 
	push(end); 
	
	pop(0); 
	pop(0);
	pop(0);	
	POINT now_point=start;		//当前点 
	POINT text_point;			//试探点 
	
	text_point=now_point;
	
	node[now_point.x][now_point.y].flag_pass=1;		//起点走了 
	push(start);
	node[text_point.x][text_point.y].next_node=Up;	//初始化向上
//	make_map(maxmap);
	while(i--)
	{
		
		text_point=now_point;
		switch(node[now_point.x][now_point.y].next_node)		//判断试探方向 
		{
			case Up : 
			{
//				printf("u");
				number++;
				text_point.x--;
				node[now_point.x][now_point.y].next_node=Down;	//改试探方向 
				if(node[text_point.x][text_point.y].flag_pass==0 && maxmap[text_point.x][text_point.y]==0)	//还没走且能走(有路)
				{
					node[text_point.x][text_point.y].flag_pass=1;	//标记走过 
					push(text_point);								//入栈 
//					maxmap[text_point.x][text_point.y]=2;
//					printf("1 %d %d\n",Correct[head].x,Correct[head].y);
					now_point = text_point; 						//走 
				} 	
			}
			break;
			
			case Right : 
			{
//				printf("r");
				text_point.y++;
				node[now_point.x][now_point.y].next_node=left;	//改试探方向 
				if(node[text_point.x][text_point.y].flag_pass==0 && maxmap[text_point.x][text_point.y]==0)	//还没走且能走(有路)
				{
					node[text_point.x][text_point.y].flag_pass=1;	//标记走过
					push(text_point);								//入栈 
//					maxmap[text_point.x][text_point.y]=2;
//					printf("2 %d %d\n",Correct[head].x,Correct[head].y);
					now_point = text_point; 						//走 
//					printf("x=%dy=%d %d",now_point.x,now_point.y,node[now_point.x][now_point.y].next_node);
				} 
				
			}
				break;
			case Down : 
			{
//				printf("d");
				text_point.x++;
				node[now_point.x][now_point.y].next_node=Right;	//改试探方向 
				if(node[text_point.x][text_point.y].flag_pass==0 && maxmap[text_point.x][text_point.y]==0)	//还没走且能走(有路)
				{
					node[text_point.x][text_point.y].flag_pass=1;	//标记走过
			push(text_point);								//入栈 
//					maxmap[text_point.x][text_point.y]=2;
//					printf("3 %d %d\n",Correct[head].x,Correct[head].y);
					now_point = text_point; 						//走 
				} 
					
			}
			break;	
			case left :
			{
//				printf("l");
				text_point.y--;
			//	node[now_point.x][now_point.y].next_node=Up;	//改试探方向 	
				if(node[text_point.x][text_point.y].flag_pass==0 && maxmap[text_point.x][text_point.y]==0)	//还没走且能走(有路)
				{
					node[text_point.x][text_point.y].flag_pass=1;	//标记走过
					push(text_point);								//入栈 
//					maxmap[text_point.x][text_point.y]=2;
//					printf("4 %d %d\n",Correct[head].x,Correct[head].y);
					now_point = text_point; 					 	//走 
				}
				else {		//不能走则需要回退(遇到死胡同) 
							//出栈 		
						now_point = pop(1);
				} 
			} 
			break;	
		}
		
		if(now_point.x==end.x && now_point.y==end.y)	//判断是否为终点 
		{
		//	printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n           find END!!\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"); 
			break;
		}
	}
	for(i=0;i<200;i++)		//将栈内元素写入地图   2为走过的路 
	{
		maxmap[Correct[i].x][Correct[i].y]=2;
	}	
	maxmap[0][0]=1;
	make_map(maxmap[0]);
	
	for(i=0;i<200;i++)		//将栈内元素写入地图   2为走过的路 
	{
		if(maxmap[Correct[i].x][Correct[i].y]==2)
		{ 
			if(Correct[i].x!=Correct[i+1].x||Correct[i].y!=Correct[i+1].y)
			{
				e=Correct[i].x;
				q=Correct[i].y;
				zhuan_y1[k]=q;
				zhuan_x1[k]=e;
			//	printf("(%d,%d)",zhuan_x[k],zhuan_y[k]);
				k++;
			}
		}
	}
/*	
	for(n=0;n <=k-1;n++)
	{
		printf("(%d ",zhuan_x1[n]);
		printf("%d) ",zhuan_y1[n]);	
	}
*/
	for(m=1;m<=k-1;m++)
	{
		if((zhuan_x1[m]==zhuan_x1[m+2]+1)
		&&(((zhuan_y1[m]==(zhuan_y1[m+2]-1))&&(zhuan_x1[m]==zhuan_x1[m+1]))
		||((zhuan_y1[m]==(zhuan_y1[m+2]+1))&&(zhuan_y1[m]==zhuan_y1[m+1]))))
		{
			zhuan[t]=1;
		//	printf("%d ",zhuan[t]);
			t++;
		}
		else if((zhuan_x1[m]==zhuan_x1[m+2]-1)
		&&(((zhuan_y1[m]==(zhuan_y1[m+2]+1))&&(zhuan_x1[m]==zhuan_x1[m+1]))
		||((zhuan_y1[m]==(zhuan_y1[m+2]-1))&&(zhuan_y1[m]==zhuan_y1[m+1]))))
		{
			zhuan[t]=1;
		//	printf("%d ",zhuan[t]);
			t++;
		}
		else if((zhuan_y1[m]==zhuan_y1[m+2]-1)
		&&(((zhuan_x1[m]==(zhuan_x1[m+2]+1))&&(zhuan_y1[m]==zhuan_y1[m+1]))
		||((zhuan_x1[m]==(zhuan_x1[m+2]-1))&&(zhuan_x1[m]==zhuan_x1[m+1]))))
		{
			zhuan[t]=0;
		//	printf("%d ",zhuan[t]);
			t++;
		}
		else if((zhuan_y1[m]==zhuan_y1[m+2]+1)
		&&(((zhuan_x1[m]==(zhuan_x1[m+2]-1))&&(zhuan_y1[m]==zhuan_y1[m+1]))
		||((zhuan_x1[m]==(zhuan_x1[m+2]+1))&&(zhuan_x1[m]==zhuan_x1[m+1]))))
		{
			zhuan[t]=0;
		//	printf("%d ",zhuan[t]);
			t++;
		}
	}
/*	for(p=0;p<=t-1;p++)
	{
		printf("%d ",zhuan[p]);	
	}
*/
	printf("%d \n",k);
	return zhuan;
}
void main()
{
	int a=0;
	int *p;
	p=map_show(19,1,1,19);
//	printf("**************************");
//	p=map_show(19,1,13,1);
//	p=map_show(13,1,13,9);
//	p=map_show(13,9,1,19);
	for(a=0;a<=20;a++)
	{
		printf("%d ",zhuan[a]);	
	}
	printf("\n%d ",a);	
}

没有实现最短路径,只是找到路了

 程序运行结果

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值