# 【題解】ZeroJudge e605: 10189 – Minesweeper

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e605 (考生答對率: 37.68%)

```#include <iostream>
#include <cstring>
using namespace std;
char a[105][105];
int b[105][105];
int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};

int main() {
int n, m, Case = 1;
string s;
while (cin >> n >> m){
if (n == 0 && m == 0) break;
for (int i = 0; i < n; i++){
cin >> s;
for (int j = 0; j < m; j++){
a[i][j] = s[j];
}
}
memset(b, 0, sizeof(b));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (a[i][j] == '*'){
b[i][j] = -1;
} else {
for (int k = 0; k < 8; k++){
int x = i + dx[k];
int y = j + dy[k];
if (x >= 0 && x < n && y >= 0 && y < m && a[x][y] == '*')
b[i][j]++;
}
}
}
}
if (Case > 1) cout << "\n";
cout << "Field #" << Case++ << ":\n";
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (b[i][j] == -1) cout << '*';
else cout << b[i][j];
}
cout << "\n";
}
}
return 0;
}
```

Python code (credit: Amy Chou)

```dx = [1, -1, 0, 0, -1, 1, -1, 1]
dy = [0, 0, 1, -1, 1, 1, -1, -1]
TC = 1
while True:
n, m = map(int, input().split())
if n == 0 and m == 0:
break
a = []
for i in range(n):
a.append(list(input()))

b = [[0 for j in range(m)] for i in range(n)]
for i in range(n):
for j in range(m):
if a[i][j] == '*':
b[i][j] = -1
else:
for k in range(8):
x = i + dx[k]
y = j + dy[k]
if x >= 0 and x < n and y >= 0 and y < m and a[x][y] == "*":
b[i][j] += 1
if TC > 1:
print()
print(f"Field #{TC}:")
TC += 1
for i in range(n):
for j in range(m):
if b[i][j] == -1:
print("*", end="")
else:
print(b[i][j], end="")
print()

```