纬度打击 2021-02-16 22:23 采纳率: 88.9%
浏览 1334
已采纳

C++的:exit code -1073741819 (0xC0000005) 怎么解决?

#include <iostream>
#include <queue>

#define maxn 35
using namespace std;

int n;
int arr[maxn][maxn];
bool vis[maxn][maxn];

struct pr {
    int h, l;

    pr() {}

    pr(int a, int b) {
        l = a;
        h = b;
    }
} a[1000000];

void bfs() {
    int k = 0;
    queue<pr> que;
    pr st;
    for (int i = 2; i <= n - 1; i++) {
        for (int j = 2; j <= n - 1; j++) {
            if (arr[i][j] == 0 && !vis[i][j]) {
                st.l = i;
                st.h = j;
            }
        }
    }
    que.push(st);
    while (!que.empty()) {
        st = que.front();
        que.pop();
        vis[st.l][st.h] = true;
        if (arr[st.l][st.h] == 0) {
            a[k++] = st;
            if (!vis[st.l - 1][st.h]) {
                que.push(pr(st.l - 1, st.h));
            }
            if (!vis[st.l + 1][st.h]) {
                que.push(pr(st.l + 1, st.h));
            }
            if (!vis[st.l][st.h - 1]) {
                que.push(pr(st.l, st.h - 1));
            }
            if (!vis[st.l][st.h + 1]) {
                que.push(pr(st.l, st.h + 1));
            }
        } else if (arr[st.l][st.h] == 3) {
            k = 0;
            while (!que.empty()) {
                que.pop();
            }
            for (int i = 2; i <= n - 1; i++) {
                for (int j = 2; j <= n - 1; j++) {
                    if (arr[i][j] == 0 && !vis[i][j]) {
                        st.l = i;
                        st.h = j;
                    }
                }
            }
            que.push(st);
        }
    }
    for (int i = 0; i < k; i++) {
        arr[a[i].l][a[i].h] = 2;
    }
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> arr[i][j];
        }
    }
    for (int i = 0; i <= n + 1; i++) {
        arr[0][i] = arr[n + 1][i] = arr[i][0] = arr[i][n + 1] = 3;
    }
    bfs();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}
  • 写回答

1条回答 默认 最新

  • 幻灰龙 2021-02-16 22:40
    关注

    下面这段代码for循环的条件不满足时,for循环并没有执行,st并没有被初始化,于是你push进去后后面的代码取出来使用时,st.h和st.l是随机数据,超出vist的容量时数组越界,程序崩溃。

    pr st;
    for (int i = 2; i <= n - 1; i++) {
        for (int j = 2; j <= n - 1; j++) {
            if (arr[i][j] == 0 && !vis[i][j]) {
                st.l = i;
                st.h = j;
            }
        }
    }
    que.push(st);

    这种问题,问题不在于代码错在哪里,而在于需要把代码切割成更小的小函数,针对每个小函数单独进行单元测试,每个函数经过单元测试都功能正确后再组装起来完成更复杂的功能。这样层层编程才能实现可测试、可调试、可快速诊断问题在哪里的好的程序。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。