题解 | #扫雷#

扫雷

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

def get_mine(matrix, raw, col):
    result = [['*'] * col for _ in range(raw)]
    eight_direct = [(0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)]

    def cal_mine(i_n, j_n):
        count = 0
        for direct in eight_direct:
            i_new = i_n + direct[0]
            j_new = j_n + direct[1]
            if 0 <= i_new <= raw - 1 and 0 <= j_new <= col - 1 and matrix[i_new][j_new] == '*':
                count += 1

        return count

    for i in range(raw):
        for j in range(col):
            if matrix[i][j] != '*':
                result[i][j] = str(cal_mine(i, j))

    return result

def print_result(matrix):
    for raw in matrix:
        print(''.join(raw))


raw, col = map(int, input().split())
mine_matrix = []
for i in range(raw):
    line = input().strip()
    mine_matrix.append(line)
# print(mine_matrix)
result_matrix = get_mine(mine_matrix, raw, col)
print_result(result_matrix)

全部评论

相关推荐

GoldenPota...:能做这个方向的人200一天是吧🤗
找AI工作可以去哪些公司...
点赞 评论 收藏
分享
UtopianYou...:这个简历排版真的不太行哦,去找免费的或者花点小钱,把排版弄整齐一点吧,看着舒服。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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