나는 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);
}
}
},
쇼 보드 : function () {
var 보드 = "";
for (var i = 0; i <81; i ++) {
if (i % 9 == 0) {
보드 = 보드 .concat ( "/n");
}
보드 = 보드 .concat ( "[");
for (var j = 0; j <9; j ++) {
if ((this.cell [i] >> j & 1) == 1) {
보드 = Board.Concat (String.FromCharCode (j + 49));
}
}
보드 = 보드 .concat ( "]");
}
리턴 보드;
},
점검 : function () {
var 체크 포인트 = [0, 12, 24, 28, 40, 52, 56, 68, 80];
for (checkpoint의 var i) {
var r, b, c;
r = b = c = this.cell [Checkpoint [i]];
for (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) {
거짓을 반환합니다.
}
}
진실을 반환하십시오.
},
bitcount : function (i) {
var n = 0;
for (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 1)
n ++;
}
리턴 n;
},
NumberofTrailingzeros : 함수 (i) {
var n = 0;
for (var j = 0; j <9; j ++) {
if ((i >> j & 1) == 0)
n ++;
또 다른{
부서지다;
}
}
리턴 n;
},
UpdateCandidates : function () {
for (var i in this.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]);
거짓을 반환합니다.
}
}
}
진실을 반환하십시오.
},
SeekuuniqueCandidate : function () {
for (var bidx in this.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;
}
}
},
Seekfilledable : function () {
this.fixed = [];
var _del = [];
for (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;i] +"=>"+ this.cell])));
//this.blank.splice(i, 1); // 루프에서 삭제하면 버그가 발생합니다.
_del.push (i);
}
}
while (_del.length> 0) {
this.blank.splice (_del.pop (), 1);
}
},
SeekMutexCell : function () {
var two = [];
for (var n in this.blank) {
if (this.bitcount (this.cell [this.blank [n]] == 2) {
2.push (this.blank [n]);
}
}
for (var i = 0; i <two.length; i ++) {
for (var j = i+1; j <two.length; j ++) {
if (this.cell [2 [i]] == this.cell [2 [j]]) {
var opt = ~ this.cell [2 [i]];
if (parseint (2 [i] / 9) == parseint (2 [j] / 9)) {
for (n = 0; n <8; n ++) {
this.cell [this.getx (2 [i]) [n]] & = opt;
}
}
if ((2 [i] -2 [j]) % 9 == 0) {
for (n = 8; n <16; n ++) {
this.cell [this.getx (2 [i]) [n]] & = opt;
}
}
if ((parseint (2 [i] / 27) * 3 + parseint (2 [i] % 9 / 3)) == (parseint (2 [j] / 27) * 3 + parseint (2 [j] % 9 / 3)) {
for (n = 16; n <24; n ++) {
this.cell [this.getx (2 [i]) [n]] & = opt;
}
}
this.cell [두 [j]] = ~ opt;
}
}
}
},
기본 사항 : function () {
하다 {
if (! this.updatecandidates (this.fixed)) {
this.backforward ();
}
this.seekuniquecandidate ();
this.seekmutexcell ();
this.seekfilledable ();
} while (this.fixed.length! = 0);
this.blank.length == 0;
},
settrialCell : function () {
for (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]] ^ rivulalue;
//console.log ( "try : [" + this.blank [i] + "]->" + (this.numberftrailingzeros (rivilValue) + 1) + "#" + (this.numberoftrailingzeros (WaitingValue) + 1));
this.cell [this.blank [i]] = trialValue;
this.trials.push (this.createTrialpoint (this.blank [i], WaitingValue, 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.numberftrailingzeros (back.val) + 1));
},
재설정 : 함수 (데이터) {
this.blank = [];
this.fixed = [];
this.cell = data.concat ();
for (var i = 0; i <81; i ++) {
if (this.bitcount (this.cell [i])! = 1) {
this.blank.push (i);
} 또 다른 {
this.fixed.push (i);
}
}
},
rivinsolve : function () {
while (this.blank.length! = 0) {
if (this.settrialCell ()) {
this.basicsolve ();
} 또 다른 {
if (this.trials.length == 0) {
//console.log("can은 뒤로 돌아 가지 않아! 어쩌면 해결책이 없을 수도 있습니다! ");
부서지다;
} 또 다른 {
this.backforward ();
this.basicsolve ();
}
}
}
},
재생 : 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, 해결할 수 없습니까? ");
}
// reture this.showboard ();
},
getx : function (idx) {
var neighbors = 새로운 배열 (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 ++) {
(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];
(t == idx) 계속;
이웃 [i ++] = t;
}
이웃을 반환합니다.
},
CreaterTrialPoint : function (Idx, Val, Board) {
var tp = {};
tp.idx = idx;
tp.val = val;
tp.data = board.concat ();
반환 tp;
}
};
//sudoku.init("0000005000000000830060010000000000000000000000000000000000000000000000000200017090000060 ");
//sudoku.init("53007000060019500980000608000340080300170002000606000028004190050080079 ");
Sudoku.init ( "8000000000036000000007009020000000000000000000000000030001000068008500010090000400");
sudoku.play ();
위의 것은 JavaScript를 사용하여 스도쿠 솔루션을 구현하는 모든 코드입니다.