最少的python代码
2024-08-25 18:51:43
发布于:广东
23阅读
0回复
0点赞
思路:
遍历每个位置的情况,当遇到?时,看它的周围有没有地雷,有的话count+=1,然后把count值放到另一个矩阵res中。
n,m = map(int,input().split())
s = [list(input()) for _ in range(n)]
res = [['*'] *m for _ in range(n)]
direction = [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]
count = 0
for i in range(n):
for j in range(m):
if s[i][j]=='?':
count = 0
for dx,dy in direction:
x = i + dx
y = j + dy
if 0<=x<n and 0<=y<m and s[x][y]=="*":
count+=1
res[i][j] = str(count)
for i in res:
print("".join(i))
这里空空如也
有帮助,赞一个