算法
(暴力枚举)
C++ 代码
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
const int di[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dj[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int main() {
int h, w;
cin >> h >> w;
vector<string> s(h);
rep(i, h) cin >> s[i];
vector<string> ans(h, string(w, '#'));
rep(i, h)rep(j, w) if (s[i][j] == '.') {
int now = 0;
rep(v, 8) {
int ni = i+di[v], nj = j+dj[v];
if (ni < 0 or ni >= h or nj < 0 or nj >= w) continue;
if (s[ni][nj] == '#') now++;
}
ans[i][j] = now+'0';
}
rep(i, h) cout << ans[i] << '\n';
return 0;
}