题解 | 扫雷

扫雷

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

n, m = map(int,input().split())
metrix = [list(input()) for _ in range(n)]
#数据输入及初始化
for i in range(n):
    for j in range(m):
        if metrix[i][j] != '*':
            cot = 0
            if i-1>=0 and j-1>=0 and metrix[i-1][j-1]=='*':
                cot += 1#左上
            if i-1>=0  and metrix[i-1][j]=='*':
                cot += 1#上
            if i-1>=0 and j+1<m and metrix[i-1][j+1]=='*':
                cot += 1#右上
            if j-1>=0 and metrix[i][j-1]=='*':
                cot += 1#左
            if j+1<m and metrix[i][j+1]=='*':
                cot += 1#右
            if i+1<n and j-1>=0 and metrix[i+1][j-1]=='*':
                cot += 1#左下
            if i+1<n and metrix[i+1][j]=='*':
                cot += 1#下
            if i+1<n and j+1<m and metrix[i+1][j+1]=='*':
                cot += 1#右下
            metrix[i][j] = str(cot)
#遍历二维矩阵,判断每一个非地雷区域周边的地雷数量
print('\n'.join(map(''.join,metrix)))
#数据格式化输出

全部评论

相关推荐

码农顶针:估计让你免费辅导老板孩子的学习
点赞 评论 收藏
分享
08-21 16:35
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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