UVA 10189:扫雷

时间:2011-05-31 16:28:53

标签: c++ minesweeper

以下是问题的链接: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 个答案:

答案 0 :(得分:2)

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

除非我有误解,否则当有3个时,这不算只计算1个?

答案 1 :(得分:2)

没有尝试在两次运行之间清除arr[][](或在开始时清除),因此在第四位置使用*的4x4雷区将导致下一个3x3雷区的值不正确。

答案 2 :(得分:2)

您应该将arr清除为全部'。'处理每个“字段”之前的字符。否则,您的边界检查将包含错误数据。

for (int x=0; x < 102; ++x)
  for (int y=0; y < 102; ++y)
    arr[x][y] = '.';

答案 3 :(得分:0)

你必须在循环结束之前用点重新填充数组

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;

替换流缓冲区