Python shell 由于递归深度而重新启动

标签 python shell recursion sudoku

我尝试使用 python 中的递归来制作自己的广度优先数独求解程序。目前,代码一直运行到我用作示例的简单数独的第 5 行。显然我试图扩大递归深度,但现在内核或 shell 没有崩溃,而是以更大的 sys.setrecursion 值重新启动。也许我的代码中有一个错误,或者Python根本不允许这种类型的递归。任何帮助将不胜感激。谢谢!

P.S.:我意识到我可以更有效地完成事情,有些事情是不必要的,但我只是想解决递归问题,然后我会把事情弄平。再次:谢谢!

这是代码:

import numpy as np
import copy
import sys

sys.setrecursionlimit(10000)

task = np.array([[7, 9, 0, 0, 0, 0, 3, 0, 0],
                  [0, 0, 0, 0, 0, 6, 9, 0, 0], 
                  [8, 0, 0, 0, 3, 0, 0, 7, 6],
                  [0, 0, 0, 0, 0, 5, 0, 0, 2], 
                  [0, 0, 5, 4, 1, 8, 7, 0, 0], 
                  [4, 0, 0, 7, 0, 0, 0, 0, 0], 
                  [6, 1, 0, 0, 9, 0, 0, 0, 8], 
                  [0, 0, 2, 3, 0, 0, 0, 0, 0],
                  [0, 0, 9, 0, 0, 0, 0, 5, 4]])

task_sol = copy.deepcopy(task)

def algorithm2(i=None, j=None, number=None):
    if i == 6:
        return task_sol
    if i is None and j is None:
        i, j = 0, 0
    if task[i, j] != 0:
        if j == 8:
            j = 0
            return algorithm2(i+1, j)
        else:
            return algorithm2(i, j+1)
    if number is None:
        if task_sol[i, j] == 0:
            number = 1
        else:
            number = task_sol[i, j]+1
    if i > 8:
        return task_sol
    if number > 9:
        if j == 0:
            j = 8
            amount = 1
            while task[i-amount, j] != 0:
                amount += 1
            temp_number = task_sol[i-amount, j]
            task_sol[i-amount, j] = 0
            return algorithm2(i-amount, j, temp_number+1)
        else:
            amount = 1
            while task[i, j-amount] != 0:
                amount += 1
            temp_number = task_sol[i, j-amount]
            task_sol[i, j-amount] = 0
            return algorithm2(i, j-amount, temp_number+1)
    if number_check(i, j, number) is False:
        return algorithm2(i, j, number+1)
    else:
        task_sol[i, j] = number
        if j == 8:
            j = 0
            return algorithm2(i+1, j)
        else:
            return algorithm2(i, j+1)
        
def number_check(i, j, number):
    if number in task_sol[i:i+1, :]:
        return False
    if number in task_sol[:, j:j+1]:
        return False
    new_i, new_j = i//3, j//3
    if number in task_sol[new_i*3:(new_i+1)*3, new_j*3:(new_j+1)*3]:
        return False
    return True

print(algorithm2())

最佳答案

您可以使用sys模块:

sys.setrecursionlimit(limit)

https://docs.python.org/3/library/sys.html#sys.setrecursionlimit

注意:

"This should be done with care, because a too-high limit can lead to a crash."

关于Python shell 由于递归深度而重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49619930/

相关文章:

Python优先队列检查项目是否存在而不循环

linux - 如何转义变量以用于 sed?

c++ - 为什么在递归中使用循环会产生意想不到的结果?

python - 获取链接到 "QPushButton.clicked.connect"信号的函数

python - 如何构建一个内存中的虚拟文件系统,然后将这个结构写入磁盘

Python、Selenium + 陈旧元素引用

linux - 两个命令或命令管道命令 - Spawn Expect

python - Bash 脚本传输到 Python - 将日志从服务器复制到另一个远程服务器

javascript - 递归 xml 解析函数未按预期工作

c++ - 在递归中修改调用指针时出现问题