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

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 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题