-1

問題へのリンクは次のとおりです。http://uva.onlinejudge.org/index.php?option = com_onlinejudge&Itemid = 8&category = 13&page = show_problem& problem = 1130

これは私のコードであり、完全に機能します。しかし、私がそれを提出するときはいつでもそれは間違った答えを与えます。誰かが理由を知っていますか?

注:最初の列の左側または最後の行の下部をチェックするときにエラーが発生しないように、マトリックスに2つの追加の行と列を埋め込みます。

//A minesweeper generator
#include <iostream>
#include <sstream>

using namespace std;

char arr[102][102]; //2D dynamic array used temporarily

int main() {
    int n, m; //Rows and columns
    int count = 0, recordNum = 0; //Number of mines around the current dot

    while(true) { //Keep processing records until "0 0" is encountered
        cin >> n >> m;

        if(n == 0 && m == 0 ) //End of input
            break;

        //Read the values into the array
        for(int i = 1; i < n+1; i++) { //Rows
            for(int j = 1; j < m+1; j++) { //Columns
                cin >> arr[i][j];
            }
        }

        //Process the values of the array and generate the numbers
        for(int i = 1; i < n+1; i++) { //Rows
            for(int j = 1; j < m+1; j++) { //Columns
                if(arr[i][j] == '*')
                    continue;
                else { //Count the number of mines around this dot
                    if(arr[i-1][j-1] == '*')
                                        count++;
                                    if(arr[i-1][j] == '*')
                                        count++;
                                    if(arr[i-1][j+1] == '*')
                        count++;
                    if(arr[i][j-1] == '*')
                                        count++;
                                    if(arr[i][j+1] == '*')
                                        count++;
                                    if(arr[i+1][j-1] == '*')
                        count++;
                    if(arr[i+1][j] == '*')
                                        count++;
                                    if(arr[i+1][j+1] == '*')
                        count++;
                }

                //Create a buffer to convert the count to a char
                stringstream buffer;
                buffer << count;
                arr[i][j] = buffer.str().at(0);

                count = 0; //Finally reset the counter
            }
        }

        if(recordNum > 0)
            cout << endl;
        recordNum++;
        cout << "Field #" << recordNum << ":\n";

        //Output the values
        for(int i = 1; i < n+1; i++) { //Rows
            for(int j = 1; j < m+1; j++) { //Columns
                cout << arr[i][j];
            }
            cout << endl;
        }
    }
    return 0;
}
4

4 に答える 4

2
if(arr[i-1][j-1] == '*' || arr[i-1][j] == '*' || arr[i-1][j+1] == '*')
    count++;

私が誤解していない限り、それは3つある可能性があるときに1つの鉱山だけを数えませんか?

于 2011-05-31T16:32:01.297 に答える
2

実行の合間にクリアする(または開始時にクリアする)試みは行われないarr[][]ため、4番目の位置に*が付いた4x4の地雷原は、次の3x3の地雷原の値が正しくなくなります。

于 2011-05-31T16:48:00.113 に答える
2

arrすべての「。」をクリアする必要があります。各「フィールド」が処理される前の文字。そうしないと、境界チェックに不良データが含まれます。

for (int x=0; x < 102; ++x)
  for (int y=0; y < 102; ++y)
    arr[x][y] = '.';
于 2011-05-31T16:48:33.873 に答える
0

whileループが終了する直前に、配列にドットを再入力する必要があります

for (int i = 1; i < n + 1; i++) { //Rows
    for (int j = 1; j < m + 1; j++) { //Columns
        arr[i][j] = '.';
    }
}

を使用してストリームバッファを置き換えることができますarr[i][j] = count + 48;

于 2017-11-09T16:26:54.983 に答える