python
2024-10-26 20:50:32
发布于:北京
0阅读
0回复
0点赞
def cnt_mines(fld, r, c, n, m):
dirs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
count = 0
for dr, dc in dirs:
nr, nc = r + dr, c + dc
if 0 <= nr < n and 0 <= nc < m and fld[nr][nc] == '*':
count += 1
return count
def main():
n, m = map(int, input().split())
fld = [input().strip() for _ in range(n)]
res = [[''] * m for _ in range(n)]
for i in range(n):
for j in range(m):
if fld[i][j] == '?':
res[i][j] = str(cnt_mines(fld, i, j, n, m))
else:
res[i][j] = fld[i][j]
for row in res:
print(''.join(row))
if __name__ == "__main__":
main()
这里空空如也
有帮助,赞一个