I'm facing a problem that I can't solve ...
Here it is, I get from a HTML page a sudoku in this form for example :
<table cellspacing="0" cellpadding="0" class="sudoku">
<tr>
<td class="chiffe1">0</td>
<td class="chiffe1">0</td>
<td class="chiffe1">0</td>
</tr>
<tr>
<td class="chiffe1">0</td>
<td class="chiffe1">0</td>
<td class="chiffe1">0</td>
</tr>
<tr>
<td class="chiffe1">0</td>
<td class="chiffe1">0</td>
<td class="chiffe1">0</td>
</tr>
</table>
The problem is that the incrementation for a 2D Array should be like this to fill it :
0,0
0,1
0,2
1,0
1,1
1,2
2,0
2,1
2,2
0,4
0,5
0,6
1,4
1,5
1,6
2,4
2,5
2,6
0,7
0,8
0,9
1,7
1,8
1,9 ...
For a tab with a size of [9][9].
My probleme is that I already programmed all the function to solve it but I was filling it with that logic :
for($i=0;$i<9;$i++){
for($j=0;$j<9;$j++){
$tab[$i][$j]=$val;
}
}
But it doesn't match at all, so all my results are wrong, and my problem is that I can't translate the logic order of the filling with mathematical algorithm.
Can someone help me to find the good iteration to initialize that sudoku ?
Thank you for your help and your attention.
You'll have to loop on a set of three rows and three columns at a time and shift $i and $j by the starting value of the set:
// sets of three for $i
for($iset=0; $iset<3; $iset++) {
$istart=$iset*3;
for($i=0;$i<3;$i++){
// sets of three for $j
for($jset=0; $jset<3; $jset++) {
$jstart=$jset*3;
for($j=0;$j<3;$j++){
$tab[$istart+$i][$jstart+$j]=$val;
}
}
}
}
So for sets (0, 1, 2) you'll have starting values (0, 3, 6). So you'll end up iterating through like this (which hopefully solves your problem):
i-> (0, 1, 2) j -> (0, 1, 2), (3, 4, 5), (6, 7, 8)
i-> (3, 4, 5) j -> (0, 1, 2), (3, 4, 5), (6, 7, 8)
i-> (6, 7, 8) j -> (0, 1, 2), (3, 4, 5), (6, 7, 8)