Я изменил версию Java, которую я написал на версию JavaScript. Увы, как я свободен.
Кода -копия выглядит следующим образом:
var sudoku = {
init: function (str) {
this.blank = [];
this.fixed = [];
this.cell = [];
this.trials = [];
for (i = 0; i <81; i ++) {
var chr = str.charcodeat (i);
if (chr == 48) {
this.cell [i] = 511;
this.blank.push (i);
} еще {
this.cell [i] = 1 << chr - 49;
this.fixed.push (i);
}
}
},
Showboard: function () {
var board = "";
для (var i = 0; i <81; i ++) {
if (i % 9 == 0) {
poard = poard.concat ("/n");
}
poard = poard.concat ("[");
for (var j = 0; j <9; j ++) {
if ((this.cell [i] >> j & 1) == 1) {
poard = poard.concat (string.fromCharcode (j + 49));
}
}
poard = poard.concat ("]");
}
возвратный доска;
},
Проверка: function () {
VAR Checkpoint = [0, 12, 24, 28, 40, 52, 56, 68, 80];
для (var i в контрольной точке) {
var r, b, c;
r = b = c = this.cell [контрольная точка [i]];
for (j = 0; j <8; j ++) {
c ^= this.cell [this.getx (контрольная точка [i]) [j]];
b ^= this.cell [this.getx (контрольная точка [i]) [8 + j]];
r ^= this.cell [this.getx (контрольная точка [i]) [16 + j]];
}
if ((r & b & c)! = 0x1ff) {
вернуть ложь;
}
}
вернуть истину;
},
BitCount: function (i) {
var n = 0;
for (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 1)
n ++;
}
возврат n;
},
numberoftrailingzeros: function (i) {
var n = 0;
for (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 0)
n ++;
еще{
перерыв;
}
}
возврат n;
},
UpdateCandidates: function () {
для (var i в этом. fixed) {
var opt = 0x1ff ^ this.cell [this.fixed [i]];
for (var j = 0; j <24; j ++) {
this.cell [this.getx (this.fixed [i]) [j]] & = opt;
//!Уведомление
if (this.cell [this.getx (this.fixed [i]) [j]] == 0) {
//console.log("error-0 кандидат: "+x [this.fixed [i]] [j]);
вернуть ложь;
}
}
}
вернуть истину;
},
SeekUniqueCandidate: function () {
для (var bidx в этом.blank) {
var row = 0, col = 0, box = 0;
for (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;
продолжать;
}
if (this.bitcount (this.cell [this.blank [bidx]] & ~ col) == 1) {
this.cell [this.blank [bidx]] & = ~ col;
продолжать;
}
if (this.bitcount (this.cell [this.blank [bidx]] & ~ box) == 1) {
this.cell [this.blank [bidx]] & = ~ box;
}
}
},
seekfillebleble: function () {
this.fixed = [];
var _del = [];
для (var i в этом.blank) {
if (this.bitcount (this.cell [this.blank [i]]) == 1) {
this.fixed.push (this.blank [i]);
//console.log("fixed:"+This.blank®+"=>"+This.cell[
//This.blank.splice(i, 1); // Удалить его в цикле вызовет ошибку
_del.push (i);
}
}
while (_del.length> 0) {
this.blank.splice (_del.pop (), 1);
}
},
seekmutexcell: function () {
var two = [];
для (var n в этом.blank) {
if (this.bitcount (this.cell [this.blank [n]]) == 2) {
Two.push (this.blank [n]);
}
}
for (var i = 0; i <two.length; i ++) {
for (var j = i+1; j <two.length; j ++) {
if (this.cell [два [i]] == this.cell [два [j]]) {
var opt = ~ this.cell [два [i]];
if (parseint (два [i] / 9) == parseint (два [j] / 9)) {
for (n = 0; n <8; n ++) {
this.cell [this.getx (два [i]) [n]] & = opt;
}
}
if ((два [i] - два [j]) % 9 == 0) {
для (n = 8; n <16; n ++) {
this.cell [this.getx (два [i]) [n]] & = opt;
}
}
if ((parseint (два [i] / 27) * 3 + parseint (два [i] % 9/3)) == (parseint (два [j] / 27) * 3 + parseint (два [j] % 9/3))) {
для (n = 16; n <24; n ++) {
this.cell [this.getx (два [i]) [n]] & = opt;
}
}
this.cell [два [j]] = ~ opt;
}
}
}
},
basicsulv: function () {
делать {
if (! this.updateCandidates (this.fixed)) {
this.backforward ();
}
this.seekuniqueCandidate ();
this.seekmutexcell ();
this.seekfillable ();
} while (this.fixed.length! = 0);
вернуть это.blank.length == 0;
},
settrialcell: function () {
для (var i в этом.blank) {
if (this.bitcount (this.cell [this.blank [i]]) == 2) {
var tarevalue = 1 << this.numberoftrailingzeros (this.cell [this.blank [i]]);
var whiveValue = this.cell [this.blank [i]] ^ trialValue;
//console.log ("try: [" + this.blank [i] + "]->" + (this.numberoftrailingzeros (trialvalue) + 1) + "#" + (this.numberoftrailingzeros (watchvalue) + 1));
this.cell [this.blank [i]] = trialvalue;
this.trials.push (this.createtrialpoint (this.blank [i], whietvalue, this.cell));
вернуть истину;
}
}
вернуть ложь;
},
Backforward: function () {
if (this.trials.length == 0) {
console.log («Может быть, нет решения!»);
возвращаться;
}
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));
},
сбросить: function (data) {
this.blank = [];
this.fixed = [];
this.cell = data.concat ();
для (var i = 0; i <81; i ++) {
if (this.bitcount (this.cell [i])! = 1) {
this.blank.push (i);
} еще {
this.fixed.push (i);
}
}
},
trialsolve: function () {
while (this.blank.length! = 0) {
if (this.settrialcell ()) {
this.basicsolve ();
} еще {
if (this.trials.length == 0) {
//console.log("can не пойти за задний план! Может быть, нет решения! »);
перерыв;
} еще {
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.check ()) {
console.log ("[" + (end - start) + "ms ok!]");
} еще {
console.log ("[" + (end - start) + "ms, не может его решить?");
}
// вернуть это.showboard ();
},
getX: function (idx) {
var youndes = new Array (24);
var box = новый массив (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;
for (var n = 0; n <9; n ++) {
if (n == c) продолжить;
Соседи [i ++] = r*9+n;
}
for (var n = 0; n <9; n ++) {
if (n == r) продолжить;
Соседи [i ++] = C+N*9;
}
for (var n = 0; n <9; n ++) {
var t = xs+box [n];
if (t == idx) продолжить;
Соседи [i ++] = t;
}
вернуть соседей;
},
CreateTrialPoint: function (idx, val, poard) {
var tp = {};
tp.idx = idx;
tp.val = val;
tp.data = poard.concat ();
вернуть TP;
}
};
//Sudoku.init("000000500000008300600100000008009300000000020700000000058000000000200017090000060 ");
//Sudoku.init("530070000600195000098000060800060003400803001700020006060000280000419005000080079 ");
Sudoku.init ("80000000000360000007009020005000700000045700000100030001000068008500010090000400");
Sudoku.play ();
Выше приведено код в реализации решения Sudoku с использованием JavaScript, я надеюсь, что вам понравится.