⁢⁢⁢⁢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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵