Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 递归函数在0x79B20AD2(ucrtbased.dll)抛出未处理的异常_C++_Visual Studio_Recursion - Fatal编程技术网

C++ 递归函数在0x79B20AD2(ucrtbased.dll)抛出未处理的异常

C++ 递归函数在0x79B20AD2(ucrtbased.dll)抛出未处理的异常,c++,visual-studio,recursion,C++,Visual Studio,Recursion,我写了《扫雷舰》,现在的任务是写一个函数,可以发现附近没有地雷的地区。在最初的扫雷艇中,如果你在没有地雷的区域内单击,它将打开一个区域,直到其边界附近有地雷。为此,我编写了函数unravel()。代码如下: #include <iostream> #include <cstdlib> #include <time.h> #include <vector> #include <string> using namespace std; #

我写了《扫雷舰》,现在的任务是写一个函数,可以发现附近没有地雷的地区。在最初的扫雷艇中,如果你在没有地雷的区域内单击,它将打开一个区域,直到其边界附近有地雷。为此,我编写了函数unravel()。代码如下:

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <string>
using namespace std;

#define Str1D vector<string>
#define Str2D vector<Str1D>
#define Int1D vector<int>
#define Int2D vector<Int1D>

void unravel(Str2D &fogofwar, Int2D &display, int x, int y) {

    for (int minusrows = -1; minusrows < 2; minusrows++){ // going through the 
                                                          // neighbouring cells (+ the cell itself)
        for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){

            if (x + minusrows > 0 && y + minuscolumns > 0 && x + minusrows < fogofwar.size() && y + minuscolumns < fogofwar[0].size()){ // checking 
                                                                                                                                        // if within borders
                if (x > 0 && y > 0 && x < fogofwar.size() && y < fogofwar[0].size()) { // checking if the oririginal 
                                                                                       // values are within borders
                    fogofwar[x + minusrows][y + minuscolumns] = to_string(display[x + minusrows][y + minuscolumns]); // revealing the 
                                                                                                                     // neighbouring cells
                    if (display[x + minusrows][y + minuscolumns] == 0) { // if the cell is 0 on the display, 
                                                                         // open it and the 8 neighbouring to it cells

                        if (not (minusrows == 0 && minuscolumns == 0)) { // if it's not the same cell, of course,
                                                                         // otherwise it's an endless cycle

                            unravel(fogofwar, display, x + minusrows, y + minuscolumns);
                        }
                    }
                }                               
            }
        }
    }
}


int main() {
    int row, column, prob;
    bool running = true;


    cout << "Input width and height: ";
    cin >> row >> column;

    cout << endl << "Input mines probability (%): ";
    cin >> prob;
    cout << endl;
    srand (time(NULL));

    Int2D field(row + 1, Int1D(column + 1));
    Int2D display(row + 1, Int1D(column + 1));
    Str2D fogofwar(row + 1, Str1D(column + 1, "*"));

    field[0][0] = 0; // field of mines
    display[0][0] = 0; // display of neighbouring mines
    fogofwar[0][0] = to_string(0); // what the player will see

    for (int i = 1; i < row + 1; i++) { //assigning coordinates
        field[i][0] = i;
        display[i][0] = i;
        fogofwar[i][0] = to_string(i);
    }
    for (int j = 1; j < column + 1; j++) { //assigning coordinates
        field[0][j] = j;
        display[0][j] = j;
        fogofwar[0][j] = to_string(j);
    }

    for (int i = 1; i < row + 1; i++){ // filling the field with mines
        for (int j = 1; j < column + 1; j++){
            int x = rand() % 100;
            if (x < prob) {
                field[i][j] = 1;
            }
            else{
                field[i][j] = 0;
            }

        }

    }

    cout << endl << endl;

    for (int i = 0; i < row + 1; i++){ // printing field
            for (int j = 0; j < column + 1; j++){
                cout << " " << field[i][j] << " ";
            }
            cout << endl;
        }

    cout << endl << endl;

    for (int i = 0; i < row + 1; i++){ // assigning the display of amount of neighbouring mines
        for (int j = 0; j < column + 1; j++){

            int count = 0;

            if (i > 0 && j > 0){

                for (int minusrows = -1; minusrows < 2; minusrows++){

                    for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){

                        if (i + minusrows > 0 && i + minusrows < row + 1 && j + minuscolumns > 0 && j + minuscolumns < column + 1){

                            if (field[i + minusrows][j + minuscolumns] == 1){

                                count++;

                            }

                        }

                    }
                }
                display[i][j] = count;
            }


            cout << " " << display[i][j] << " ";


        }
        cout << endl;
    }


    cout << endl << endl;



    while (running) {

        for (int i = 0; i < row + 1; i++){
            for (int j = 0; j < column + 1; j++){
                cout << " " << fogofwar[i][j] << " ";
            }
            cout << endl;
        }

        cout << endl;

        int x, y;
        cout << endl << "Input the target cell (x, y): ";
        cin >> x >> y;
        cout << endl;

        unravel(fogofwar, display, x, y);

    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#定义Str1D向量
#定义Str2D向量
#定义Int1D向量
#定义Int2D向量
无效解列(Str2D和fogofwar、Int2D和display、intx和inty){
对于(int-minusrows=-1;minusrows<2;minusrows++){//遍历
//相邻单元(+单元本身)
对于(int-minuscolumns=-1;minuscolumns<2;minuscolumns++){
如果(x+minusrows>0&&y+minuscolumns>0&&x+minusrows0&&y>0&&x行>>列;
cout-prob;

cout首先,我无法用所讨论的信息重现错误。请尝试指定完整的用例以及您将要出错的值。 然而,在解构的实施中存在着一个明显的问题。 你对同一个单元格进行多次检查,直到内存超过总内存(我相信这就是你的程序崩溃的原因)

您应该维护已经访问过的插槽。您可以通过多种方式来执行此操作。我提供了一种方法来处理此问题

请尝试以下代码:-

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <string>
using namespace std;

#define Str1D vector<string>
#define Str2D vector<Str1D>
#define Int1D vector<int>
#define Int2D vector<Int1D>

void unravel(Str2D &fogofwar, Int2D &display, int x, int y, vector<vector<bool> > &visited) {

    for (int minusrows = -1; minusrows < 2; minusrows++){ // going through the
                                                          // neighbouring cells (+ the cell itself)
        for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){

            if (x + minusrows > 0 && y + minuscolumns > 0 && x + minusrows < fogofwar.size() && y + minuscolumns < fogofwar[0].size()){ // checking
                                                                                                                                        // if within borders
                if (x > 0 && y > 0 && x < fogofwar.size() && y < fogofwar[0].size()) { // checking if the oririginal
                                                                                       // values are within borders
                    if (x > 0 && y > 0 && x < visited.size() && y < visited[0].size()) {
                      cout.flush();
                    }
                    visited[x][y] = true;
                    fogofwar[x + minusrows][y + minuscolumns] = to_string(display[x + minusrows][y + minuscolumns]); // revealing the
                                                                                                                     // neighbouring cells
                    if (display[x + minusrows][y + minuscolumns] == 0) { // if the cell is 0 on the display,
                                                                         // open it and the 8 neighbouring to it cells

                        if (not visited[x + minusrows][y + minuscolumns]) { // if it's not the same cell, of course,
                                                                         // otherwise it's an endless cycle
                            unravel(fogofwar, display, x + minusrows, y + minuscolumns, visited);
                        }
                    }
                }
            }
        }
    }
}


int main() {
    int row, column, prob;
    bool running = true;


    cout << "Input width and height: ";
    cin >> row >> column;

    cout << endl << "Input mines probability (%): ";
    cin >> prob;
    cout << endl;
    srand (time(NULL));

    Int2D field(row + 1, Int1D(column + 1));
    Int2D display(row + 1, Int1D(column + 1));
    Str2D fogofwar(row + 1, Str1D(column + 1, "*"));

    field[0][0] = 0; // field of mines
    display[0][0] = 0; // display of neighbouring mines
    fogofwar[0][0] = to_string(0); // what the player will see

    for (int i = 1; i < row + 1; i++) { //assigning coordinates
        field[i][0] = i;
        display[i][0] = i;
        fogofwar[i][0] = to_string(i);
    }
    for (int j = 1; j < column + 1; j++) { //assigning coordinates
        field[0][j] = j;
        display[0][j] = j;
        fogofwar[0][j] = to_string(j);
    }

    for (int i = 1; i < row + 1; i++){ // filling the field with mines
        for (int j = 1; j < column + 1; j++){
            int x = rand() % 100;
            if (x < prob) {
                field[i][j] = 1;
            }
            else{
                field[i][j] = 0;
            }

        }

    }

    cout << endl << endl;

    for (int i = 0; i < row + 1; i++){ // printing field
            for (int j = 0; j < column + 1; j++){
                cout << " " << field[i][j] << " ";
            }
            cout << endl;
        }

    cout << endl << endl;

    for (int i = 0; i < row + 1; i++){ // assigning the display of amount of neighbouring mines
        for (int j = 0; j < column + 1; j++){

            int count = 0;

            if (i > 0 && j > 0){

                for (int minusrows = -1; minusrows < 2; minusrows++){

                    for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){

                        if (i + minusrows > 0 && i + minusrows < row + 1 && j + minuscolumns > 0 && j + minuscolumns < column + 1){

                            if (field[i + minusrows][j + minuscolumns] == 1){

                                count++;

                            }

                        }

                    }
                }
                display[i][j] = count;
            }


            cout << " " << display[i][j] << " ";


        }
        cout << endl;
    }


    cout << endl << endl;



    while (running) {

        for (int i = 0; i < row + 1; i++){
            for (int j = 0; j < column + 1; j++){
                cout << " " << fogofwar[i][j] << " ";
            }
            cout << endl;
        }

        cout << endl;

        int x, y;
        cout << endl << "Input the target cell (x, y): ";
        cin >> x >> y;
        cout << endl;

        vector<vector<bool> > visited(row+1, vector<bool>(column+1, false));
        unravel(fogofwar, display, x, y, visited);

    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#定义Str1D向量
#定义Str2D向量
#定义Int1D向量
#定义Int2D向量
空洞分解(Str2D和fogofwar、Int2D和display、intx、inty、vector和visitored){
对于(int-minusrows=-1;minusrows<2;minusrows++){//遍历
//相邻单元(+单元本身)
对于(int-minuscolumns=-1;minuscolumns<2;minuscolumns++){
如果(x+minusrows>0&&y+minuscolumns>0&&x+minusrows0&&y>0&&x0&&y>0&&x行>>列;
cout-prob;

CUT在调试器中运行。捕获和检查异常。注意,当你递归太深时,堆栈被耗尽,代码失败,很难说如果没有.BTW,情况会是这样:如果你硬编码了你的地图内容来作为例子,这将是有帮助的。作为一个新的用户,也可以读取和读取。此外,考虑提交C。一旦codereview.stackexchange.com正常运行,请访问ode。您正在检查同一单元格上是否没有递归,但我看不到任何东西可以阻止您从单元格A递归到单元格B,然后再返回单元格A。可能只需要在更改当前单元格的情况下递归,检查