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


    //先输入行列,在输入迷宫 以-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;
    }
  • 相关阅读:
    (转)介绍一些.net开源项目
    (转).Net有哪些大型项目、大型网站的案例?
    Linux下的awk使用
    linux下的sed使用
    linux下的find、grep、cut使用
    CentOS7下的Zabbix4.4安装配置
    NetBackup linux客户端安装
    bat小脚本
    批量复制指定目录下的文件,并统计执行时间
    iptables
  • 原文地址:https://www.cnblogs.com/ZhengLijie/p/12491363.html
Copyright © 2020-2023  润新知