Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prolog sudoku block algorithm?

how to get all elements of the blocks in prolog? the size can change dynamic in my code, so the blocksize differs, 4x4 = 4 elements, 9x9= 9 elements etc. the blocks are cut into squares, so in 4x4 the horizontal length is round(sqrt(4))= 2 and vertical length of the block is round(sqrt(4)) = 2. and 9x9 ... sqrt(9).. so height and width of the blocks are 3. i need an algorithm to get the elements efficient.

my sudokulists are build up this way:

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

so a list, with lists of the rows in sudoku. checking rows and columns is no problem, -> all_different check with rows, transpose the whole List, all_different check with the transposed list.

but because of the dynamic size of the sudoku, i cannot code fix code for the blocks. so anyone any idea? i thought about flatten(L) and working with offsets to get the correct blocks, but it seems pretty hard to do that this way?

please help me!

like image 424
viktor Avatar asked Jan 28 '26 22:01

viktor


1 Answers

A possible solution goes as follows (assuming you have blocksizexblocksize blocks of size blocksizexblocksize - in standard sudoku all numbers are equal, can be adjusted to match other layouts)

  1. Let a = [],...,[] be a list of blocksize buckets.
  2. Partition each row into blocksize parts.
  3. Put first part into first bucket, second into second, and so on. If you reached last bucket start with first one again.
  4. Flatten out a completely
  5. Partition the result again into blocksizexblocksize blocks

In your example:

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]]
like image 200
Howard Avatar answered Jan 30 '26 16:01

Howard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!