sudoku
<?php
$type = isset($_POST'sudoku_type']) ? $_POST'sudoku_type'] : 'chance';
print '<FORM ACTION=sudoku METHOD=POST>';
print ' <SELECT NAME=sudoku_type>';
IF ('chance' == $type) {
print ' <OPTION VALUE=chance SELECTED>Chance It</OPTION>';
print ' <OPTION VALUE=make>Make One (Takes ~10s.)</OPTION>';
} else {
print ' <OPTION VALUE=chance>Chance It</OPTION>';
print ' <OPTION VALUE=make SELECTED>Make One (Takes ~10s.)</OPTION>';
}
print ' </SELECT>';
print ' <INPUT TYPE=SUBMIT>';
print '</FORM><p></p><p></p>';
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];
function check(&$grid, $ii, $jj, $value)
{
if (0 == $value) {
return false;
}
for ($kk = 0; $kk < 9; $kk++) {
if ($value == $grid$ii]$kk]) {
return false;
}
}
for ($kk = 0; $kk < 9; $kk++) {
if ($value == $grid$kk]$jj]) {
return false;
}
}
$cell = array();
$ith = 3*floor(($ii) / 3);
$jth = 3*floor(($jj) / 3);
for ($i = $ith; $i < $ith + 3; $i++) {
for ($j = $jth; $j < $jth + 3; $j++) {
array_push($cell, $grid$i]$j]);
}
}
for ($i = 0; $i < sizeof($cell); $i++) {
if ($value == $cell$i]) {
return false;
}
}
return true;
}
$checks = array();
$grid = array();
for ($ii = 0; $ii < 9; $ii++) {
array_push($checks, array(0, 0, 0, 0, 0, 0, 0, 0, 0));
array_push($grid, array(0, 0, 0, 0, 0, 0, 0, 0, 0));
}
for ($ii = 0; $ii < 9; $ii++) {
$jcheck = 0;
for ($jj = 0; $jj < 9; $jj++) {
srand((double)microtime()*1000000);
$try = rand(1,9);
while (!check($grid, $ii, $jj, $try)) {
while ($checks$ii]$jj] == 8) {
$grid$ii]$jj] = 0;
if (0 == $jj) {
if (0 == $ii) {
for ($i = 0; $i < 9; $i++) {
for ($j = 0; $j < 9; $j++) {
$checks$i]$j] = 0;
}
}
} else {
$ii--;
$jj = 8;
}
} else {
$jj--;
}
$try = $grid$ii]$jj];
$try++;
}
if ($try > 8) {
$try = 1;
} else {
$try++;
}
$checks$ii]$jj]++;
if ('chance' == $type && $checks$ii]$jj] == 8) {
break 2;
}
}
$grid$ii]$jj] = $try;
}
}
$output = "<table width=320 height=180 border=1>\n";
for ($ii = 0; $ii < 9; $ii++) {
$output .= " <tr>\n";
for ($jj = 0; $jj < 9; $jj++) {
$output .= " <td align=center><font size=large>" . $grid$ii]$jj] . "</font></td>\n";
}
$output .= " </tr>\n";
}
$output .= "</table>\n";
print $output;
$etimer = explode( ' ', microtime() );
$etimer = $etimer[1] + $etimer[0];
printf( "<p></p><p></p><b>%f</b> seconds.", ($etimer-$stimer) );
if ('make' == $type) {
print '<br>';
print '<i>Optimization, anyone?</i>';
}
?>
Can we do them?
That's the goal.
you know you have a Sudoku Problem when _____.
_______________________________
I can't understand why people are frightened of new ideas. I'm frightened of the old ones.
-John Cage
_______________________________
I can't understand why people are frightened of new ideas. I'm frightened of the old ones.
-John Cage
Or is there an art to blanking, which is why Will Shortz edits these things?
So, even if the blanks were computed in an artful way, the underlying puzzle might still be un-appealing?