Ich habe die Java -Version geändert, die ich in eine JavaScript -Version geschrieben habe. Leider, wie ich frei bin.
Die Codekopie lautet wie folgt:
var sudoku = {
init: function (str) {
this.blank = [];
this.fixed = [];
this.cell = [];
this.trials = [];
für (i = 0; i <81; i ++) {
var chr = str.charcodeat (i);
if (chr == 48) {
this.cell [i] = 511;
this.blank.push (i);
} anders {
this.cell [i] = 1 << chr - 49;
this.fixed.push (i);
}
}
},
Showboard: function () {
var board = "";
für (var i = 0; i <81; i ++) {
if (i % 9 == 0) {
board = board.concat ("/n");
}
board = board.concat ("[");
für (var j = 0; j <9; j ++) {
if ((this.cell [i] >> j & 1) == 1) {
board = board.concat (string.fromCharCode (j + 49));
}
}
board = board.concat ("]");
}
Rückgabeversorgungsbehörde;
},
Überprüfen Sie: function () {
var Checkpoint = [0, 12, 24, 28, 40, 52, 56, 68, 80];
für (var i im Checkpoint) {
var r, b, c;
r = b = c = this.cell [Checkpoint [i]];
für (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) {
false zurückgeben;
}
}
zurückkehren;
},
Bitcount: Funktion (i) {
var n = 0;
für (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 1)
n ++;
}
return n;
},
NumberOfTrailingzeros: Funktion (i) {
var n = 0;
für (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 0)
n ++;
anders{
brechen;
}
}
return n;
},
UpdateCandidates: function () {
für (var i in this.fixed) {
var opt = 0x1ff ^ this.cell [this.fixed [i]];
für (var j = 0; j <24; j ++) {
this.cell [this.getX (this.fixed [i]) [j]] & = opt;
//!Beachten
if (this.cell [this.getX (this.fixed [i]) [j]] == 0) {
//console.log("Error-0 Kandidat: "+x [this.fixed [i]] [j]);
false zurückgeben;
}
}
}
zurückkehren;
},
seeeuneuniquecandidate: function () {
für (var bidx in this.blank) {
var row = 0, col = 0, box = 0;
für (i = 0; i <8; i ++) {
row | = 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;
weitermachen;
}
if (this.bitCount (this.cell [this.blank [bidx]] & ~ col) == 1) {
this.cell [this.blank [bidx]] & = ~ col;
weitermachen;
}
if (this.bitCount (this.cell [this.blank [bidx]] & ~ box) == 1) {
this.cell [this.blank [bidx]] & = ~ Box;
}
}
},
suchFillestable: function () {
this.fixed = [];
var _del = [];
für (var i in this.blank) {
if (this.bitCount (this.cell [this.blank [i]]) == 1) {
this.fixed.push (this.blank [i]);
//console.log("Fixed:"+This.BlANK;
//this.blank.spleplice(i, 1); //, um es in der Schleife zu löschen, würde Fehler verursachen
_del.push (i);
}
}
while (_del.length> 0) {
this.blank.splice (_del.pop (), 1);
}
},
seeearmutexcell: function () {
var zwei = [];
für (var n in this.blank) {
if (this.bitCount (this.cell [this.blank [n]]) == 2) {
two.push (this.blank [n]);
}
}
für (var i = 0; i <zwei.Length; i ++) {
für (var j = i+1; j <zwei.länge; j ++) {
if (this.cell [zwei [i]] == this.cell [zwei [j]]) {
var opt = ~ this.cell [zwei [i]];
if (parseInt (zwei [i] / 9) == parseInt (zwei [j] / 9)) {
für (n = 0; n <8; n ++) {
this.cell [this.getX (zwei [i]) [n]] & = opt;
}
}
if ((zwei [i] - zwei [j]) % 9 == 0) {
für (n = 8; n <16; n ++) {
this.cell [this.getX (zwei [i]) [n]] & = opt;
}
}
if ((parseInt (zwei [i] / 27) * 3 + parseInt (zwei [i] % 9 /3) == (ParseInt (zwei [j] / 27) * 3 + ParseInt (zwei [j] % 9 /3)) {
für (n = 16; n <24; n ++) {
this.cell [this.getX (zwei [i]) [n]] & = opt;
}
}
this.cell [zwei [j]] = ~ opt;
}
}
}
},
BasicSolve: function () {
Tun {
if (! this.updatecandidates (this.fixed)) {
this.backforward ();
}
this.seekuniquecandidate ();
this.seekmutexcell ();
this.seekFillingable ();
} while (this.fixed.length! = 0);
return this.blank.length == 0;
},
setTrialCell: function () {
für (var i in this.blank) {
if (this.bitCount (this.cell [this.blank [i]]) == 2) {
var trialValue = 1 << this.numberoftrailingzeros (this.cell [this.blank [i]]);
var waitingValue = this.cell [this.blank [i]] ^ trialValue;
//console.log ("try: [" + this.blank [i] + "]->" + (this.numberoftrailingzeros (trialValue) + 1) + "#" + (this.numberoftrailingzeros (WaitingValue) + 1));
this.cell [this.blank [i]] = trialValue;
this.trials.push (this.createtrialPoint (this.blank [i], WaitingValue, this.cell));
zurückkehren;
}
}
false zurückgeben;
},
Rückwärts: function () {
if (this.trials.length == 0) {
console.log ("Vielleicht keine Lösung!");
zurückkehren;
}
var back = this.trials.pop ();
this.reset (zurück.data);
this.cell [zurück.idx] = zurück.val;
this.fixed.push (back.idx);
//console.log ("zurück: [" + zurück.idx + "]->" + (this.numberOfTrailingzeros (zurück.val) + 1));
},
Zurücksetzen: Funktion (Daten) {
this.blank = [];
this.fixed = [];
this.cell = data.concat ();
für (var i = 0; i <81; i ++) {
if (this.bitCount (this.cell [i])! = 1) {
this.blank.push (i);
} anders {
this.fixed.push (i);
}
}
},
trialsolve: function () {
while (this.blank.length! = 0) {
if (this.settrialcell ()) {
this.asicSolve ();
} anders {
if (this.trials.length == 0) {
//console.log("kan nicht zurück nachwärts gehen! Vielleicht keine Lösung! ");
brechen;
} anders {
this.backforward ();
this.asicSolve ();
}
}
}
},
Play: function () {
console.log (this.showboard ());
var start = new Date (). getMilliseconds ();
if (! this.basicSolve ()) {
this.trialSolve ();
}
var end = new Date (). getMilliseconds ();
console.log (this.showboard ());
if (this.check ()) {
console.log ("[" + (end - start) + "ms ok!]");
} anders {
console.log ("[" + (end - start) + "ms kann es nicht lösen?");
}
// return this.showboard ();
},
getX: function (idx) {
var neighbors = new Array (24);
var box = neuarray (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;
für (var n = 0; n <9; n ++) {
if (n == c) fortsetzen;
Nachbarn [i ++] = r*9+n;
}
für (var n = 0; n <9; n ++) {
if (n == r) fortsetzen;
Nachbarn [i ++] = C+N*9;
}
für (var n = 0; n <9; n ++) {
var t = xs+box [n];
if (t == idx) weiter;
Nachbarn [i ++] = t;
}
Nachbarn zurückkehren;
},
CreateTrialPoint: Funktion (IDX, Val, Board) {
var tp = {};
tp.idx = idx;
tp.val = val;
tp.data = board.concat ();
return tp;
}
};
//Sudoku.init("000000500000008300600100000008009300000002070000000580000000200017090000060 ");
//Sudoku.init("53007000060019500980000608000600034008030017000200060600002800419005000080079 ");
Sudoku.init ("80000000000360000007009020005000700000045700000100030001000068008500010090000400");
Sudoku.play ();
Das obige ist der gesamte Code für die Implementierung der Sudoku -Lösung mit JavaScript. Ich hoffe, es gefällt Ihnen.