随手练——C语言网-1096: Minesweeper

题目链接:http://www.dotcpp.com/oj/problem1096.html

#include <iostream>
#include <string.h>
using namespace std;
char M[105][105];

void DFS(int i, int j) {
    if (M[i][j] == '.' || M[i][j] == '*') {
        if (M[i][j] == '.') {
            int count = 0;
            for (int a = i - 1; a <= i + 1; a++) {
                for (int b = j - 1; b <= j + 1; b++) {
                    if (M[a][b] == '*')count++;
                }
            }
            M[i][j] = count + '0';
        }
        DFS(i + 1, j);
        DFS(i, j + 1);
    }
}
int main() {
    int m, n, k = 1;
    while (cin >> m >> n) {
        if (m == 0 || n == 0)break;
        cin.ignore();
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                char c = getchar();
                M[i][j] = c;
            }
            getchar();
        }
        DFS(1, 1);
        cout << "Field #" << k++ << ":" << endl;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                cout << M[i][j];
            }
            cout << endl;
        }
        cout << endl;
    }

    return 0;
}

 

上一篇:BZOJ 1096: [ZJOI2007]仓库建设 动态规划 + 斜率优化


下一篇:【数学建模】基于matlab银行多机排队论模型【含Matlab源码 1096期】