焌烬 2022-06-06 20:39 采纳率: 66.7%
浏览 118
已结题

【问题描述】下图给出了一个迷宫的平面图,其中标记为黑色的为障碍,标记为白色的为可以通行的区域。

【问题描述】下图给出了一个迷宫的平面图,其中标记为黑色的为障碍,标记为白色的为可以通行的区域。迷宫的入口为左上角的黄色方格,出口为右下角的黄色方格。在迷宫中,只能从一个方格走到相邻的上、下、左、右四个方向之一。

img


找到一条从起点到终点的迷宫路径,并将路径输出。如果从起点到终点没有路径,则输出NO PASS!注:所有迷宫的起点为左上角,终点为右下角。【输入形式】依次输入n行由0和1构成的字符串,每行字符串长度相同,输入空串结束,其中1表示围墙,0表示可行路径。
【输出形式】如果起点到终点有路,则依次输出由L、R、D、U组成的路径字符串;否则输出NO PASS!。
【样例输入】
0111111
0011101
1001101
0011001
1000111
1110000

  • 写回答

2条回答 默认 最新

  • ...404 Not Found 2022-06-06 20:46
    关注
    
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<assert.h>
    #include<stdbool.h>
     
    //创建一个点的数据类型
    typedef struct Coordinate
    {
        int row;
        int col;
    }Coor;
    /
    typedef Coor STD;
     
    typedef struct Stack
    {
        STD* base;
        int top;
        int capacity;
    }S;
     
    void StackInit(S* ps)
    {
        assert(ps);
     
        ps->base = (STD*)malloc(sizeof(STD) * 4);
        if (ps->base == NULL)
        {
            printf("malloc fail\n");
            exit(-1);
        }
     
        ps->capacity = 4;
        ps->top = 0;
    }
     
    void StackDestory(S* ps)
    {
        assert(ps);
        free(ps->base);
        ps->base = NULL;
        ps->top = ps->capacity = 0;
    }
     
    // 入栈
    void StackPush(S* ps, STD x)
    {
        assert(ps);
     
        // 满了-》增容
        if (ps->top == ps->capacity)
        {
            STD* tmp = (STD*)realloc(ps->base, ps->capacity * 2 * sizeof(STD));
            if (tmp == NULL)
            {
                printf("realloc fail\n");
                exit(-1);
            }
            else
            {
                ps->base = tmp;
                ps->capacity *= 2;
            }
        }
     
        ps->base[ps->top] = x;
        ps->top++;
    }
     
    // 出栈
    void StackPop(S* ps)
    {
        assert(ps);
        // 栈空了,调用Pop,直接中止程序报错
        assert(ps->top > 0);
     
        //ps->a[ps->top - 1] = 0;
        ps->top--;
    }
     
    STD StackTop(S* ps)
    {
        assert(ps);
        // 栈空了,调用Top,直接中止程序报错
        assert(ps->top > 0);
     
        return ps->base[ps->top - 1];
    }
     
    int StackSize(S* ps)
    {
        assert(ps);
     
        return ps->top;
    }
     
    bool StackEmpty(S* ps)
    {
        assert(ps);
     
        return ps->top == 0;
    }
    /
    //栈的文件
    S Path;
    bool checkCoor(int**maze, int N, int M, Coor coor)
    {
        if ((coor.row >= 0 && coor.col < N)
            && (coor.row >= 0 && coor.col < M)
            && maze[coor.row][coor.col] == 0)
            return true;
        else
            return false;
    }
    bool SearchmazePath(int**maze, int N, int M, Coor coor)
    {
        //点入栈
        StackPush(&Path, coor);
        if (coor.row == N - 1 && coor.col == M - 1)//找到目标位置返回真
            return true;
     
        Coor next;
        maze[coor.row][coor.col] = 2;//将走过的点标记为2
     
        //分别对上、下、左、右四个方向进行判断,递归调用。
        next = coor;
        next.col += 1;
        if (checkCoor(maze, N, M, next))
        {
            if (SearchmazePath(maze, N, M, next))
                return true;
        }
     
        next = coor;
        next.col -= 1;
        if (checkCoor(maze, N, M, next))
        {
            if (SearchmazePath(maze, N, M, next))
                return true;
        }
        next = coor;
        next.row -= 1;
        if (checkCoor(maze, N, M, next))
        {
            if (SearchmazePath(maze, N, M, next))
                return true;
        }
        next = coor;
        next.row += 1;
        if (checkCoor(maze, N, M, next))
        {
            if (SearchmazePath(maze, N, M, next))
                return true;
        }
        //为到达目标点,弹出数据,递归回退。
        StackPop(&Path);
        return false;
    }
    void PrintPath(S*path)
    {
        S rpath;//在创建一个栈
        StackInit(&rpath);
        while (!StackEmpty(path))//将原栈的数据存入逆序栈中
        {
            StackPush(&rpath, StackTop(path));
            StackPop(path);
        }
        while (!StackEmpty(&rpath))
        {
            Coor c = StackTop(&rpath);
            printf("(%d,%d)\n", c.row, c.col);//进行打印
            StackPop(&rpath);
        }
        StackDestory(&rpath);
    }
    int main()
    {
        int N, M;
        while (scanf("%d%d", &N, &M)!=EOF)
        {
            int**maze = (int**)malloc(sizeof(int*)*N);
            for (int i = 0; i < N; i++)
                maze[i] = (int*)malloc(sizeof(int)*M);
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < M; j++)
                    scanf("%d", &maze[i][j]);
            }
            StackInit(&Path);
            Coor start = { 0,0 };
            if (SearchmazePath(maze, N, M, start))
            {
                //printf("找到出口了,路径如下:\n");
                PrintPath(&Path);
            }
            else
                printf("NO PASS!\n");*/
            StackDestory(&Path);
            for (int i = 0; i < N; i++)
                free(maze[i]);
            free(maze);
            maze = NULL;
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月14日
  • 已采纳回答 6月6日
  • 创建了问题 6月6日

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看