美文网首页算法
用栈实现迷宫求解(改进版,C语言)

用栈实现迷宫求解(改进版,C语言)

作者: beed0c3eb989 | 来源:发表于2017-10-12 13:27 被阅读0次
    #include <stdio.h>
    #include <stdlib.h>
    #define MaxRow 10
    #define MaxCol 10
    #define MAXSTACKSIZE 100
    #define STACKINCREMENT 10
    #define NOENOUGHROOM 10
    #define STACKOVERFLOW 11
    #define up 1
    #define down 2
    #define left 3
    #define right 4
    
    typedef struct  {
        int *abscissa;
        int *ordinate;
        int *direction;
        int maxstacksize;
    }SqStack;
    
    SqStack Stack;
    int top = -1; //初始化栈顶
    
    //依次上下左右行走
    int arrowx[4] = {0, 0, -1, 1};
    int arrowy[4] = {-1, 1, 0, 0};
    
    int Maze[MaxRow][MaxCol] = {}; //用来标记走过的路
    int pathMaze[MaxRow][MaxCol] = {};  //用来存储正确路径
    
    int maxabs = 1, maxord = 1; //用户输入的迷宫大小
    
    void InitStack() {
        int *absbase, *ordbase, *direbase;
        absbase = (int *)malloc(MAXSTACKSIZE*sizeof(int));
        ordbase = (int *)malloc(MAXSTACKSIZE*sizeof(int));
        direbase = (int *)malloc(MAXSTACKSIZE*sizeof(int));
        if(!absbase || !ordbase || !direbase)
            exit(NOENOUGHROOM);
        Stack.abscissa = absbase;
        Stack.ordinate = ordbase;
        Stack.direction = direbase;
        Stack.maxstacksize = MAXSTACKSIZE;
    }
    
    void push(int abscissa, int ordinate, int direction) {
        ++top;
        
        //判断是否溢出
        if(top >= Stack.maxstacksize){
            int *newabsbase, *newordbase, *newdirebase;
            newabsbase = (int *)realloc(Stack.abscissa, (MAXSTACKSIZE + STACKINCREMENT)*sizeof(int));
            newordbase = (int *)realloc(Stack.ordinate, (MAXSTACKSIZE + STACKINCREMENT)*sizeof(int));
            newdirebase = (int *)realloc(Stack.direction, (MAXSTACKSIZE +STACKINCREMENT)*sizeof(int));
            if(!newordbase || !newabsbase || !newdirebase)
                exit(NOENOUGHROOM);
            Stack.abscissa = newabsbase;
            Stack.ordinate = newordbase;
            Stack.direction = newdirebase;
        }
            
        *(Stack.abscissa + top) = abscissa;
        *(Stack.ordinate + top) = ordinate;
        *(Stack.direction + top -1) = direction;
    }
    
    void pop() {
        //判断是否溢出
        if(top == -2)
            exit(STACKOVERFLOW);
        *(Stack.abscissa + top) = 0;
        *(Stack.ordinate + top) = 0;
        *(Stack.direction + top) = 0;
        --top;
    }
    
    //打印点和方向
    void printPointAndDirection(int n) {
        switch (*(Stack.direction + n)) {
            case up:
                printf("(%d, %d, up)\n", *(Stack.abscissa + n), *(Stack.ordinate + n));
                break;
            case down:
                printf("(%d, %d, down)\n", *(Stack.abscissa + n), *(Stack.ordinate + n));
                break;
            case left:
                printf("(%d, %d, left)\n", *(Stack.abscissa + n), *(Stack.ordinate + n));
                break;
            case right:
                printf("(%d, %d, right)\n", *(Stack.abscissa + n), *(Stack.ordinate + n));
                break;
        }
    }
    
    //打印迷宫解
    void printPath() {
        printf("输出路线:\n");
        int i = 0;
        while(1) {
            printPointAndDirection(i);
            if(*(Stack.abscissa + i) == maxabs && *(Stack.ordinate + i) == maxord)
                break;
            ++i;
        }
    }
    
    //标记走过的路
    void markMaze(int abscissa, int ordinate) {
        Maze[ordinate][abscissa] = 2;
        pathMaze[ordinate][abscissa] = 2;
    }
    
    //打印迷宫并标出正确出路
    void printPathMaze() {
        printf("\n迷宫输出是:\n");
        for(int i = 1;i <= maxord;i++) {
            for(int j = 1;j <= maxabs;j++) {
                printf("%d ", pathMaze[i][j]);
            }
            printf("\n");
        }
        printf("\n\n");
    }
    
    //打印用来做标记的迷宫
    void printMaze() {
        printf("\n迷宫输出是:\n");
        for(int i = 1;i <= maxord;i++) {
            for(int j = 1;j <= maxabs;j++) {
                printf("%d ", Maze[i][j]);
            }
            printf("\n");
        }
        printf("\n\n");
    }
    
    //初始化迷宫
    void InitMaze() {
        printf("\n请输入迷宫的长和宽:\n");
        scanf("%d %d", &maxabs, &maxord);
        printf("请输入迷宫:\n");
        for(int i = 1;i <= maxord;i++) {
            for(int j = 1;j <= maxabs;j++) {
                scanf("%d", &Maze[i][j]);
                pathMaze[i][j] = Maze[i][j];
            }
        }
    }
    
    //寻找出路
    void runMaze() {
        //判断第一个方向
        push(1, 1 , 0);
        markMaze(1, 1);
        int flag = 0;
        while(1) {
            int xNow, yNow;
            int i = 0;
            xNow = *(Stack.abscissa + top);
            yNow = *(Stack.ordinate + top);
            if(xNow == maxabs && yNow == maxord) {
                flag = 1;
                break;
            }else if(top == -2)
                break;
            for(i = 0;i < 4;i++) {
                int xNext, yNext;
                xNext = xNow + arrowx[i];
                yNext = yNow + arrowy[i];
                //找到可走的点
                if(xNext > 0 && xNext <= maxabs && yNext > 0 && yNext <= maxord && Maze[yNext][xNext] == 0) {
                    push(xNext, yNext, i+1);
                    markMaze(xNext, yNext);
                    break;
                }
            }
            //无路可走
            if(i == 4) {
                pathMaze[yNow][xNow] = 0;
                pop();
            }
        }
        
        if(flag) {
            printPath();
            printPathMaze();
        } else {
            printf("\n没有出路,无解!\n");
        }
    }
    
    int main() {
        InitMaze();
        runMaze();
    }
    
    

    相关文章

      网友评论

        本文标题:用栈实现迷宫求解(改进版,C语言)

        本文链接:https://www.haomeiwen.com/subject/jynnyxtx.html