我正在编写一个基本上用Python解决数独的代码 [英] I'm writing a code basically that solves sudoku in Python

查看:86
本文介绍了我正在编写一个基本上用Python解决数独的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试编写一个代码来解决数独。此方法主要涉及在输入列表中查找空元素,然后通过删除行,列或框中存在的值来填充可在该框中使用的可能值的空元素。然后我尝试使用回溯方法来解决使用列表中存在的每个值的问题。



此外我认为删除功能不能正常工作,因为如果它工作正常那么值8应该不在列表中的那里[1] [2]。



I have been trying to write a code to solve sudoku. This method basically involves finding the empty elements in the list of input, then filling the empty elements with the possible values that can be used in that box by deleting the values present in row, column or a box. Then I have tried using the method of backtracking to solve using each and every value present in the list.

Moreover I believe that the remove function is not working as it should have because if it had worked correctly then the value 8 should not have been there in the list in a[1][2].

def print_grid(arr):
	for i in range(9):
		for j in range(9):
			print arr[i][j],
		print ('\n')

def emptyLoc(a):
	l=[0,0]
	for i in range(9):
		for j in range(9):
			if(a[i][j]==0):
				a[i][j] = [1,2,3,4,5,6,7,8,9]
				l[0] = i
				l[1] = j
	return l

def remove(a):
    for i in range(9):
		for j in range(9):
		        if type(a[i][j]) == list :
               	       		x = a[i][j]
                		for k in x:
                    			for l in range(9):
                        			if(k == a[i][l]):
                            				x.remove(k)

    for i in range(9):
		for j in range(9):
                        if type(a[i][j]) == list:
                        	x = a[i][j]
                		for k in x:
                    			for l in range(9):
                        			if(k == a[l][j]):
                            				x.remove(k)

    for i in range(9):
		for j in range(9):
                	if type(a[i][j]) == list:
                		x = a[i][j]
                		for k in x:
                    			for l in range(3):
                        			for m in range(3):
                            				if(k == a[l+i-(i%3)][m+j-(j%3)]):
                            					x.remove(k)

    return a

def solver(arr):
    newArr = emptyLoc(arr)
    finalArr = remove(arr)

    #Changing One element lists to that single element
    for i in range(9):
		for j in range(9):
                	if type(arr[i][j]) == list and len(arr[i][j]) == 1:
                        	arr[i][j] = arr[i][j][0]

    for i in range(9):
		for j in range(9):
                	if not(type(arr[i][j]) == list):
                		return True
            		else :
                		for num in arr[i][j]:
                    			store = arr[i][j]
                    			arr[i][j] = num
                    			if (solver(arr) == True):
                        			return True
                    			else :
                        			arr[i][j] = store
                		return False




if __name__ == "__main__":
    grid=[[3,0,6,5,0,8,4,0,0],
		[5,2,0,0,0,0,0,0,0],
		[0,8,7,0,0,0,0,3,1],
		[0,0,3,0,1,0,0,8,0],
		[9,0,0,8,6,3,0,0,5],
		[0,5,0,0,9,0,6,0,0],
		[1,3,0,0,0,0,2,5,0],
		[0,0,0,0,0,0,0,7,4],
		[0,0,5,2,0,6,3,0,0]]


    if(solver(grid)):
		print_grid(grid)
    else:
		print "No solution exists"





我尝试了什么:



我不明白我的代码基本上出错了,因为我收到了一个没有回溯的答案。这是预期的输出



3 1 6 5 7 8 4 9 2

5 2 9 1 3 4 7 6 8
4 8 7 6 2 9 5 3 1

2 6 3 4 1 5 9 8 7

9 7 4 8 6 3 1 2 5

8 5 1 7 9 2 6 4 3

1 3 8 9 4 7 2 5 6

6 9 2 3 5 1 8 7 4

7 4 5 2 8 6 3 1



但我将此作为输出



3 [1,4,7,9] 6 5 [2,4,7] 8 4 [2,6,9] [2,6,7,9]

5 2 [1,4,8,9] [1,3,4,6,7,9] [3,4,7] [1,4,7,9] [4,7,8,9] [4,6,8,9] [6,7,8,9]

[4,8] 8 7 [4,6,9] [2,4,8] [2, 4,8,9 [5,8,9] 3 1

[2,4,6,7] [4,6,7] 3 [4,7] 1 [2,4 ,5,7] [4,7,9] 8 [2,6,7,9]

9 [1,4,6,7] [1,2,4,7] 8 6 3 [1,4,7,9] [1,2,4,9] 5

[2,4,6,7,8] 5 [1,2,4,7, 8] [3,4,7] 9 [2,4,7] 6 [1,2,4,8] [2,3,7]

1 3 [2,4,7] ,8,9 [4,7,9] [4,7,9] 2 5 [6,8,9]

[2,6,8] [3,6,9] [2,8,9] [1, 3,9] [3,5,8] [1,5,8,9] [1,5,8,9] 7 4

[4,6,7,8] [4 ,6,7,9] 5 2 [3,4,7,8] 6 3 [1,6,8,9] [6,8,9]



有人请帮帮我。谢谢!!!



What I have tried:

I dont understand where I am basically going wrong with the code as I recieve an un-backtracked answer. This is the expected output

3 1 6 5 7 8 4 9 2
5 2 9 1 3 4 7 6 8
4 8 7 6 2 9 5 3 1
2 6 3 4 1 5 9 8 7
9 7 4 8 6 3 1 2 5
8 5 1 7 9 2 6 4 3
1 3 8 9 4 7 2 5 6
6 9 2 3 5 1 8 7 4
7 4 5 2 8 6 3 1 9

but I am getting this as an output

3 [1, 4, 7, 9] 6 5 [2, 4, 7] 8 4 [2, 6, 9] [2, 6, 7, 9]
5 2 [1, 4, 8, 9] [1, 3, 4, 6, 7, 9] [3, 4, 7] [1, 4, 7, 9] [4, 7, 8, 9] [4, 6, 8, 9] [6, 7, 8, 9]
[4, 8] 8 7 [4, 6, 9] [2, 4, 8] [2, 4, 8, 9] [5, 8, 9] 3 1
[2, 4, 6, 7] [4, 6, 7] 3 [4, 7] 1 [2, 4, 5, 7] [4, 7, 9] 8 [2, 6, 7, 9]
9 [1, 4, 6, 7] [1, 2, 4, 7] 8 6 3 [1, 4, 7, 9] [1, 2, 4, 9] 5
[2, 4, 6, 7, 8] 5 [1, 2, 4, 7, 8] [3, 4, 7] 9 [2, 4, 7] 6 [1, 2, 4, 8] [2, 3, 7]
1 3 [2, 4, 7, 8, 9] [4, 7, 9] [4, 7, 8] [4, 7, 9] 2 5 [6, 8, 9]
[2, 6, 8] [3, 6, 9] [2, 8, 9] [1, 3, 9] [3, 5, 8] [1, 5, 8, 9] [1, 5, 8, 9] 7 4
[4, 6, 7, 8] [4, 6, 7, 9] 5 2 [3, 4, 7, 8] 6 3 [1, 6, 8, 9] [6, 8, 9]

Somebody please help me with this. Thank You !!!

推荐答案

你的代码乱七八糟,用Python,缩进不仅仅是为了帮助读取与其他语言一样的代码,缩进是结构的你的程序。

你不能有大致的缩进。

只看输出告诉你哪些问题

Your code is a mess, with Python, indentation is not just to help reading the code like with other languages, indentation is the structure of your program.
You can't have approximate indentation.
Just looking at the output tells you where are some problems
3 [1, 4, 7, 9] 6 5 [2, 4, 7] 8 4 [2, 6, 9] [2, 6, 7, 9]



在第一行,4在单元格6中定义,但仍然可能在单元格1和4中


in first line, 4 is defined in cell 6 but is still a possibility in cell 1 and 4

[4, 8] 8 7 [4, 6, 9] [2, 4, 8] [2, 4, 8, 9] [5, 8, 9] 3 1



在第三行, 8在单元格1中定义但在单元格0,4,5和6中仍然有可能

这告诉您 remove 无效行。



至于求解器,我不明白逻辑。



您的代码没有按照您的预期行事,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么,你的任务是与之比较它应该做什么。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是通过向你展示什么来帮助你正在进行。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]



调试器只是向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。


in third line, 8 is defined in cell 1 but is still a possibility in cell 0, 4, 5 and 6
This tells you that remove is not working in rows.

as for the solver, I don't understand the logic.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于我正在编写一个基本上用Python解决数独的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆