码迷,mamicode.com
首页 > 其他好文 > 详细

栈与队列应用:迷宫问题(DFS非最短路径)

时间:2020-03-14 13:05:04      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:入行   std   一个   lse   printf   回退   direct   creat   方向   

//先输入行列,在输入迷宫 以-1 -1 结束
#include<stdio.h>
#include<stdlib.h>

#define MAXSIZE 100
#define ERROR -1
#define OK 1

struct Direction
{
    int incX; //增量
    int incY;
};

Direction direct[4] = { {0,1},{1,0},{0,-1},{-1,0} };//方向试探

struct Box
{
    int x, y;  //当前访问的迷宫格子的纵横坐标
    int di;  //当前方向
};

typedef struct  //用Box声明栈来存放数据
{
    Box* base;
    Box* top;
    int stackSize;
}SqStack;

void InitStack(SqStack* s)
{
    s->base = (Box *)malloc(sizeof(Box) * MAXSIZE);
    if (s->base == NULL)
    {
        exit(0);
    }
    s->top = s->base;
    s->stackSize = MAXSIZE;
}

void Push(SqStack* s, Box e)
{
    if (s->top - s->base == s->stackSize)
    {
        exit(0);
    }
    *(s->top) = e;
    (s->top)++;
}

void Pop(SqStack* s, Box* e)
{
    if (s->top == s->base)
    {
        return;
    }
    *e = *--(s->top);
}

int StackLen(SqStack s)
{
    return (s.top - s.base);
}

int isEmptyStack(SqStack* s)  //判断栈是否为空 是返回1 不是返回-1
{
    if (s->top == s->base)  //栈为空
    {
        return 1;
    }
    else //栈不为空
    {
        return 0;
    }
}

int** CreatMaze(int M, int N) //初始化
{
    int** maze;

    maze = (int **)malloc(sizeof(int*) * M);  //
    for (int i = 0; i < N; i++)
    {
        maze[i] = (int *)malloc(sizeof(int) * N); //
    }

    for (int i = 0; i < M; i++) //将边界置为1
    {
        for (int j = 0; j < N; j++)
        {
            maze[i][j] = 1;
        }
    }

    for (int i = 1; i < M-1; i++) //输入
    {
        for (int j = 1; j < N-1; j++)
        {
            scanf("%d", &maze[i][j]);
        }
    }

    return maze;
}

bool findPath(int M, int N, Direction direct[], SqStack* s)
{
    int** maze;
    Box temp,e;
    int x, y, di;  //当前正在处理的单元行列
    int line, col; //将要处理的下一个单元行列
    int flag = 0;

    maze = CreatMaze(M + 2, N + 2); //初始化

    maze[1][1] = -1;
    temp = { 1,1,-1 };
    Push(s, temp);

    while (!isEmptyStack(s))  //栈不为空循环继续
    {   
        Pop(s, &temp);  //如果路走不通就回退
        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)
            {
                temp = { x,y,di };
                Push(s, temp);
                x = line;
                y = col;
                maze[line][col] = -1;
                if (x == M && y == N)
                {
                    return true;
                }
                else
                {
                    di = 0;
                }
            }
            else
            {
                di++;
            }
        }
    }

    return false;
}

int main(void)
{
    int M, N;
    bool tem;
    SqStack s;
    Box e;
    while (1)
    {
        InitStack(&s);
        scanf("%d %d", &M, &N);
        if (M == -1)
            break;
        tem = findPath(M, N, direct, &s);

        if (tem)
        {
            while (s.base != s.top)
            {
                printf("%d,%d\n", s.base->x, s.base->y);
                s.base++;
            }
            printf("%d,%d\n", M, N);
        }
        else
        {
            printf("NO FOUND");
        }
    }
    
    return 0;
}

 

栈与队列应用:迷宫问题(DFS非最短路径)

标签:入行   std   一个   lse   printf   回退   direct   creat   方向   

原文地址:https://www.cnblogs.com/ZhengLijie/p/12491363.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!