C语言数据结构学习笔记(5)-链栈迷宫问题DFS显示

# include <stdio.h>
# include <stdlib.h>
# include <windows.h>
# define bool int
# define true 1
# define false 0
# define M 18
# define N 18
# define SIZE 4

//方向试探
typedef struct Direction{
    int incX, incY;//x, y方向的增量即行和列
}Direction;
//栈中数据元素的组织
typedef struct Box{
    int x, y;//当前访问迷宫格子的纵横坐标
    int di;//当前方向
}Box;

typedef Box ElemType;
typedef struct Node{
    ElemType data;
    struct Node * next;
}Node, *pNode;

typedef struct Stack{
    pNode top;
    int count;
}Stack, *pStack;

void initStack(pStack pS);//初始化链栈
bool isEmpty(pStack pS);//判断链栈是否为空
void pushStack(pStack pS, ElemType data);//压栈,保存迷宫格子数据
ElemType popStack(pStack pS);//出栈,弹出迷宫格子数据
void destoryStack(pStack pS);//摧毁链栈,释放内存空间
void traverseStack(pStack pS);//遍历链栈
bool findPath(int (*maze)[N+2], Direction * direct, pStack pS);//找寻迷宫路径
void printmaze(int (*maze)[N+2]);//打印迷宫路径

int main(void)
{
    int maze[M+2][N+2] = {
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1},
        {1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1},
        {1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1},
        {1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1},
        {1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1},
        {1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1},
        {1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1},
        {1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
        {1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1},
        {1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1},
        {1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1},
        {1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1},
        {1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1},
        {1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1},
        {1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1},
        {1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1},
        {1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1},
        {1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 2, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
    };
    Direction direct[SIZE] = {
        {0, 1},{1, 0},{0, -1},{-1, 0}
    };//定义四个方向
    Stack S;
    initStack(&S);
    if(findPath(maze, direct, &S))
    {    
        printf("迷宫有路:\n");
        traverseStack(&S);
        destoryStack(&S);
    }
    else
    {
        printf("迷宫没路.\n");
        destoryStack(&S);
    }
    system("pause");
    return 0;
}
void printmaze(int (*maze)[N+2])
{

    for(int i = 0; i < M+2; i++)
    {
        for(int j = 0; j < N+2; j++)
        {    
            if(maze[i][j] == 0)
                printf("  ");
            else if(maze[i][j] == 1)
                printf("■");
            else if(maze[i][j] == -1)
                printf("◎");
            else if(maze[i][j] == -2)
                printf("□");
            else if(maze[i][j] == -3)
                printf("  ");
            else
                printf("♀");
        }
        printf("\n");
    }
    Sleep(100);
    system("cls");
}
bool findPath(int (*maze)[N+2], Direction * direct, pStack pS)
{
    Box temp;
    int x, y, di;
    int line, col;
    maze[1][1] = -1;
    temp.x = 1;
    temp.y = 1;
    temp.di = -1;
    pushStack(pS, temp);
    while(!isEmpty(pS))
    {
        temp = popStack(pS);
        x = temp.x;
        y = temp.y;
        di = temp.di + 1;
        while(di < 4)
        {
            line = x + direct[di].incX;
            col = y + direct[di].incY;
            if(maze[line][col] == 0 || maze[line][col] == 2)
            {
                temp.x = x;
                temp.y = y;
                temp.di = di;
                pushStack(pS, temp);
                x = line;
                y = col;
                maze[line][col] = -1;
                printmaze(maze);
                if(x == M && y == N)
                {    
                    temp.x = x;
                    temp.y = y;
                    temp.di = di;
                    pushStack(pS, temp);
                    return true;
                }
                else
                    di = 0;
            }
            else
                di++;
        }
        //maze[x][y] = -2;
        maze[x][y] = -3;
        printmaze(maze);
    }
    return false;
}

void initStack(pStack pS)
{
    pS->top = NULL;
    pS->count = 0;
}

bool isEmpty(pStack pS)
{
    if(pS->count == 0)
        return true;
    else
        return false;
}

void pushStack(pStack pS, ElemType elem)
{
    pNode pNew = (pNode)malloc(sizeof(Node));
    if(pNew == NULL)
    {
        printf("动态内存分配失败.\n");
        exit(-1);
    }
    pNew->data = elem;
    pNew->next = pS->top;
    pS->top = pNew;
    pS->count++;
}

ElemType popStack(pStack pS)
{
    if(pS->count == 0)
        printf("栈空,无法出栈.\n");
    pNode p = pS->top;
    ElemType elem = p->data;
    pS->top = p->next;
    free(p);
    p = NULL;
    pS->count--;
    return elem;
}

void destoryStack(pStack pS)
{    
    pNode p = pS->top;
    while(pS->top != NULL)
    {
        pS->top = p->next;
        free(p);
        p = pS->top;
    }
}

void traverseStack(pStack pS)
{    
    Box * temp = (Box*)malloc(pS->count*sizeof(Box));
    if(temp == NULL)
    {
        printf("动态内存分配失败.\n");
        exit(-1);
    }
    int i = 0;
    pNode p = pS->top;
    while(p != NULL)
    {    
        temp[i].x = p->data.x;
        temp[i].y = p->data.y;
        temp[i].di = p->data.di;
        p = p->next;
        i++;
    }
    for(i = pS->count-1; i >= 0 ; i--)
        printf("(%d %d %d)\n", temp[i].x, temp[i].y, temp[i].di);
    free(temp);
    temp = NULL;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值