汚いソースですが…
$lsts = array(
array( 0, 1, 2, 3, 4, 5, 6, 7, 8),
array( 9,10,11,12,13,14,15,16,17),
array(18,19,20,21,22,23,24,25,26),
array(27,28,29,30,31,32,33,34,35),
array(36,37,38,39,40,41,42,43,44),
array(45,46,47,48,49,50,51,52,53),
array(54,55,56,57,58,59,60,61,62),
array(63,64,65,66,67,68,69,70,71),
array(72,73,74,75,76,77,78,79,80),
array( 0, 9,18,27,36,45,54,63,72),
array( 1,10,19,28,37,46,55,64,73),
array( 2,11,20,29,38,47,56,65,74),
array( 3,12,21,30,39,48,57,66,75),
array( 4,13,22,31,40,49,58,67,76),
array( 5,14,23,32,41,50,59,68,77),
array( 6,15,24,33,42,51,60,69,78),
array( 7,16,25,34,43,52,61,70,79),
array( 8,17,26,35,44,53,62,71,80),
array( 0, 1, 2, 9,10,11,18,19,20),
array( 3, 4, 5,12,13,14,21,22,23),
array( 6, 7, 8,15,16,17,24,25,26),
array(27,28,29,36,37,38,45,46,47),
array(30,31,32,39,40,41,48,49,50),
array(33,34,35,42,43,44,51,52,53),
array(54,55,56,63,64,65,72,73,74),
array(57,58,59,66,67,68,75,76,77),
array(60,61,62,69,70,71,78,79,80)
);
$rst = array();
$rst['flag'] = true;
// $target = array(
// 0,0,0, 0,0,0, 0,0,0,
// 0,1,2, 0,0,0, 0,9,0,
// 7,0,0, 9,0,0, 2,1,0,
// 0,0,0, 6,0,3, 0,8,0,
// 0,0,8, 0,0,5, 0,2,0,
// 0,3,0, 0,7,2, 1,6,5,
// 4,0,0, 0,0,0, 0,3,0,
// 8,6,1, 2,0,0, 0,7,0,
// 0,0,0, 0,0,0, 0,0,0
// );
$target = $_POST['lists'];
$tinx = array();
for ($i = 0; $i < count($target); $i++)
{
if ($target[$i] == 0)
{
$tinx [] = $i;
}
}
$i = 0;
$flag = false;
while(true)
{
do
{
$target[$tinx[$i]]++;
if ($target[$tinx[$i]] > 9)
{
$target[$tinx[$i]] = 0;
$i--;
$flag = true;
break;
}
}
while (check_duplicate($lsts, $target) == false);
if ($flag == true)
{
$flag = false;
continue;
}
$i++;
if ($i < 0)
{
$rst['flag'] = false;
break;
}
else if ($i >= count($tinx))
{
break;
}
}
$rst['lists'] = $target;
echo json_encode($rst);
function check_duplicate($lsts, $tgt)
{
foreach($lsts as $lst)
{
$arr = array();
foreach($lst as $key)
{
if ($tgt[$key] != 0)
$arr[] = $tgt[$key];
}
if (count($arr) > 1)
{
$value_count = array_count_values($arr);
if (max($value_count) > 1)
{
// echo var_dump($lst);
return false;
}
}
}
return true;
}
https://sample.keaven.cyou/sudoku/