解决3D数独 [英] Solving a 3D sudoku

查看:139
本文介绍了解决3D数独的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



我尝试做一个程序,它解决了一个3d数独(9x9x9,9行soduko表,相互排在后面),第一件事是做第一个数独表,然后第二个,依此类推。我做了它,然后下一个任务是旋转它,或垂直或水平翻转,直到我得到一个有效的3d数独,所以行和列中的数字不包含2个相同的数字。所以soduko的规则。



任何想法都可以吗?



目前我所拥有的: br />
- 解决了9个数独桌子

- 它可以旋转桌子

- 它可以翻转它



我尝试了什么:

-我为了解决所有可能的解决方案做了很多。(你知道,这是一个非常非常愚蠢的想法,但首先我想要它看起来工作得很好)我大约一个小时前开始它,它仍然有效,因为我计算它有3000亿可能的解决方案,所以是的,它需要几个小时才能完成。



我的问题是,我怎么能这样做?你有什么想法?



谢谢你的答案。

Hello!

I try to do a program which solves a 3d sudoku (9x9x9, 9 soduko table in row, behind each other) and the first thing is doing the first sudoku table, then the second and so on. I did it, and then the next task is rotate it, or flip it in vertical, or in horizontal until i get a valid 3d sudoku, so the numbers in the rows and the columns don't contain 2 same number. So the rules of soduko.

Any idea to do it?

What i have currently:
-Solves the 9 sudoku table
-It can rotate the table
-It can flip it

What I tried:
-I made a lot "for" to go through all possible solutions.(ye, i know, thats a very very stupid idea, but first I wanted it to see working perfectly) I started it about an hour ago, and it still works, as I calculated it has 300 billion possible solutions, so yes it will take a few hours to go through.

My question is, how could I do this? What's your idea?

Thanks for the answers.

推荐答案

我想你只需要三倍-subscripted数组。



I think you would just need a triple-subscripted array.

int[,,] solver = new int[9,9,9];





前进,然后编码。



Go forth, and code.


I dont know, if I understand the question, but i started programming generator of 3D sudoku 2x2x2. It has numbers 1-8 in it. Its big cube 8x8x8, it has LINES and COLUMNS, each of them has numbers 1-8. There are also VERTICAL LINES from bottom up. And SMALL CUBES 2x2x2, which has numbers 1-8 too. (2D sudoku have only columns, lines and small squares)

This puzzle has got 512 numbers.

Program is made in python 3.

My program generate array with 512 numbers. Those are the numbers in the puzzle. You can count their indexes as (x + y*8 + z*64).

The program starts with array in array: sudokulist = [[3,4,5,6,8,1,2,7]]
sudokulist is array, with all sudokus. The program tries to add all posibilities of numbers to the sudoku and create more sudokus in sudokulist. It will add only correct sudokus.

Example how it could generate 2D sudoku 1x2:¨

[[]]
[[1][2]]
[[1,2][2]]
[[1,2,2][2]]
[[1,2,2,1][2]]

This example program generated one of 2 possible solutions of sudoku 1x2 (the sudoku with 2 wasnt used, couse the program ended really fast, but the real one shuffle sometimes the sudokus):

12       21
21  AND  12

The REAL program sometimes delete some sudokus, so there wont be too many. This is why it always when i use it, it can generate only about 310 long sudoku. (once it hits 448 it will always find one correct solution with 512 numbers, couse the last 64 numbers have only one solution (or I hope so, it works with all 2D sudokus, where the last column i generated always had one good posibility))

IF YOU HAVE ANY SUGGESTIONS HOW TO GENERATE IT SOMEHOW, YOU CAN HELP ME OR SORRY FOR INTERRUPTIONG YOUR FORUM. NOONE EVER GENERATED ANY 3D SUDOKU I KNOW ABOUT, SO I MAY BE THE FIRST. :D


#PROGRAM SUDOKU 2X2X2 --- GENERATING

from copy import deepcopy
from random import shuffle

#set first line to random numbers 1-8, becouse these numbers arent important
def start():
    sudoku = []
    for i in range(1, 9):
        sudoku.append(i)
    shuffle(sudoku)
    sudokulist = []
    sudokulist.append(sudoku)
    return(sudokulist)

#sudokulist is array with all the sudokus in it
sudokulist = []
longest = 0

#add one number to the sudoku
while True:

    #if the sudokulist is empty (all correct solutions were deleted due to )
    if len(sudokulist) == 0:
        print("RESTART", longest)
        sudokulist = start()
        longest = 0

    #this part of code delete some sudoku from sudokulist, so the memory wont overload
    #if limit high - slow program, if low - high chance of not getting any sudoku
    if len(sudokulist) > 50000:
        shuffle(sudokulist)
        for i in range(0, 10000):
            sudokulist.pop(0)

    #set sudoku to sudokulist[0], remove sudokulist[0]
    sudoku = deepcopy(sudokulist[0])
    sudokulist.pop(0)

    #try to add numbers 1-8 to the sudoku (last = number you are adding)
    for last in range(1, 9):

        #set x,y,z (positions of the number you are adding)
        position = len(sudoku)
        x = position % 8
        position = round((position - x) / 8)
        y = position % 8
        position = round((position - y) / 8)
        z = position % 8
        correct = True

        #if last already exist in LINE, the program will delete the sudoku
        for i in range(0, x):
            if sudoku[z*64 + y*8 + i] == last:
                correct = False

        #if last already exist in COLUMN, the program will delete the sudoku
        for i in range(0, y):
            if sudoku[z*64 + i*8 + x] == last:
                correct = False

        #if last already exist in VERTICAL LINE, the program will delete the sudoku
        for i in range(0, z):
            if sudoku[i*64 + y*8 + x] == last:
                correct = False

        #if last already exist in 2x2x2 CUBE, the program will delete the sudoku
        base = (z - z % 2)*64 +  (y - y % 2)*8 + (x - x % 2)
        above = len(sudoku) - base
        if above > 9:
            if sudoku[base] == last or sudoku[base + 1] == last or sudoku[base + 8] == last or sudoku[base + 9] == last:
                corect = False
            above -= 64
            base += 64
        if above > 1:
            if sudoku[base] == last or sudoku[base + 1] == last:
                correct = False
            above -= 8
            base += 8
        if above == 1:
            if sudoku[base] == last:
                correct = False

        #if the program didnt find last already exist in any object already add it to the sudokulist
        if correct:
            sudokuadd = deepcopy(sudoku)
            sudokuadd.append(last)
            sudokulist.append(sudokuadd)
            if len(sudokuadd) > longest:
                longest += 1

                #this part controlls if the program do any progress print the longest sudoku found
                if longest % 30 == 0:
                    print(longest)


这里有一个可疑的数独2x2x2。我只是用自己的想法创造了它,但它是对称的。



这些是8个8x8数字的表格。您选择一个表8x8,其中每行,列和2x2正方形都包含所有数字。我选择了:



12 34 56 78

34 12 78 56



56 78 12 34

78 56 34 12



21 43 65 87

43 21 87 65



87 87 21 43

87 65 43 21





表格也是彼此对称的。



>>>

到每个偶数表添加+每个号码都有4个。如果太高,请删除-8。

为每个不平衡的表添加+1。如果太高,请删除-8。

>>>



示例数独2x2x2:



12345678

34127856

56781234

78563412

21436587

43218765

65872143

87654321



56781234

78563412

12345678

34127856

65872143

87654321

21436587

43216378



23456781

45238167

67812345

81674521

32547618

54321876

81674523

54327618



67812345

81674523

23456781

45218167

76183254

18765432

45238167

76185432



34567812

56341278

78123456
12785634

43658721

34652187

87214365

21876543



78123456

1278 5634

34567812

56341278

87214365

21873465

43658721

65432187



45678123

67452381

81234567

23816745

54762381

76543218

18325476

32187654



81234567

23816745

45678123

67452381

23815476

32187654
54761832

76543218
One posible sudoku 2x2x2 here. I created it only with my mind, but it is symetrical.

These are 8 tables of 8x8 numbers. You choose one table 8x8, which has all numbers in each line, column and 2x2 square. I chose:

12 34 56 78
34 12 78 56

56 78 12 34
78 56 34 12

21 43 65 87
43 21 87 65

65 87 21 43
87 65 43 21


The tables are symetrical to each other too.

>>>
To every even table add +4 to every number. If too high, remove -8.
To every uneven table add +1. If too high, remove -8.
>>>

Example sudoku 2x2x2:

12345678
34127856
56781234
78563412
21436587
43218765
65872143
87654321

56781234
78563412
12345678
34127856
65872143
87654321
21436587
43216378

23456781
45238167
67812345
81674521
32547618
54321876
81674523
54327618

67812345
81674523
23456781
45218167
76183254
18765432
45238167
76185432

34567812
56341278
78123456
12785634
43658721
34652187
87214365
21876543

78123456
12785634
34567812
56341278
87214365
21873465
43658721
65432187

45678123
67452381
81234567
23816745
54762381
76543218
18325476
32187654

81234567
23816745
45678123
67452381
23815476
32187654
54761832
76543218


这篇关于解决3D数独的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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