C语言走迷宫

写在前面

用C语言也可以写出很多好玩的东西,这篇博客介绍一下如何用C语言来走迷宫。

实现原理

首先用到的就是栈这样一种数据结构,有先进后出的特性。
另外走迷宫还用到了回溯的思想,就是若此条道路行不通,就返回上次分叉的那个路口,换一条道,直到找到出口或者遍历结束。(和此代码中注释中的“回溯”意思不一样)
具体的细节就在代码中了,代码如下:

代码实现

代码运行截图
运行截图

#include <stdio.h>
#include <windows.h>

#define ROW 5
#define COL 5
#define MAX 512


/*****************************stack*******************************/
typedef struct point
{
    int x;
    int y;
}point;

int top = 0;
point stack[MAX] = {{0, 0}};

point push(point p)
{
    stack[top] = p;
    top++;
}
point pop()
{
    top--;
    return stack[top];
}
int is_empty()
{
    return top == 0;
}
int is_full()
{
    return top == MAX;
}

void printf_stack()
{
    int i = 0;
    for (i = 0; i < top; i++)
    {
        printf("[%d, %d] ", stack[i].y, stack[i].x);
    }
    printf("\n");
}
/*****************************stack*******************************/

原始迷宫数组
//int maze[ROW][COL] =
//{
//    0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
//    0, 0, 0, 1, 1, 1, 0, 1, 0, 0,
//    0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
//    0, 1, 0, 1, 0, 1, 0, 0, 1, 0,
//    0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
//    0, 1, 1, 1, 0, 0, 1, 0, 0, 0,
//    0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
//    0, 1, 0, 0, 0, 1, 0, 1, 1, 0,
//    0, 1, 1, 1, 1, 1, 0, 1, 0, 0,
//    0, 0, 0, 0, 0, 0, 0, 1, 0, 0
//};

//原始迷宫数组
int maze[ROW][COL] =
{
    0, 1, 0, 1, 0,
    0, 1, 0, 0, 0,
    0, 1, 0, 1, 0,
    0, 1, 0, 1, 0,
    0, 0, 0, 1, 0
};

//记录路径数组
point maze_path[ROW][COL] ={{0, 0}};
//用来打印路线的数组
int maze_show[ROW][COL] ={ 0 };

void printf_maze()
{
    int i = 0;
    int j = 0;

    for (i = 0; i < ROW; i++)
    {
        for (j = 0; j < COL; j++)
        {
            printf("%d ", maze[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

void printf_path()
{
    int i = 0;
    int j = 0;

    for (i = 0; i < ROW; i++)
    {
        for (j = 0; j < COL; j++)
        {
            printf("[%d %d] ", maze_path[i][j].y, maze_path[i][j].x);
        }
        printf("\n");
    }
    printf("\n");
}
void printf_show()
{
    int i = 0;
    int j = 0;

    //输出颜色控制
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

    for (i = 0; i < ROW; i++)
    {
        for (j = 0; j < COL; j++)
        {
            if (maze_show[i][j] == 1)
            {
                SetConsoleTextAttribute(handle,
                    FOREGROUND_INTENSITY | FOREGROUND_RED); //红色高亮显示
            }
            else SetConsoleTextAttribute(handle, 0x07);     //正常打印

            printf("%d ", maze_show[i][j]);

            SetConsoleTextAttribute(handle, 0x07);          //正常打印
        }
        printf("\n");
    }
    printf("\n");
}

int main()
{
    point p = {0, 0};
    point p_next = {0, 0};
    maze_path[0][0] = p;
    push(p);

    while (!is_empty())
    {
        p = pop();  //弹出栈顶的点
        if (p.x == COL - 1 && p.y == ROW - 1) //达到终端(右下角)
        {
            break;
        }

        p_next.x = p.x + 1;     //right
        p_next.y = p.y;
        if (p_next.x < COL && maze[p_next.y][p_next.x] == 0)
        {
            maze[p_next.y][p_next.x] = 2; //2表示已经走过这个点
            maze_path[p_next.y][p_next.x] = p;  //当前点记录上一个点的坐标,用来回溯找路径
            push(p_next);   //下个点入栈
        }

        p_next.x = p.x;         //down
        p_next.y = p.y + 1;
        if (p_next.y < ROW && maze[p_next.y][p_next.x] == 0)
        {
            maze[p_next.y][p_next.x] = 2;
            maze_path[p_next.y][p_next.x] = p;
            push(p_next);
        }

        p_next.x = p.x - 1;     //left
        p_next.y = p.y;
        if (p_next.x >= 0 && maze[p_next.y][p_next.x] == 0)
        {
            maze[p_next.y][p_next.x] = 2;
            maze_path[p_next.y][p_next.x] = p;
            push(p_next);
        }

        p_next.x = p.x;         //up
        p_next.y = p.y - 1;
        if (p_next.y >= 0 && maze[p_next.y][p_next.x] == 0)
        {
            maze[p_next.y][p_next.x] = 2;
            maze_path[p_next.y][p_next.x] = p;
            push(p_next);
        }
    }

    //回溯寻找路线
	if (p.y == ROW - 1 && p.x == COL - 1)
	{
        maze_show[p.y][p.x] = 1;
		while (p.y != 0 || p.x != 0)
		{
			p = maze_path[p.y][p.x];
			maze_show[p.y][p.x] = 1;
		}

        printf_maze();
        printf_path();
        printf_show();
	}
	else printf("No path to exit!\n");


    return 0;
}


与我联系

QQ:1570553025
github:https://github.com/myzcl
微信公众号,扫二维码即可关注:
在这里插入图片描述

  • 7
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chasentech

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值