UVA 10189: тральщик - PullRequest
       20

UVA 10189: тральщик

0 голосов
/ 31 мая 2011

Вот ссылка на проблему: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1130

Это мой код, и он отлично работает;однако, он дает неправильный ответ всякий раз, когда я отправляюКто-нибудь знает, почему?

ПРИМЕЧАНИЕ. Я дополняю матрицу двумя дополнительными строками и столбцами, чтобы при проверке левого или первого столбца первого столбца я не получалошибка.

//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 ]

2 голосов
/ 31 мая 2011

Не делается попытка очистить arr[][] между запусками (или очистить при запуске), поэтому минное поле 4x4 с * в 4-м положении вызовет неправильное значение следующего минного поля 3x3.

2 голосов
/ 31 мая 2011

Вы должны очистить arr для всех '.'символы перед каждым «полем» обрабатывается.В противном случае ваши проверки границ будут содержать неверные данные.

for (int x=0; x < 102; ++x)
  for (int y=0; y < 102; ++y)
    arr[x][y] = '.';
2 голосов
/ 31 мая 2011
if(arr[i-1][j-1] == '*' || arr[i-1][j] == '*' || arr[i-1][j+1] == '*')
    count++;

Если я не понимаю, разве это не считается только 1 моим, когда может быть 3?

0 голосов
/ 09 ноября 2017

Вы должны заполнить массив точками прямо перед тем, как закончится цикл

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;

...