【python】大家来扫雷

题目链接:https://www.nowcoder.com/practice/2104cdc2aa464befac72868421066fcb

AC 65%

不知道哪里有问题,估计还是出现了python递归超过最大深度的错误…… 不知道应该怎么解决……

欢迎大家留言帮我解答!!

n,m = list(map(int,input().split()))
x,y = list(map(int,input().split()))
mat = []
for i in range(n):
    mat.append(list(input()))
res = mat.copy()
def find(mat,n,m,x,y,flag,res):
    if x < 0 or y < 0 or x >= n or y >= m or flag[x][y] == 1:
        return
    if mat[x][y] == '*':
        return 'GG'
    num = 0
    if mat[x][y] == '.':
        if x > 0 and mat[x-1][y] == '*':
            num += 1
        if x < n-1 and mat[x+1][y] == '*':
            num += 1
        if y > 0 and mat[x][y-1] == '*':
            num += 1
        if y < m-1 and mat[x][y+1] == '*':
            num += 1
        if x > 0 and y > 0 and mat[x-1][y-1] == '*':
            num += 1
        if x < n-1 and y < m-1 and mat[x+1][y+1] == '*':
            num += 1
        if y > 0 and x < n-1 and mat[x+1][y-1] == '*':
            num += 1
        if y < m-1 and x > 0 and mat[x-1][y+1] == '*':
            num += 1
        res[x][y] = str(num)
        flag[x][y] = 1
    if num == 0:
        find(mat,n,m,x+1,y,flag,res)
        find(mat,n,m,x-1,y,flag,res)
        find(mat,n,m,x,y+1,flag,res)
        find(mat,n,m,x,y-1,flag,res)
        find(mat,n,m,x+1,y+1,flag,res)
        find(mat,n,m,x-1,y-1,flag,res)
        find(mat,n,m,x-1,y+1,flag,res)
        find(mat,n,m,x+1,y-1,flag,res)
flag = [[0]*m for i in range(n)]
k = find(mat,n,m,x-1,y-1,flag,res)
if k:
    print(k)
else:
    for i in res:
        print(''.join(i))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值