<html>
<head>
<title>Sukuna problem solving program</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<script>
function keygo(evt,obj){
key = window .event?:;
var next= ;
var inputs=("input");
if (key==38){//↑
if (next -9>=0 ) {
inputs[next-9].select()
}
}
if (key==40){//↓
if (next +9<81 ) {
inputs[next+9].select()
}
}
if (key==37){//←
if (next -1>=0 ) {
inputs[next-1].select()
}
}
if (key==39){//→
if(next+1<81)inputs[next+1].select();
}
}
</script>
</head>
<body onload="init();">
<div ></div><input type=button name="cal" onclick="calculate()" value="calculate">
<input type=button name="clear" onclick="clearGrid()" value="Clear">
<br><span><textarea cols="19" rows="10">004502006
000000005
002014007
008000012
070080050
930020700
600190200
020000000
300208500</textarea></span>
<input type=button name="cal" onclick="paste()" value="paste" > You can copy the text to the lower box and paste it after clicking it. The 81 digit sequence from left to right from top to bottom is not filled in. The non-numeric characters in the middle will be ignored<br>
<span></span><br><span ></span><span ></span>
<script>
var maxRow =9;
var maxCol = 9;
var strTbody = ["<table id='sodukuTable' border='0'><tbody>"];
for(var i = 0; i < maxRow; i++){
("<tr>");
for(var j = 0; j < maxCol; j++){
("<td style='border-left:"+(j%3==0?1:0)
+"px solid black ;border-right:"+(j%3==2?1:0)
+"px solid black;border-top:"+(i%3==0?1:0)
+"px solid black;border-bottom:"+(i%3==2?1:0)+"px solid black;' ><input style='width:20px;' tabindex='"+(i*9+j)
+"'onclick='check(this);' onKeyUp='return keygo(event,this)'type='text' id='input"+(i*9+j)+"'name='n"+(i*9+j)+"'value='"+i+j+"' ></td>");
}
("</tr>");
}
("</tbody></table>");
var sTbody = ["<table border='1'><tbody>"];
for(var i = 0; i < maxRow; i++){
("<tr>");
for(var j = 0; j < maxCol; j++){
("<td style='border-left:"+(j%3==0?1:0)
+"px solid black ;border-right:"+(j%3==2?1:0)
+"px solid black;border-top:"+(i%3==0?1:0)
+"px solid black;border-bottom:"+(i%3==2?1:0)+"px solid black;'><div style='width:25px;height:25px' id='status"+(i*9+j)+"'name='div"+i+j+"' ></div></td>");
}
("</tr>");
}
("</tbody></table>");
var obj = ("sodukuTable");
= ("");
var obj2 = ("statusDiv");
var grid=[
[5, 7, 0, 1, 2, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 6, 7, 0, 0, 0, 8, 0],
[3, 0, 4, 0, 0, 9, 0, 7, 0],
[0, 2, 0, 0, 7, 0, 0, 5, 0],
[0, 1, 0, 3, 0, 0, 9, 0, 2],
[0, 8, 0, 0, 0, 2, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 5, 4, 0, 6, 3]];
var candidatNum=[];
var columns=[];
var rows=[];
var blook=[];
var papers=0;
var discards=0;
var success=false;
var steps = new Array();
var log1 = ("statusDiv");
function Step(current1,arrys){
this.temp1=new Array();
=[arrys[0],arrys[1],arrys[2]];
for (var i = 0; i < 9; i++)
{
this.temp1[i]=new Array();
for (var j = 0; j < 9; j++)
{
this.temp1[i][j]=current1[i][j];
}
}
}
out(grid);
init();
function push( current1, i, j, n) {
var s = new Step(current1, [ i, j, n ]);
(s);
}
function pop(){
var step = ();
discards ++;
grid=step.temp1;
grid[[0]][[1]] = [2];
var timeline = ('PaperList');
+= ('discard: ['+discards+']:['+papers+']\n');
= ;
return step;
}
function check(obj){
if(==0)return;
for(var i=0;i<9;i++){
for(var j=0;j<9;j++){
var text = ("input"+(i*9+j));
if(==){
="green";
}else{
="";
}
}
}
}
function CheckNumInput(array,num, x, y) {
// Target:
// �
// There is no conflict in the palaces of ranks, return true;
// �
if (((rows[x] & (1 << num)) == 0) && (columns[y] & (1 << num)) == 0
&& (blook[parseInt(x / 3) * 3 + parseInt(y / 3)] & (1 << num)) == 0) {
return true;
}
return false;
}
function out(array){
var result=true;
for (var i = 0; i < 9; i++)
{
for (var j = 0; j < 9; j++)
{
("input"+(i*9+j)).value=array[i][j];
if(array[i][j]==0)result=false;
}
}
return result;
}
function setOne(){
var result = false;
//Target:
// traversing the matrix, can it be refreshed simply at present, or can an error be found.
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
//Target:
// (grid[i][j] == 0 && candidateNum[i][j][0] == 0) >> There is no candidate number, something went wrong
// (candidatNum[i][j][0] == 1)
// CheckNumInput(grid,candidatNum[i][j][10],i,j) >> Check if this number is logical
//Judge There is no candidate number||The last candidate number does not meet the logical conditions, fall back or return an error from here
if (grid[i][j] == 0 && candidatNum[i][j][0] == 0||
(candidatNum[i][j][0] == 1&&!CheckNumInput(grid,candidatNum[i][j][10],i,j))) {
if (grid[i][j] == 0) {
result = false;
}
if (>0) {// Back
pop();
return true;
} else {
if (!success) {
alert("Stack is empty ends!"); //The question is wrong, end
}
return false;
}
}
if (candidatNum[i][j][0] == 1) { �
grid[i][j] = candidateNum[i][j][10]; // Update the matrix
refreshStat3(candidatNum[i][j][10],i,j); // Update the palace of ranks
candidateNum[i][j][0] = 0; �
result = true;
continue;
}
}
}
if (result == false) { //Filter for the case of (candidatNum[i][j][0] != 1), filter
return choose();
}
return result;
}
function refreshStat3( num, x, y) { // Update the palace of ranks
rows[x] |= 1<<num;
columns[y] |= 1<<num;
blook[parseInt(x / 3) * 3 + parseInt(y / 3)] |= 1 << num ;
}
/*********************
* Matrix Data Analysis
* Statistics Remainable options
*********************/
function refreshStat(){
var over = true;
// Target:
// Decomposition of rows/columns/palaces
for (var i = 0; i < 9; i++) {
rows[i] = 0; //Line
columns[i] = 0; //Column
blook[i] = 0; //official
for (var j = 0; j < 9; j++) {
if (grid[i][j] != 0) {
rows[i] |= 1 << grid[i][j];
} else {
rows[i] &= ~(1 << grid[i][j]);
}
if (grid[j][i] != 0) {
columns[i] |= 1 << grid[j][i];
} else {
columns[i] &= ~(1 << grid[j][i]);
}
if (grid[parseInt(i / 3) * 3 + parseInt(j / 3)][i % 3 * 3 + j % 3] != 0) {
blook[i] |= 1 << grid[parseInt(i / 3 )* 3 + parseInt(j / 3)][i % 3 * 3 + j % 3];
}
}
}
// Target:
// �
// candidateNum[i][j][0] = 0; >>>> �
// candidateNum[i][j][0] = k; >>>> Number of possible values
// candidateNum[i][j][1] = 987654321x Optional digits represented by digits
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
if (grid[i][j] != 0) {
candidatNum[i][j][0] = 0;
continue;
}
var size = 0;
over = false;
for (var k = 1; k < 10; k++) {
if (CheckNumInput(grid, k, i, j)) {
candidatNum[i][j][1] |= 1 << k;
candidatNum[i][j][10] = k;
over = false;
size++;
} else {
candidatNum[i][j][1] &= ~(1 << k);
}
}
candidateNum[i][j][0] = size; // Mark the remaining number of options
}
}
return over;
}
function calculate(){ //Function portal
//Read data
var start=new Date();
for (var i = 0; i < 9; i++)
{
for (var j = 0; j < 9; j++)
{
var text = ("input"+(i*9+j));
grid[i][j]=parseInt();
}
}
//Refresh the grid
refreshStat();
out(grid);
//Computing matrix
while(true){
var a=setOne();
var b=refreshStat();
if(!a||b){ //If a==false or b==ture, the loop can be jumped
break;
}
}
out(grid); //Answer
alert("Time: "+(new Date()-start)+"ms");
success = true;
//The calculation ends
//Verify whether the answer is unique
if (papers != discards){
if (>0) {// Back
pop();
//Computing matrix
while(true){
var a=setOne();
var b=refreshStat();
if(!a||b){ //If a==false or b==ture, the loop can be jumped
break;
}
}
if (b) {
alert("The answer is not unique! Record!");
out(grid); //Answer
}
else {
alert("The answer is unique!!"); //The answer is unique
}
} else {
alert("Error ended!");
return false;
}
}
}
function clearGrid(){
for (var i = 0; i < 9; i++){
for (var j = 0; j < 9; j++){
grid[i][j]=0;
("input"+(i*9+j)).value=grid[i][j];
}
}
out(grid);
}
function init(){
for (var i = 0; i < 9; i++)
{ candidatNum[i]=new Array();
for (var j = 0; j < 9; j++)
{ candidatNum[i][j]=new Array();
for (var k = 0; k < 11; k++)
{ candidatNum[i][j][k]=0;
}
}
}
}
function choose() {
//Target:
// Transfer the matrix and create a search branch from the current location.
var binarynode = false;
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
// 2 Forktree Branch:
// If there are two possibilities in a certain position, option 1/option 2
// �
if (candidatNum[i][j][0] == 2) {// There are 2 options
binarynode = true;
var found = -1;
for (var k = 1; k < 10; k++) {
if ((candidatNum[i][j][1] & (1 << k)) > 0) {
if (found > 0) {
papers ++;
var timeline = ('PaperList');
+= ('add papers:'+papers+':'+i+' '+j+' '+k+'\n');
= ;
push(grid, i, j, k);
}else{
found = k;
}
}
}
grid[i][j] = found;
candidateNum[i][j][0] = 0; // Mark selected on the current label
var timeline = ('PaperList');
+= ('TRY CURRENT:'+i+' '+j+' '+found+'\n');
= ;
return true;
}
}
}
if (!binarynode) {
var timeline = ('PaperList');
+= ('2 fork branch not found!\n');
= ;
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
// When the 2 forktree search fails, start the 3 forktree branch. As an expansion, you can also start the 3 forktree branch:
// �
// �
if (candidatNum[i][j][0] == 3) {// There are 3 options
var timeline = ('PaperList');
+= ('Discover 3 fork branches!\n');
= ;
binarynode = true;
var found = -1;
for (var k = 1; k < 10; k++) {
if ((candidatNum[i][j][1] & (1 << k)) > 0) {
if (found > 0) {
papers ++;
var timeline = ('PaperList');
+= ('add papers:'+papers+':'+i+' '+j+' '+k+'\n');
= ;
push(grid, i, j, k);
}else{
found = k;
}
}
}
grid[i][j] = found;
candidateNum[i][j][0] = 0; // Mark selected on the current label
var timeline = ('PaperList');
+= ('TRY CURRENT:'+i+' '+j+' '+found+'\n');
= ;
return true;
}
}
}
}
return false;
}
function paste(){
var gridstr= ("gtxt").value;
var a = (/[^0-9]/g,'');
if(!=81){
alert("Data format is incorrect:\n"+gridstr);
return;
}
for (var i = 0; i < 9; i++)
{
for (var j = 0; j < 9; j++){
grid[i][j]=(i*9+j);
("input"+(i*9+j)).value=grid[i][j];
}
}
out(grid);
papers = 0;
discards = 0;
success = false;
steps = new Array();
}
</script>
It is recommended to use IE browser, F12 to enable debugging mode<br>
<br><span>
<textarea cols="30" rows="20" style="position:absolute;left:550px;"></textarea></span>
</body>
</html>