Proog sudoku block algorithm?

How to get all the elements of blocks in the prolog? the size can change dynamically in my code, so the block size is different, 4x4 = 4 elements, 9x9 = 9 elements, etc. blocks are cut into squares, so in 4x4 the horizontal length is round (sqrt (4)) = 2 and the vertical length of the block is round (sqrt (4)) = 2. and 9x9 ... sqrt (9) .. therefore the height and width of the blocks equal to 3. I need an algorithm for the efficient use of elements.

my sudokulists are built like this:

L = [[4,3,1,2], [2,1,4,3], [3,4,2,1], [1,2,3,4]],

so a list with lists of lines in sudoku. checking rows and columns is not a problem, → all_different check with rows, move the whole list, check all_different with a transposed list.

but due to the dynamic size of sudoku, I cannot encode the fix code for the blocks. so anybody any idea? I thought about flatten (L) and worked with offsets to get the right blocks, but does it look pretty hard to do that way?

Please help me!

+3
source share
2 answers

A possible solution is as follows (it is assumed that you have blocksizex blocksizeblocks of size blocksizex blocksize- in standard sudoku all numbers are equal, they can be configured in accordance with other layouts)

  • Let be a = [],...,[]a list of codes blocksize.
  • Divide each line into parts blocksize.
  • , .. , .
  • a
  • blocksize x blocksize blocks

:

L=[ [4,3,1,2], [2,1,4,3], [3,4,2,1], [1,2,3,4] ]
Partitions => [[4,3] [1,2] [2,1] [4,3] [3,4] [2,1] [1,2] [3,4]]
Bucketed => [[4,3] [2,1] [3,4] [1,2]] [[1,2] [4,3] [2,1] [3,4]]
Flattened => [4,3,2,1,3,4,1,2,1,2,4,3,2,1,3,4]
Partitioned => [4,3,2,1], [3,4,1,2], [1,2,4,3], [2,1,3,4]]
+2

. , " ", , "" .

transpose([[ ]|_],[ ]) :- !.
transpose(A,[H|T]) :-
    decap_List(A,H,B),
    transpose(B,T).

decap_List([ ],[ ],[ ]).
decap_List([[H|T]|Rows],[H|Hs],[T|Ts]) :-
    decap_List(Rows,Hs,Ts).

:

?- transpose([[1,2,3],[4,5,6],[7,8,9]],X).

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

, , K²xK² " " "" KxK, . , , "" .

"" .

part_K_rows([ ],_,[ ]) :- !.
part_K_rows(A,K,[H|T]) :-
    get_K_rows(A,K,H,B),
    part_K_rows(B,K,T).

get_K_rows(A,0,[ ],A) :- !.
get_K_rows([H|T],K,[H|Z],B),
    J is K-1,
    get_K_rows(T,J,Z,B).

part_K_rows/3 sudoku, part_K_rows/3 "box".

+1

All Articles