I started a program to make Sudoku as follows.
I was able to create 9x9 without any problems (without feeling slow). 16x16 immediately becomes heavy, like an infinite loop, and no end of processing.
In the above three, If there is no value to be placed in the square, initialize and re-run the two squares. seemed to repeat forever. I tried to write down the value I put on and omit it when I tried to run it again. It didn't matter much.(The sauce has become dirty and is omitted here)
Please tell me how to implement it from a different perspective and how to improve it in the source.
public function make()
{
// the number of blocks in Sudoku
$blkNum = 9;
// US>Initialize Mass
for($i=0;$i<$blkNum;$i++){
for($j=0;$j<$blkNum;$j++){
$d[$i][$j]=-1;
}
}
// Set start to upper left
$sx = 0;
$sy = 0;
$d[$sx][$sy] = land(1,$blkNum); // set start to init
// move vector
$dx = [0,1,0, -1,0];
$dy = [0,0,1,0,-1];
$queue = [[$sx,$sy]];
// 0 : x
// [1]—y
while($queue){
$now=array_shift($queue);
for($i=0;$i<5;$i++) {//vector movement num
// nx,ny —After move mass
$nx = $now[0]+$dx[$i];
$ny = $now[1]+$dy[$i];
if($nx>=0&$nx<$blkNum&$ny>=0&$ny<$blkNum&&isset($d[$nx][$ny])&$d[$nx][$ny]==-1){
// calculate the value to be inserted into
// *If you are doing something, pass the value at random, and if you do not duplicate it in rows, columns, or blocks, you will adopt the value.
$calc=$this->__put($d,$nx,$ny,range(1,$blkNum)));
if($calc['result']===true){// If successful, enter a value and push it
$d[$nx][$ny] = $calc['value'];
array_push($queue,[$nx,$ny]);
} else {// When the value is not successful
// Initialize overlapping parts.
// push through the queue
$d[$nx][$ny]=-1;
$d[$calc['err_x']][$calc['err_y']]=-1;
array_push($queue, [$calc['err_x', $calc['err_y']]]);
array_push($queue,[$nx,$ny]);
}
}
}
}
}
/**
* get a value for the __put parameter
*
*/
private function__put($d,$nx,$ny,$param)
{
// take a random array with a queue
shuffle($param);
while($param){
$p = array_shift($param);
// Check values (overlapping checks in rows, columns, and blocks)
$check = $this->__check($d, $nx, $ny, $p);
if($check['result']===true) {//check ok->return
return ['result'=>true,'value'=>$p];
} Reduce else {// check NG->param and regression
if($param){
return$this->__put($d,$nx,$ny,$param);
} Else {//param not present
// Return x,y caught in duplicate check because there was no value until the end.
return ['result'=>false,'value'=>$p,'err_x'=>$check['x'],'err_y'=>$check['y']);
}
}
}
}
I've only done a simple verification, so just for your information...
The procedure is
That's it.
As for python, please use the code below. You can generate simple Sudoku in ascending horizontal order.
def create(num=3):
num2 = num*num
g = [[0]* num2 for i in range (num2)]
For y in range (num2):
yy=(y*num)%num2+int(y/num)
for x in range (num2):
g[x][yy]=(num2-y+x)%num2+1
9x9(num=3) means
abc de fghi
1 2 3 4 5 6 7 8 9
7 8 9 1 2 3 4 5 6
4 5 6 7 8 9 1 2 3
9 1 2 3 4 5 6 7 8
6 7 8 9 1 2 3 4 5
3 4 5 6 7 8 9 1 2
8 9 1 2 3 4 5 6 7
5 6 7 8 9 1 2 3 4
2 3 4 5 6 7 8 9 1
That's what it looks like. (a-z is used in 2 so I gave it to you.)
1. Shuffle what you generated in so that Sudoku is not broken.
Sudoku will not be broken if the following conditions are met.
Replace columns a,b,c and d,e,f(org,h,i)
This can also be done in rows, so if you run a total of four shuffles enough times, I think it will be random Sudoku.
Replace columns a, b, c and d, e, f(org, h, i)
This can also be done in rows, so if you run a total of four shuffles enough times, I think it will be random Sudoku.
First, flag all empty squares with all numbers. 16 x 16 means that you can set the numbers from 1 to 16 in each empty square.
Then repeat the following four steps:
You should be able to do it quite efficiently, but please also note that there are patterns that cannot be solved.
972538164
861 749 235
345AB6789
789 CD3456
126 954 378
453 867 912
214 675 893
697 382 541
538 491 627
There are two answers. A = 1, B = 2, C = 2, D = 1 with A = 2, B = 1, C = 1, D = 2 That's it.
It's like an infinite loop, so maybe you're solving a problem that you can't solve. I've heard that 9x9 doesn't solve the problem if the first number you know is less than 6.
I think the termination conditions are difficult.
© 2024 OneMinuteCode. All rights reserved.