题解 | #扫雷#

扫雷

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

def generate_minesweeper_matrix(n, m, matrix):
    # Directions array for 8 neighboring cells
    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:
                mine_count = 0
                for di, dj in directions:
                    ni, nj = i + di, j + dj
                    if 0 <= ni < n and 0 <= nj < m and matrix[ni][nj] == "*":
                        mine_count += 1
                row.append(str(mine_count))
        result.append("".join(row))

    return result


# Input reading
n, m = map(int, input().split())
matrix = []
for _ in range(n):
    matrix.append(input().strip())

# Generate minesweeper matrix
minesweeper_matrix = generate_minesweeper_matrix(n, m, matrix)

# Output the result
for row in minesweeper_matrix:
    print(row)

#python#
全部评论

相关推荐

Kunnnnnnn:看这公司23年就成立了啊 还没倒闭呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务