lijiachen 发表于 2020-8-30 15:09:29

数独求解器问题

看到论坛里有人做了数独求解器,本来也想做一个,但是出了一点问题:(求分析或思路,不用帮我改代码)

# sudoku here
# 原题
line1 =
line2 =
line3 =
line4 =
line5 =
line6 =
line7 =
line8 =
line9 =

# solution here

def which_block(where):# 获取这个数所在的宫有哪些数
      line = where + 1
      exec(f'row = {where + 1}', globals())

      if line < 4 and row < 4:
                return set(line1[:4] + line2[:4] + line3[:4])
      elif 7 > line > 3 and row < 4:
                return set(line4[:4] + line5[:4] + line6[:4])
      elif line > 6 and row < 4:
                return set(line7[:4] + line8[:4] + line9[:4])

      if line < 4 and 3 < row < 7:
                return set(line1[:4] + line2[:4] + line3[:4])
      elif 7 > line > 3 and 3 < row < 7:
                return set(line4[:4] + line5[:4] + line6[:4])
      elif line > 6 and 3 < row < 7:
                return set(line7[:4] + line8[:4] + line9[:4])

      if line < 4 and 6 < row:
                return set(line1[:4] + line2[:4] + line3[:4])
      elif 7 > line > 3 and 6 < row:
                return set(line4[:4] + line5[:4] + line6[:4])
      elif line > 6 and 6 < row:
                return set(line7[:4] + line8[:4] + line9[:4])

def solve_one(where):# 求解一个数
      exec(f'number = line{where + 1}[{where}]', globals()) # 求这个数是什么
      exec(f'line = line{where + 1}', globals())
      exec(f'row = {where}', globals())

      row_numbers = []
      for i in range(9):
                exec(f'row_numbers.append(line{i + 1})')# 一列都有什么数

      numbers = set(range(1, 10))

      if number == 0:# 只有这个数未知才判断
                numbers = numbers - set(line)# 一行去除相同数
                numbers = numbers - set(row_numbers)# 一列去除相同数
                numbers = numbers - which_block(where)# 一宫去除相同数

                if len(numbers) == 1:# 如果有唯一数就返回它
                        exec(f'line{where + 1}[{where}] = list(numbers)')# 更新数独
                        return list(numbers)

                else:
                        return# 求不出就退出等待下次

      else:
                return

temp = 0

while 0 in line1 + line2 + line3 + line4 + line5 + line6 + line7 + line8 + line9: # 如果还有零就继续循环
      for ii in range(9):
                for jj in range(9):
                        result = solve_one((ii, jj))

      print(line1, line2, line3, line4, line5, line6, line7, line8, line9, '\n', sep='\n')
      temp += 1# 控制循环次数

      if temp >= 100:
                break

# 100 steps here
# while循环一百次结果










# question
# 执行到20次左右时结果就不会再变了
# 手工做了一下, 发现答案不符
# 速度每次循环(1 step)耗时0.0149秒

谢谢!{:10_243:}

永恒的蓝色梦想 发表于 2020-8-30 15:09:30

lijiachen 发表于 2020-8-30 16:16
高深……
https://fishc.com.cn/forum.php?mod=viewthread&tid=82257&highlight=%BB%D8%CB%DD
看回复… ...

https://fishc.com.cn/forum.php?mod=viewthread&tid=74816

lijiachen 发表于 2020-8-30 15:47:46

本帖最后由 lijiachen 于 2020-8-30 15:48 编辑

啊!没人!!!{:10_269:}
@Twilight6
求大神解答{:10_254:}

疾风怪盗 发表于 2020-8-30 16:11:45

这个太高深了,论坛里都是初学者,完全没思路,只有请版主来解惑了
这个帖子起码设个100++鱼币,4个太少了

lijiachen 发表于 2020-8-30 16:16:10

疾风怪盗 发表于 2020-8-30 16:11
这个太高深了,论坛里都是初学者,完全没思路,只有请版主来解惑了
这个帖子起码设个100++鱼币,4个太少了

高深……
https://fishc.com.cn/forum.php?mod=viewthread&tid=82257&highlight=%BB%D8%CB%DD
看回复……

疾风怪盗 发表于 2020-8-30 16:20:43

lijiachen 发表于 2020-8-30 16:16
高深……
https://fishc.com.cn/forum.php?mod=viewthread&tid=82257&highlight=%BB%D8%CB%DD
看回复… ...

{:5_99:}看了一脸懵_.......
求解答

lijiachen 发表于 2020-8-30 16:24:41

疾风怪盗 发表于 2020-8-30 16:20
看了一脸懵_.......
求解答

呃,我也发了求助帖,等别人解答吧……

lijiachen 发表于 2020-8-30 21:06:02

永恒的蓝色梦想 发表于 2020-8-30 17:53
https://fishc.com.cn/forum.php?mod=viewthread&tid=74816

哦~
页: [1]
查看完整版本: 数独求解器问题