'Traversing sub squares in sudoku
I have a 2d array of n^2Xn^2 dimensions (like a sudoku board, does not have to be valid numbers). I want to traverse each subsquare of nXn elements and put them into an array, but I'm having difficulty structuring the for loops.
If n=2, then my sudoku board could look like this:
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]]
And I want to traverse 1,2,5,6 then 3,4,7,8 then 9,10,13,14 then 11,12,15,16. I'm guessing there should be four for loops, two of them nested, but I'm having difficulty structuring them. I was thinking the first one should be something like
for i in range(0,n^2,int(n^2/n)):
something nested here
Someting here
something nested here
Solution 1:[1]
The terminology for this set of cells in sudoku is usually a region. Organizing it as four for loops is fairly natural, the outer two controlling which region you are looking at and the inner two scanning the cells within the region.
n = 3 # gives a 9x9 grid
for reg_r in range(n):
for reg_c in range(n):
for r in range(n):
for c in range(n):
print(board[n*reg_r + r][n*reg_c + c])
print('--------')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joffan |
