Cambié la versión Java que escribí a una versión de JavaScript. La primera vez que lo escribí, fue muy poco profesional, me perdono. Por desgracia, cómo soy libre.
La copia del código es la siguiente:
var sudoku = {
init: function (str) {
this.blank = [];
this.fixed = [];
this.cell = [];
this.trials = [];
para (i = 0; i <81; i ++) {
var chr = str.charcodeat (i);
if (chr == 48) {
this.cell [i] = 511;
this.blank.push (i);
} demás {
this.cell [i] = 1 << chr - 49;
this.fixed.push (i);
}
}
},
showboard: function () {
Var Board = "";
para (var i = 0; i <81; i ++) {
if (i % 9 == 0) {
board = board.concat ("/n");
}
board = board.concat ("[");
para (var j = 0; j <9; j ++) {
if ((this.cell [i] >> j & 1) == 1) {
board = board.concat (String.FromCharCode (j + 49));
}
}
board = board.concat ("]");
}
Junta de regreso;
},
comprobar: function () {
Var CheckPoint = [0, 12, 24, 28, 40, 52, 56, 68, 80];
para (var i en punto de control) {
var r, b, c;
r = b = c = this.cell [punto de control [i]];
para (j = 0; j <8; j ++) {
c ^= this.cell [this.getx (punto de control [i]) [j]];
b ^= this.cell [this.getx (punto de control [i]) [8 + j]];
r ^= this.cell [this.getx (punto de control [i]) [16 + j]];
}
if ((r & b & c)! = 0x1ff) {
devolver falso;
}
}
devolver verdadero;
},
bitCount: function (i) {
var n = 0;
para (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 1)
n ++;
}
regresar n;
},
NumberOftrailingzeros: function (i) {
var n = 0;
para (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 0)
n ++;
demás{
romper;
}
}
regresar n;
},
updateCandidates: function () {
para (var i en this.fixed) {
var opt = 0x1ff ^ this.cell [this.fixed [i]];
para (var j = 0; j <24; j ++) {
this.cell [this.getx (this.fixed [i]) [j]] & = opt;
//!Aviso
if (this.cell [this.getx (this.fixed [i]) [j]] == 0) {
//console.log("error-0 candidato: "+x [this.fixed [i]] [j]);
devolver falso;
}
}
}
devolver verdadero;
},
SeekUniqueCandidate: function () {
para (var bidx en this.blank) {
var fila = 0, col = 0, box = 0;
para (i = 0; i <8; i ++) {
fila | = 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;
continuar;
}
if (this.bitcount (this.cell [this.blank [bidx]] & ~ col) == 1) {
this.Cell [this.blank [bidx]] & = ~ col;
continuar;
}
if (this.bitcount (this.cell [this.blank [bidx]] & ~ box) == 1) {
this.cell [this.blank [bidx]] & = ~ box;
}
}
},
SeekFilledable: function () {
this.fixed = [];
var _del = [];
para (var i en this.blank) {
if (this.bitcount (this.cell [this.blank [i]]) == 1) {
this.fixed.push (this.blank [i]);
//console.log("fixed:"+THIS.BlankimaefontNo+"=>"+THIS.CELL[THIS.BLANK[I]]);
//this.blank.splice(i, 1); // para eliminarlo en el bucle causaría un error
_del.push (i);
}
}
while (_del.length> 0) {
this.blank.splice (_del.pop (), 1);
}
},
Seekmutexcell: functer () {
var dos = [];
para (var n en this.blank) {
if (this.bitcount (this.cell [this.blank [n]]) == 2) {
two.push (this.blank [n]);
}
}
para (var i = 0; i <two.length; i ++) {
para (var j = i+1; j <two.length; j ++) {
if (this.cell [dos [i]] == this.cell [dos [j]]) {
var opt = ~ this.cell [dos [i]];
if (parseInt (dos [i] / 9) == parseint (dos [j] / 9)) {
para (n = 0; n <8; n ++) {
this.Cell [this.getx (dos [i]) [n]] & = opt;
}
}
if ((dos [i] - dos [j]) % 9 == 0) {
para (n = 8; n <16; n ++) {
this.Cell [this.getx (dos [i]) [n]] & = opt;
}
}
if ((parseInt (dos [i] / 27) * 3 + parseint (dos [i] % 9 /3)) == (parseInt (dos [j] / 27) * 3 + parseint (dos [j] % 9 /3)) {
para (n = 16; n <24; n ++) {
this.Cell [this.getx (dos [i]) [n]] & = opt;
}
}
this.cell [dos [j]] = ~ opt;
}
}
}
},
Basicsolve: function () {
hacer {
if (! this.updateCandidates (this.fixed)) {
this.backforward ();
}
this.seekuniquecandidate ();
this.seekmutexcell ();
this.seekFilledable ();
} while (this.fixed.length! = 0);
devolver esto.blank.length == 0;
},
setTrialCell: functer () {
para (var i en 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 ("intente: [" + this.blank [i] + "]->" + (this.numberOfTrailingros (TrialValue) + 1) + "#" + (this.numberOfTrailingros (WaitingValue) + 1));
this.cell [this.blank [i]] = spryleValue;
this.trials.push (this.createTrialpoint (this.blank [i], WaitingValue, this.Cell));
devolver verdadero;
}
}
devolver falso;
},
Backforward: function () {
if (this.trials.length == 0) {
console.log ("¡Quizás no hay solución!");
devolver;
}
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.numberoftrailingros (back.val) + 1));
},
Restablecer: function (data) {
this.blank = [];
this.fixed = [];
this.cell = data.concat ();
para (var i = 0; i <81; i ++) {
if (this.bitCount (this.cell [i])! = 1) {
this.blank.push (i);
} demás {
this.fixed.push (i);
}
}
},
TrialSolve: function () {
while (this.blank.length! = 0) {
if (this.setTrialCell ()) {
this.basicsolve ();
} demás {
if (this.trials.length == 0) {
//console.log("Can't Go Backforward! ¡Tal vez no hay solución! ");
romper;
} demás {
this.backforward ();
this.basicsolve ();
}
}
}
},
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.eck ()) {
console.log ("[" + (end - inicio) + "MS OK!]");
} demás {
console.log ("[" + (end - inicio) + "MS, ¿no puede resolverlo?");
}
// devuelve this.showboard ();
},
getx: function (idx) {
vecinos var = nueva matriz (24);
Var Box = nueva matriz (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;
para (var n = 0; n <9; n ++) {
if (n == c) continuar;
vecinos [i ++] = r*9+n;
}
para (var n = 0; n <9; n ++) {
if (n == r) continuar;
vecinos [i ++] = c+n*9;
}
para (var n = 0; n <9; n ++) {
var t = xs+box [n];
if (t == idx) continuar;
vecinos [i ++] = t;
}
Vecinos de regreso;
},
createtrialpoint: function (idx, val, tablero) {
var tp = {};
tp.idx = idx;
tp.val = val;
tp.data = board.concat ();
devolver tp;
}
};
//Sudoku.init("00000050000000830060010000000800930000000002070000000000058000000000200017090000060 ");
//Sudoku.init("5300700006001950000980000608000600034008030017000200060600002800419005000080079 ");
Sudoku.init ("80000000000360000007009020005000700000045700000100030001000068008500010090000400");
Sudoku.play ();
Lo anterior es todo el código en la implementación de la solución Sudoku usando JavaScript, espero que les guste.