迷宫问题(C语言实现)

向大家道歉,之前由于我的个人原因,代码中间出现了一些错误,由于时间长,没有及时更改,今天专门检查了一下并且看了评论发现自己之前的代码的确存在问题,谢谢大家的指正,现将改正后代码贴上,如有问题请及时指正。感谢!!!

问题描述

定义一个二维数组:

int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
Input
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
Output
左上角到右下角的最短路径,格式如样例所示。
Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

思路(DFS寻路)

  • 首先将迷宫存在一个二位数组中,然后遍历数组,遇到1则改变方向,遇到0接着向下走,将此点存入栈中,如果无路,则返回上一结点,将此结点移出栈,并将此点的数组值更改为1,防止死循环。直至找到出口。
struct path
{
	int x;            //横座标
	int y;  		  //纵座标
	struct path *next;
};

typedef struct Link
{
	struct path *head;
}link;

//此处已将队列更换为栈,因为之前的队列删除尾节点会将队列结构完全破坏,而造成队列不能正确工作,sorry!!!


//模拟为栈
void push(link *que, int x, int y)
{
	struct path *p;
	p = (struct path*)malloc(sizeof(struct path));
	p->x = x;
	p->y = y;
	p->next = que->head;;
	que->head = p;
/* que->rear->next = p; que->rear = p; que->rear->next = NULL; */
}

void pop(link *que)
{
	//如果不合适将刚入栈的节点删除
	que->head = que->head->next;  //删除栈的头结点就会变得很简单
}

//此处已经不需要这个函数,太多余!
/* void pop2(link *que, int *x, int *y) { struct path *temp; temp = que->head->next; que->head->next = temp->next; //将temp结点出队 *x = temp->x; *y = temp->y; free(temp); } */
  • 写好队列的实现后,我们来实现对迷宫的分析,此处没有更正,递归是正确的!
/*用递归对迷宫进行遍历*/
void r(int a[5][5], int x, int y, link *que)
{   
    if( x < 5 && x >= 0 && y < 5 && y >= 0)
    {
        if(x == 4 && y == 4)  //到达迷宫出口
            return;
        /*首先判断右边下一个点是否可以走*/
        if(a[x][y + 1] == 0 && (y + 1) < 5) //此处为了防止数组越界
        {
            y++;
            push(que, x, y);   //可以走就将该结点入队
            r(a, x, y, que);   //递归下一个座标
            return;
        }
        /*上一个条件不满足就判断其向下是否能走*/
        if(a[x + 1][y] == 0 && (x + 1) < 5)//防止数组越界
        {
            x++;
            push(que, x, y);       //入队
            r(a, x, y, que);       //递归
            return;
        }
        /*无路可走,就返回上一个座标*/
        if(a[x - 1][y] == 0 && x - 1 >= 0)
        {
            a[x][y] = 1; //并将该点设为1,防止死循环
            pop(que);    //将该座标移出队列
            x--;
            r(a, x, y, que);
            return;
		 }
        if(a[x][y - 1] == 0 && y - 1 >= 0)
        {
            a[x][y] = 1;
            pop(que); //移出队列
            y--;
            r(a, x, y, que);
            return;
        }
    }
}   

总结

1、用到递归和栈的思想,将迷宫当作矩阵,遍历,判断下一个座标是否可以通过,如果不行则换方向,如果无路可走则退格。
2、自己今天有很多思想没有思考到,写一篇博客加深一下印象。

博客如有错误,欢迎大家评论指出,一起探讨一起进步。感谢!!

完整代码

#include<stdio.h>
#include<stdlib.h>

//迷宫问题

struct path
{
	int x;            //横座标
	int y;  		  //纵座标
	struct path *next;
};

typedef struct Link
{
	struct path *head;
}link;


//模拟为栈
void push(link *que, int x, int y)
{
	struct path *p;
	p = (struct path*)malloc(sizeof(struct path));
	p->x = x;
	p->y = y;
	p->next = que->head;;
	que->head = p;
/* que->rear->next = p; que->rear = p; que->rear->next = NULL; */
}

void pop(link *que)
{
	//如果不合适将刚入栈的节点删除
	que->head = que->head->next;
}

/* void pop2(link *que, int *x, int *y) { struct path *temp; temp = que->head->next; que->head->next = temp->next; //将temp结点出队 *x = temp->x; *y = temp->y; free(temp); } */

void r(int a[5][5], int x, int y, link *que)
{
	if( x < 5 && x >= 0 && y < 5 && y >= 0)
	{
		if(x == 4 && y == 4)
			return;
		if(a[x][y + 1] == 0 && (y + 1) < 5)
		{
			y++;
			push(que, x, y);
			r(a, x, y, que);
			return;
		}
	    if(a[x + 1][y] == 0 && (x + 1) < 5)
		{
			x++;
			push(que, x, y);
			r(a, x, y, que);
			return;
		}
	    if(a[x - 1][y] == 0 && x - 1 >= 0)
		{
			a[x][y] = 1;
			pop(que);
			x--;
			r(a, x, y, que);
			return;
		}
	    if(a[x][y - 1] == 0 && y - 1 >= 0)
		{
			a[x][y] = 1;
			pop(que);
			y--;
			r(a, x, y, que);
			return;
		}
	}
}

int main(void)
{
	int a[5][5];
	int i, j;
	int x, y;
	for(i = 0; i < 5; i++)
	{
		for(j = 0; j < 5; j++)
		{
			scanf("%d", &a[i][j]);
		}
	}
	link *top, *temp;
	top = temp = (link *)malloc(sizeof(link));
	top->head = (struct path *)malloc(sizeof(struct path));
	r(a, 0, 0, top);
	printf("(0, 0)\n");
	//此处数组是为了存储路径,因为使用的是栈结构,所以存储的路径必然是倒序!
	int PATH[25] = {0};
	int count = 0;

	//将栈倒序的路径存储进数组中,二维数组太占用空间所以此处我们使用一维数组,并使用数学方法存储!!
	while(top->head->next != NULL)
	{
		PATH[count] = top->head->x *10 + top->head->y;
		++count;
		top->head = top->head->next;
	}
	for(i = count - 1; i >= 0; --i)
	{	
		int num_x = PATH[i] / 10;  //从数据中提取出X
		int num_y = PATH[i] % 10; //从数组中提取出Y
		//打印路径
		printf("(%d, %d)\n", num_x, num_y);
	}
	return 0;
}                                                                                                         
    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/weixin_42250655/article/details/81294176
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞