gpt4 book ai didi

python - 我需要在此数独 python 代码中进行解释

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:33:03 26 4
gpt4 key购买 nike

<分区>

我尝试了这个用 Python 编写的数独解算器 https://freepythontips.wordpress.com/2013/09/01/sudoku-solver-in-python/ 代码运行良好。这是完整的代码:

import sys

def same_row(i,j): return (i/9 == j/9)
def same_col(i,j): return (i-j) % 9 == 0
def same_block(i,j): return (i/27 == j/27 and i%9/3 == j%9/3)

def r(a):
i = a.find('0')
if i == -1:
sys.exit(a)

excluded_numbers = set()
for j in range(81):
if same_row(i,j) or same_col(i,j) or same_block(i,j):
excluded_numbers.add(a[j])

for m in '123456789':
if m not in excluded_numbers:
r(a[:i]+m+a[i+1:])

r('100070030830600000002900608600004907090000050307500004203009100000002043040080009')

我不明白这个循环:

for m in '123456789':
if m not in excluded_numbers:
r(a[:i]+m+a[i+1:])

请有人解释这段代码的算法。谢谢

编辑:我问的是那个循环的逻辑,现在我明白了逻辑,我检查了 Peter Novig 的数独解算器。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com