Saya mengubah versi Java yang saya tulis ke versi JavaScript. Pertama kali saya menulisnya, itu sangat tidak profesional, saya memaafkan saya. Sayangnya, bagaimana saya bebas.
Salinan kode adalah sebagai berikut:
var sudoku = {
init: function (str) {
this.blank = [];
this.fixed = [];
this.cell = [];
this.trials = [];
untuk (i = 0; i <81; i ++) {
var chr = str.charcodeat (i);
if (chr == 48) {
this.cell [i] = 511;
this.blank.push (i);
} kalau tidak {
this.cell [i] = 1 << chr - 49;
this.fixed.push (i);
}
}
},
showboard: function () {
var board = "";
untuk (var i = 0; i <81; i ++) {
if (i % 9 == 0) {
papan = board.concat ("/n");
}
board = board.concat ("[");
untuk (var j = 0; j <9; j ++) {
if ((this.cell [i] >> j & 1) == 1) {
board = board.concat (String.FromCharCode (j + 49));
}
}
board = board.concat ("]");
}
papan kembali;
},
periksa: function () {
var checkpoint = [0, 12, 24, 28, 40, 52, 56, 68, 80];
untuk (var i di pos pemeriksaan) {
var r, b, c;
r = b = c = this.cell [checkpoint [i]];
untuk (j = 0; j <8; j ++) {
c ^= this.cell [this.getx (checkpoint [i]) [j]];
b ^= this.cell [this.getx (checkpoint [i]) [8 + j]];
r ^= this.cell [this.getx (checkpoint [i]) [16 + j]];
}
if ((r & b & c)! = 0x1ff) {
mengembalikan false;
}
}
Kembali Benar;
},
bitcount: function (i) {
var n = 0;
untuk (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 1)
n ++;
}
kembali n;
},
NumberOfTrailingZeros: function (i) {
var n = 0;
untuk (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 0)
n ++;
kalau tidak{
merusak;
}
}
kembali n;
},
UpdateCandidates: function () {
untuk (var i di this.fixed) {
var opt = 0x1ff ^ this.cell [this.fixed [i]];
untuk (var j = 0; j <24; j ++) {
this.cell [this.getx (this.fixed [i]) [j]] & = opt;
//!Melihat
if (this.cell [this.getx (this.fixed [i]) [j]] == 0) {
//console.log("Error-0 kandidat: "+x [this.fixed [i]] [j]);
mengembalikan false;
}
}
}
Kembali Benar;
},
Seekuniquecandidate: function () {
untuk (var Bidx di this.blank) {
var row = 0, col = 0, box = 0;
untuk (i = 0; i <8; i ++) {
baris | = this.cell [this.getx (this.blank [BIDX]) [i]];
box | = this.cell [this.getx (this.blank [BIDX]) [8 + i]];
col | = this.cell [this.getx (this.blank [BIDX]) [16 + i]];
}
if (this.bitcount (this.cell [this.blank [Bidx]] & ~ row) == 1) {
this.cell [this.blank [BIDX]] & = ~ row;
melanjutkan;
}
if (this.bitcount (this.cell [this.blank [Bidx]] & ~ col) == 1) {
this.cell [this.blank [BIDX]] & = ~ col;
melanjutkan;
}
if (this.bitcount (this.cell [this.blank [Bidx]] & ~ box) == 1) {
this.cell [this.blank [BIDX]] & = ~ box;
}
}
},
SeekFilledable: function () {
this.fixed = [];
var _del = [];
untuk (var i di this.blank) {
if (this.bitcount (this.cell [this.blank [i]]) == 1) {
this.fixed.push (this.blank [i]);
//console.log("fixed :"+this.blankəi media+omend
//this.blank.splice(i, 1); // untuk menghapusnya di loop akan menyebabkan bug
_del.push (i);
}
}
while (_del.length> 0) {
this.blank.splice (_del.pop (), 1);
}
},
seekmutexcell: function () {
var dua = [];
untuk (var n di this.blank) {
if (this.bitcount (this.cell [this.blank [n]]) == 2) {
Two.push (this.blank [n]);
}
}
untuk (var i = 0; i <dua.length; i ++) {
untuk (var j = i+1; j <two.length; j ++) {
if (this.cell [dua [i]] == this.cell [dua [j]]) {
var opt = ~ this.cell [dua [i]];
if (parseInt (dua [i] / 9) == parseInt (dua [j] / 9)) {
untuk (n = 0; n <8; n ++) {
this.cell [this.getx (dua [i]) [n]] & = opt;
}
}
if ((dua [i] - dua [j]) % 9 == 0) {
untuk (n = 8; n <16; n ++) {
this.cell [this.getx (dua [i]) [n]] & = opt;
}
}
if ((parseInt (dua [i] / 27) * 3 + parseInt (dua [i] % 9/3)) == (parseInt (dua [j] / 27) * 3 + parseInt (dua [j] % 9/3))) {
untuk (n = 16; n <24; n ++) {
this.cell [this.getx (dua [i]) [n]] & = opt;
}
}
this.cell [dua [j]] = ~ opt;
}
}
}
},
dasarve: function () {
Mengerjakan {
if (! this.updateCandidates (this.fixed)) {
this.backforward ();
}
this.seekuniquecandidate ();
this.seekmutexcell ();
this.seekFilledable ();
} while (this.fixed.length! = 0);
kembalikan this.blank.length == 0;
},
settrialcell: function () {
untuk (var i di this.blank) {
if (this.bitcount (this.cell [this.blank [i]]) == 2) {
var uji coba = 1 << this.numberoftrailingzeros (this.cell [this.blank [i]]);
var waitingValue = this.cell [this.blank [i]] ^ TrialValue;
//console.log ("Coba: [" + this.blank [i] + "]->" + (this.numberoftrailingzeros (TrialValue) + 1) + "#" + (this.numberoftrailingzeros (waitingValue) + 1));
this.cell [this.blank [i]] = TrialValue;
this.trials.push (this.createRialpoint (this.blank [i], waitingValue, this.cell));
Kembali Benar;
}
}
mengembalikan false;
},
backforward: function () {
if (this.trials.length == 0) {
Console.log ("Mungkin tidak ada solusi!");
kembali;
}
var back = this.trials.pop ();
this.reset (back.data);
this.cell [back.idx] = back.val;
this.fixed.push (back.idx);
//console.log ("back: [" + back.idx + "]->" + (this.numberoftrailingzeros (back.val) + 1));
},
Reset: function (data) {
this.blank = [];
this.fixed = [];
this.cell = data.concat ();
untuk (var i = 0; i <81; i ++) {
if (this.bitcount (this.cell [i])! = 1) {
this.blank.push (i);
} kalau tidak {
this.fixed.push (i);
}
}
},
uji coba: function () {
while (this.blank.length! = 0) {
if (this.setTrialCell ()) {
this.basicsolve ();
} kalau tidak {
if (this.trials.length == 0) {
//console.log(" tidak bisa mundur! Mungkin tidak ada solusi! ");
merusak;
} kalau tidak {
this.backforward ();
this.basicsolve ();
}
}
}
},
play: function () {
console.log (this.showboard ());
var start = new date (). getMilliseconds ();
if (! this.basicsolve ()) {
this.trialsolve ();
}
var end = tanggal baru (). getMilliseconds ();
console.log (this.showboard ());
if (this.check ()) {
console.log ("[" + (end - start) + "ms ok!]");
} kalau tidak {
console.log ("[" + (end - start) + "ms, tidak dapat menyelesaikannya?");
}
// kembalikan this.showboard ();
},
getx: function (idx) {
var tetangga = array baru (24);
var box = array baru (0,1,2,9,10,11,18,19,20);
var r = parseInt (idx/9);
var c = idx%9;
var xs = parseInt (idx/27)*27+parseInt (idx%9/3)*3;
var i = 0;
untuk (var n = 0; n <9; n ++) {
if (n == c) lanjutkan;
tetangga [i ++] = r*9+n;
}
untuk (var n = 0; n <9; n ++) {
if (n == r) Lanjutkan;
tetangga [i ++] = c+n*9;
}
untuk (var n = 0; n <9; n ++) {
var t = xs+box [n];
if (t == idx) lanjutkan;
tetangga [i ++] = t;
}
tetangga yang kembali;
},
CreateTrialPoint: Function (idx, val, board) {
var tp = {};
tp.idx = idx;
tp.val = val;
tp.data = board.concat ();
mengembalikan tp;
}
};
//Sudoku.init("0000005000000083006001000008009300000000020700000000058000000000200017090000060 ");
//Sudoku.init("5300700006001950098000060800060003400803001700020006060000280000419005000080079 ");
Sudoku.init ("80000000000360000007009020005000700000045700000100030001000068008500010090000400");
Sudoku.play ();
Di atas adalah semua kode untuk mengimplementasikan solusi Sudoku menggunakan JavaScript, saya harap Anda menyukainya.