⁢⁢⁢⁢418 2022-05-11 23:30 采纳率: 100%
浏览 48
已结题

迷宫问题代码报错无法解决

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容

img

img


#include<stdio.h>
#include<stdbool.h>
struct route {
    int x;
    int y;
};
typedef struct route route;
bool judge2(int** maze,int n, int m, route next) {  //判断路径是否正确
    if (next.x >= 0 && next.x < n 
        && next.y >= 0 && next.y < m 
        && maze[next.x][next.y] == 0) {
        return true;
    }
    else {
        return false;
    }
}
bool Mazesolving(int** maze, int n, int m, route cur) { //迷宫求解
    if (cur.x == n-1 && cur.y == m-1) {   //找到出口
        return true;
    }
    //将经过的路径做标记
    route next;
    maze[cur.x][cur.y] = 2;
    //探索四个方向
    next = cur;
    next.y +=1;  //右
    if (judge2(maze,n, m, next)) {
        if (Mazesolving(maze, n, m, next)) {
            return true;
        }
    }
    next = cur;
    next.x +=1;  //下
    if (judge2(maze, n, m, next)) {
        if (Mazesolving(maze, n, m, next)) {
            return true;
        }
    }
    next = cur;
    next.y -=1;   //左
    if (judge2(maze, n, m, next)) {
        if (Mazesolving(maze, n, m, next)) {
            return true;
        }
    }
    next = cur;
    next.x -=1;    //上
    if (judge2(maze, n, m, next)) {
        if (Mazesolving(maze, n, m, next)) {
            return true;
        }
    }
    //当为死路时
    return false;
}
int main() {
    int arr[4][4] = { {0,0,1,0},   //右下左上
                      {1,0,1,0},
                      {1,0,0,0}};
    route cur={0,0};
    if (Mazesolving(arr, 4, 4, cur)) {
        printf("该迷宫有出口\n");
    }
    else {
        printf("无出口\n");
    }
    return 0;
}
  • 写回答

2条回答 默认 最新

  • 丨秋水丨 2022-05-11 23:40
    关注

    应该是二维数组传参有问题,把judge2和Mazesolving两个方法的maze参数的类型改成int (*maze)[4]就可以了,
    这个4就是arr[4][4]里的第一个维大小,如果是arr[X][4],那这个4就要改成X(X是个常量)

    img

    #include<stdio.h>
    #include<stdbool.h>
    struct route {
        int x;
        int y;
    };
    typedef struct route route;
    bool judge2(int(*maze)[4], int n, int m, route next) {  //判断路径是否正确
        if (next.x >= 0 && next.x < n
            && next.y >= 0 && next.y < m
            && maze[next.x][next.y] == 0) {
            return true;
        }
        else {
            return false;
        }
    }
    bool Mazesolving(int (*maze)[4], int n, int m, route cur) { //迷宫求解
        if (cur.x == n - 1 && cur.y == m - 1) {   //找到出口
            return true;
        }
        //将经过的路径做标记
        route next;
        maze[cur.x][cur.y] = 2;
        //探索四个方向
        next = cur;
        next.y += 1;  //右
        if (judge2(maze, n, m, next)) {
            if (Mazesolving(maze, n, m, next)) {
                return true;
            }
        }
        next = cur;
        next.x += 1;  //下
        if (judge2(maze, n, m, next)) {
            if (Mazesolving(maze, n, m, next)) {
                return true;
            }
        }
        next = cur;
        next.y -= 1;   //左
        if (judge2(maze, n, m, next)) {
            if (Mazesolving(maze, n, m, next)) {
                return true;
            }
        }
        next = cur;
        next.x -= 1;    //上
        if (judge2(maze, n, m, next)) {
            if (Mazesolving(maze, n, m, next)) {
                return true;
            }
        }
        //当为死路时
        return false;
    }
    int main() {
        int arr[4][4] = { {0,0,1,0},   //右下左上
                          {1,0,1,0},
                          {1,0,0,0} };
        route cur = { 0,0 };
        if (Mazesolving(arr, 4, 4, cur)) {
            printf("该迷宫有出口\n");
        }
        else {
            printf("无出口\n");
        }
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月20日
  • 已采纳回答 5月12日
  • 修改了问题 5月11日
  • 创建了问题 5月11日

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示