题解 | 扫雷

扫雷

https://www.nowcoder.com/practice/d5f277427d9a4cd3ae60ea6c276dddfd

n, m = map(int, input().split())
matrix = [list(input().strip()) for _ in range(n)]

directions = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]

result = []
for i in range(n):
    row = []
    for j in range(m):
        if matrix[i][j] == '*':
            row.append('*')
        else:
            cnt = sum(1 for dx, dy in directions 
                     if 0 <= i+dx < n and 0 <= j+dy < m 
                     and matrix[i+dx][j+dy] == '*')
            row.append(str(cnt))
    result.append(''.join(row))

print('\n'.join(result))

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务