P2670 [NOIP2015 普及组] 扫雷游戏,随便写写

m, n = map(int, input().split())
a = []
for i in range(m):
    row = list(input())
    a.append(row)

for i in range(m):
    for j in range(n):
        if a[i][j] == '?':
           a[i][j] = 0
        elif a[i][j] == '*':
            a[i][j] = 1

for i in range(m):
    for j in range(n):
        if a[i][j] == 1:
            print('*', end='')
        else:
            count = 0
            if i > 0 and j > 0 and a[i-1][j-1] == 1:
                count += 1
            if i > 0 and a[i-1][j] == 1:
                count += 1
            if i > 0 and j < n-1 and a[i-1][j+1] == 1:
                count += 1
            if j > 0 and a[i][j-1] == 1:
                count += 1
            if j < n-1 and a[i][j+1] == 1:
                count += 1
            if i < m-1 and j > 0 and a[i+1][j-1] == 1:
                count += 1
            if i < m-1 and a[i+1][j] == 1:
                count += 1
            if i < m-1 and j < n-1 and a[i+1][j+1] == 1:
                count += 1
            print(count, end='')
    print('')

想了蛮久,最后还是用了笨办法来暴力做,直接用计数器原理把8个位置进行计数。这题我遇到的最大的问题在于索引边界的设立,就当发出来纪念一下。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值