带环迷宫

#pragma once

/************************************/
//Stack.h

#define MAXSIZE 10

//typedef int SDataType;
//typedef char SDataType;

///////////////////////////
//迷宫定义的结构体与栈的数据类型不同

typedef struct Position
{
    int _x;
    int _y;
}Position;
typedef Position SDataType;
//////////////////////////

typedef struct Stack
{
    SDataType _array[MAXSIZE];
    int _top;   //标记栈顶元素
}Stack;

//初始化
void StackInit(Stack *ps);

//压栈
void StackPush(Stack *ps, SDataType data);

//出栈
void StackPop(Stack *ps);

//返回栈顶元素
SDataType StackTop(Stack *ps);

//返回栈空间长度
int StackSize(Stack *ps);

//栈是否为空
int StackEmpty(Stack *ps);

/****************************************/
//Stack.c
#include <assert.h>
#include <stdio.h>
#include <string.h>


//初始化
void StackInit(Stack *ps)
{
    assert(ps);
    ps->_top = 0;
}

//压栈
void StackPush(Stack *ps, SDataType data)
{
    assert(ps);
    if (MAXSIZE == ps->_top)
    {
        printf("栈空间已满,无法插入!!!\N");
        return;
    }
    ps->_array[ps->_top++] = data;
}

//出栈
void StackPop(Stack *ps)
{
    assert(ps);
    if (StackEmpty(ps))
        return;
    ps->_top--;
}

//返回栈顶元素
SDataType StackTop(Stack *ps)
{
    assert(ps);
    return ps->_array[ps->_top - 1];
}

//返回栈空间长度
int StackSize(Stack *ps)
{
    assert(ps);
    return ps->_top;
}

//栈是否为空
int StackEmpty(Stack *ps)
{
    assert(ps);
    return 0 == ps->_top;
}



/////////////////////////////////////////////////
#pragma once

#include <assert.h>
#include <stdio.h>
#include "Stack.h"
//typedef struct Position
//{
//  int _x;
//  int _y;
//}Position;


#define MAX_ROW 6
#define MAX_COL 6
typedef struct Maze
{
    int _map[MAX_ROW][MAX_COL];
}Maze;

//初始化迷宫
void InitMaze(Maze *m, int map[MAX_ROW][MAX_COL])
{
    int i = 0;
    int j = 0;
    assert(m);
    for (i = 0; i < MAX_ROW; i++)
    {
        for (j = 0; j < MAX_COL; j++)
        {
            m->_map[i][j] = map[i][j];
        }
    }
}

//打印迷宫
void PrintMaze(Maze *m)
{
    int i = 0;
    int j = 0;
    assert(m);
    for (i = 0; i < MAX_ROW; i++)
    {
        for (j = 0; j < MAX_COL; j++)
        {
            printf("%d  ", m->_map[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

//检测有效入口
int IsValidEntry(Maze *m, Position entry)
{
    if (entry._x == 0 || entry._x == MAX_ROW - 1
        || entry._y == 0 || entry._y == MAX_COL - 1)
    {
        if (1 == m->_map[entry._x][entry._y])
            return 1;
    }
    return 0;
}

//检测出口
int IsExit(Maze *m, Position entry, Position cur)
{
    if (cur._x == entry._x && cur._y == entry._y)
    {
        return 0;
    }
    if (cur._x == 0 || cur._x == MAX_ROW - 1
        || cur._y == 0 || cur._y == MAX_COL - 1)
    {
        return 1;
    }
    return 0;
}

//判断下一步是否能走:下一步为 1 || 大于当前步
int IsNextPass(Maze *m, Position cur, Position next)
{
    if (m->_map[next._x][next._y] == 1 || m->_map[next._x][next._y] > m->_map[cur._x][cur._y])
        return 1;
    return 0;
}

//将shortPath栈更新为最短路径、
void SaveshortPath(Stack *shortPath, Stack *path)
{
    int i = 0;
    for (; i < StackSize(path); ++i)
    {
        StackPush(shortPath, path->_array[i]);
    }
}
//走迷宫具体方式
void GetPassMaze(Maze *m, Position entry, Position cur, Stack *path, Stack *shortPath)
{
    Position next;
    if (cur._x == entry._x && cur._y == entry._y)
    {
        m->_map[cur._x][cur._y] = 2;
        StackPush(path, cur);
    }

    if (IsExit(m, entry, cur))
    {
        if (StackEmpty(shortPath) || StackSize(path) < StackSize(shortPath))
            SaveshortPath(shortPath, path);

        StackPop(path);
        return;
    }

    //向上侧走
    next = cur;
    next._x -= 1;
    if (IsNextPass(m, cur, next))
    {
        m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
        GetPassMaze(m, entry, next, path, shortPath);
    }

    //向左侧走
    next = cur;
    next._y -= 1;
    if (IsNextPass(m, cur, next))
    {
        m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
        GetPassMaze(m, entry, next, path, shortPath);
    }

    //向右侧走
    next = cur;
    next._y += 1;
    if (IsNextPass(m, cur, next))
    {
        m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
        GetPassMaze(m, entry, next, path, shortPath);
    }

    //向下方走
    next = cur;
    next._x += 1;
    if (IsNextPass(m, cur, next))
    {
        m->_map[next._x][next._y] = m->_map[cur._x][cur._y] + 1;
        GetPassMaze(m, entry, next, path, shortPath);
    }
    StackPop(path);
}
//走迷宫
void PassMaze(Maze *m, Position entry, Stack *shortPath)
{
    Stack path;
    if (!IsValidEntry(m, entry))
    {
        printf("入口无效!!!\n");
        return;
    }
    StackInit(&path);
    StackInit(shortPath);
    GetPassMaze(m, entry, entry, &path, shortPath);
}

//测试函数
void TestMaze()
{
    int map[MAX_ROW][MAX_COL] = {
        { 0, 0, 0, 0, 0, 0 },
        { 0, 0, 1, 1, 1, 0 },
        { 0, 0, 1, 0, 1, 0 },
        { 0, 0, 1, 0, 1, 0 },
        { 0, 0, 1, 1, 1, 1 },
        { 0, 0, 1, 0, 0, 0 },
    };
    Maze m;
    Stack shortPath;
    Position entry;
    InitMaze(&m, map);
    PrintMaze(&m);

    entry._x = 5;
    entry._y = 2;
    PassMaze(&m, entry, &shortPath);
    PrintMaze(&m);
}



//////////////////////////////////////////////////////////////////
#include "MazeOP.h"
int main()
{
    TestMaze();
    system("pause");
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值