题目链接: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; }