制作我自己的数独谜题

时间:2016-02-01 22:56:01

标签: python algorithm sudoku

我正在尝试用我自己制作的算法从头开始编写一个基本的数独求解器,其中我的程序解决了一些空元素但不是全部。无论它解决了什么元素,我只是复制那个数独板替换旧板并再次运行我的程序,它应该解决其他空元素。但是,我对我的程序的问题是元素没有被替换或解决,我的程序不打印整个板。它只打印最后一行。我认为这与我如何用rone,cone和bone读取行,列和块有关,但我不确定我在这里做错了什么。

这是我制作的算法,我正在关注。 here

董事会档案。

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

计划

a1 = []
a2 = []
a3 = []
a4 = []
a5 = []
a6 = []
a7 = []
a8 = []
a9 = []
with open('Boards') as f:
    for line in f:
        data = line.split()
        a1.append(int(data[0]))
        a2.append(int(data[1]))
        a3.append(int(data[2]))
        a4.append(int(data[3]))
        a5.append(int(data[4]))
        a6.append(int(data[5]))
        a7.append(int(data[6]))
        a8.append(int(data[7]))
        a9.append(int(data[8]))





    print("Old Board")



    print(a1[0], a2[0], a3[0],"|", a4[0], a5[0], a6[0],"|", a7[0], a8[0], a9[0])
    print(a1[1], a2[1], a3[1],"|", a4[1], a5[1], a6[1],"|", a7[1], a8[1], a9[1])
    print(a1[2], a2[2], a3[2],"|", a4[2], a5[2], a6[2],"|", a7[2], a8[2], a9[2])
    print("---------------------")
    print(a1[3], a2[3], a3[3],"|", a4[3], a5[3], a6[3],"|", a7[3], a8[3], a9[3])
    print(a1[4], a2[4], a3[4],"|", a4[4], a5[4], a6[4],"|", a7[4], a8[4], a9[4])
    print(a1[5], a2[5], a3[5],"|", a4[5], a5[5], a6[5],"|", a7[5], a8[5], a9[5])
    print("---------------------")
    print(a1[6], a2[6], a3[6],"|", a4[6], a5[6], a6[6],"|", a7[6], a8[6], a9[6])
    print(a1[7], a2[7], a3[7],"|", a4[7], a5[7], a6[7],"|", a7[7], a8[7], a9[7])
    print(a1[8], a2[8], a3[8],"|", a4[8], a5[8], a6[8],"|", a7[8], a8[8], a9[8])





    #Rows
    r1 = [a1[0], a2[0], a3[0], a4[0], a5[0], a6[0], a7[0], a8[0], a9[0]]
    r2 = [a1[1], a2[1], a3[1], a4[1], a5[1], a6[1], a7[1], a8[1], a9[1]]
    r3 = [a1[2], a2[2], a3[2], a4[2], a5[2], a6[2], a7[2], a8[2], a9[2]]
    r4 = [a1[3], a2[3], a3[3], a4[3], a5[3], a6[3], a7[3], a8[3], a9[3]]
    r5 = [a1[4], a2[4], a3[4], a4[4], a5[4], a6[4], a7[4], a8[4], a9[4]]
    r6 = [a1[5], a2[5], a3[5], a4[5], a5[5], a6[5], a7[5], a8[5], a9[5]]
    r7 = [a1[6], a2[6], a3[6], a4[6], a5[6], a6[6], a7[6], a8[6], a9[6]]
    r8 = [a1[7], a2[7], a3[7], a4[7], a5[7], a6[7], a7[7], a8[7], a9[7]]
    r9 = [a1[8], a2[8], a3[8], a4[8], a5[8], a6[8], a7[8], a8[8], a9[8]]
    #Cols
    c1 = [a1[0], a1[1], a1[2], a1[3], a1[4], a1[5], a1[6], a1[7], a1[8]]
    c2 = [a2[0], a2[1], a2[2], a2[3], a2[4], a2[5], a2[6], a2[7], a2[8]]
    c3 = [a3[0], a3[1], a3[2], a3[3], a3[4], a3[5], a3[6], a3[7], a3[8]]
    c4 = [a4[0], a4[1], a4[2], a4[3], a4[4], a4[5], a4[6], a4[7], a4[8]]
    c5 = [a5[0], a5[1], a5[2], a5[3], a5[4], a5[5], a5[6], a5[7], a5[8]]
    c6 = [a6[0], a6[1], a6[2], a6[3], a6[4], a6[5], a6[6], a6[7], a6[8]]
    c7 = [a7[0], a7[1], a7[2], a7[3], a7[4], a7[5], a7[6], a7[7], a7[8]]
    c8 = [a8[0], a8[1], a8[2], a8[3], a8[4], a8[5], a8[6], a8[7], a8[8]]
    c9 = [a9[0], a9[1], a9[2], a9[3], a9[4], a9[5], a9[6], a9[7], a9[8]]
    #3x3 boxes from Left to right
    b1 = [a1[0], a2[0], a3[0], a1[1], a2[1], a3[1], a1[2], a2[2], a3[2]]
    b2 = [a4[0], a5[0], a6[0], a4[1], a5[1], a6[1], a4[2], a5[2], a6[2]]
    b3 = [a7[0], a8[0], a9[0], a7[1], a8[1], a9[1], a7[2], a8[2], a9[2]]
    b4 = [a1[3], a2[3], a3[3], a1[4], a2[4], a3[4], a1[5], a2[5], a3[5]]
    b5 = [a4[3], a5[3], a6[3], a4[4], a5[4], a6[4], a4[5], a5[5], a6[5]]
    b6 = [a7[3], a8[3], a9[3], a7[4], a8[4], a9[4], a7[5], a8[5], a9[5]]
    b7 = [a1[6], a2[6], a3[6], a1[7], a2[7], a3[7], a1[8], a2[8], a3[8]]
    b8 = [a4[6], a5[6], a6[6], a4[7], a5[7], a6[7], a4[8], a5[8], a6[8]]
    b9 = [a7[6], a8[6], a9[6], a7[7], a8[7], a9[7], a7[8], a8[8], a9[8]]
    print("\n")

#Start with rows 1-9
count = 1
for rone in r1, r2, r3, r4, r5, r6, r7, r8, r9: #Checks the rows initially 1-9
    #Check elements 1-9 for a 0
    for element in rone:
        if element == 0:
            #Count represents the number that is being looked for
            while count !=9:
                #Reset Option (Option=0)
                Option = 0
                #Check Block
                for bone in b1, b2, b3, b4, b5, b6, b7, b8, b9: #Check block for number
                    for element in bone:
                        #If there is an element 1-9 when checking 3x3 block that the selected element is in break out of loop and
                            #increment count to next number to check for
                        if element == count: #supposed element in block already

                            count += 1 #++count

                            break
                        else:
                                Option += 1
                                #Check Row
                                for rone in r1, r2, r3, r4, r5, r6, r7, r8, r9: #Check Row for number
                                    for element in rone:
                                        #If there is an element 1-9 when checking row that the selected element is in break out of loop and
                                        #increment count to next number to check for
                                        if element == count: #supposed elemnet in row already
                                            count += 1

                                            break #or Pass or break
                                        else:
                                            Option += 1
                                            for cone in c1, c2, c3, c4, c5, c6, c7, c8, c9: #Check column for number
                                                for element in cone:
                                                    #If there is an element 1-9 when columnthat the selected element is in break out of loop and
                                                    #increment count to next number to check for
                                                    if element == count: #supposed elemnet in column already
                                                        count += 1
                                                        break
                                                    else:
                                                        #The number is not in the row, column, or block so theres an option
                                                        Option += 1
                                                        break
                        if Option == 1:
                            #Place number in elemnent
                            #Replace element with count
                            count = element

                            count = 0
                        else:
                            #Else more than one option, loop to count and ++count
                            count += 1
                            break

                    count += 1


                    break
                continue
print("New Board", rone)

2 个答案:

答案 0 :(得分:1)

首先,我认为在每次迭代后用新的替换旧电路板太“昂贵”,而且一般来说编程实践也不好。

下面,我为您提供了一个基本算法,您可以使用它来解决9乘9的数独板。做你想做的事。'

Disp 81 emtpy square grids;
Fill grids with some known numbers;

while(there are empty square){
    if(top left square is empty){
        create a number x;
        if(no other numbers x are in row/column/mini grid){
            plug in x;
        }
    } else{
        while(row number < 9){
            go to next row;
            if(row number == 9){
                row  number = 1;
                next column;
            }
        }
    }
}

我意识到我为你提供的算法可能有点基础,但这是一个开始。

答案 1 :(得分:0)

它只打印一行,因为在完成整个外循环后,您明确告诉它只打印最后一行。如果您缩进语句以将其置于 rone 循环中,您将看到按照您对其进行处理的顺序打印的行:

Old Board
(1, 4, 0, '|', 0, 8, 0, '|', 0, 3, 9)
(0, 3, 0, '|', 0, 0, 0, '|', 0, 1, 2)
(0, 0, 0, '|', 1, 0, 0, '|', 0, 0, 4)
---------------------
(6, 7, 9, '|', 2, 5, 8, '|', 1, 4, 3)
(4, 0, 0, '|', 0, 9, 1, '|', 0, 0, 0)
(2, 0, 1, '|', 0, 0, 4, '|', 0, 6, 5)
---------------------
(9, 0, 0, '|', 0, 1, 3, '|', 0, 0, 0)
(5, 1, 0, '|', 0, 0, 0, '|', 0, 9, 0)
(3, 6, 0, '|', 0, 4, 0, '|', 0, 8, 0)


('New Board', [3, 6, 0, 0, 4, 0, 0, 8, 0])
('New Board', [0, 3, 0, 0, 0, 0, 0, 1, 2])
('New Board', [0, 0, 0, 1, 0, 0, 0, 0, 4])
('New Board', [6, 7, 9, 2, 5, 8, 1, 4, 3])
('New Board', [4, 0, 0, 0, 9, 1, 0, 0, 0])
('New Board', [2, 0, 1, 0, 0, 4, 0, 6, 5])
('New Board', [9, 0, 0, 0, 1, 3, 0, 0, 0])
('New Board', [5, 1, 0, 0, 0, 0, 0, 9, 0])
('New Board', [3, 6, 0, 0, 4, 0, 0, 8, 0])

这解决了你的直接问题。但是,在这之后你会遇到更多问题。最明显的是,在你进入下一个代码之前,你没有得到代码的一部分 - 你似乎已经将它编码在一个大块中,这使得调试变得更加困难。首先,您可以在循环中更改 rone 循环变量:您在 else 子句下有一个嵌套循环,可以改变 rone 。第102行。这严重扰乱了最外层循环的循环流程。

接下来,继续遍历原始板:b1-b9,r1-r9和c1-c9。据我所知,当您解决拼图时,您根本不会更新这些内容。

总的来说,您似乎已经通过更大的项目超越了您的编程能力。在列表方面,您还没有思考,并且您没有在基本数据结构方面精神上组织任务。我强烈建议您首先处理一些较小的项目,然后在您更多地运用这些基本技能时再回到这个项目。不要扔掉它 - 这是一次很棒的学习经历。我保留了我的一流课程,在学习一门新语言时我仍然会回到他们身边。事实上,我将它们分配给我自己的学生,并经常向他们展示我犯的错误(给他们时间找出自己的错误)。

如果您正在寻找攻击问题,可以从Project Euler中的问题开始。它是一个可爱的算法难题集合,通常会变得更难,每周都会发布一个新问题。你将构建一些软件工具......其中一个问题是一个很好的数独求解器。