Shengsheng は、私が書いた Java バージョンを JavaScript バージョンに変更しました。初めて書いたもので、非常に専門的ではありません。ごめんなさい。ああ、なんて自由なんだろう。
var Sudoku = {
init: 関数 (str) {
this.blank = [];
this.fixed = [];
this.cell = [];
this.trials=[];
for (i = 0; i
var chr = str.charCodeAt(i);
if (chr == 48) {
this.cell[i] = 511;
this.blank.push(i);
} else {
this.cell[i] = 1
this.fixed.push(i);
}
}
}、
showBoard: function () {
var ボード = "";
for (var i = 0; i
if (i % 9 == 0) {
ボード = ボード.concat("n");
}
ボード = ボード.concat("[");
for (var j = 0; j
if ((this.cell[i] >> j & 1) == 1) {
ボード = ボード.concat(String.fromCharCode(j 49));
}
}
ボード = ボード.concat("]");
}
リターンボード;
}、
チェック: function () {
var チェックポイント = [0, 12, 24, 28, 40, 52, 56, 68, 80];
for (チェックポイントの変数 i) {
var r、b、c;
r = b = c = this.cell[チェックポイント[i]];
for (j = 0; 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 を返します;
}
}
true を返します;
}、
bitCount: 関数 (i) {
var n = 0;
for (var j = 0; j
if ((i >> j & 1) == 1)
ん;
}
n を返します;
}、
numberOfTrailingZeros: function(i){
var n = 0;
for (var j = 0; j
if ((i >> j & 1) ==0)
ん;
それ以外{
休憩;
}
}
n を返します。
}、
updateCandidates: function () {
for (this.fixed の変数 i) {
var opt = 0x1FF ^ this.cell[this.fixed[i]];
for (var j = 0; j
this.cell[this.getX(this.fixed[i])[j]] &= opt;
//!注意
if (this.cell[this.getX(this.fixed[i])[j]] == 0) {
//console.log("エラー-0 候補:" x[this.fixed[i]][j]);
false を返します;
}
}
}
true を返します;
}、
SeekUniqueCandidate: function () {
for (var bidx in this.blank) {
var row = 0、col = 0、box = 0;
for (i = 0; i
行 |= 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("修正:" this.blank[i] "=>" this.cell[this.blank[i]]);
//this.blank.splice(i, 1);//ループ内で削除するとバグが発生します
_del.push(i);
}
}
while(_del.length>0){
this.blank.splice(_del.pop(), 1);
}
}、
SeekMutexCell: function () {
var two = [];
for (this.blank の var n) {
if (this.bitCount(this.cell[this.blank[n]]) == 2) {
two.push(this.blank[n]);
}
}
for (var i = 0; i
for (var j = i 1; j
if (this.cell[two[i]] == this.cell[two[j]]) {
var opt = ~this.cell[two[i]];
if (parseInt(two[i] / 9) ==parseInt(two[j] / 9)) {
for (n = 0; n
this.cell[this.getX(two[i])[n]] &= opt;
}
}
if ((two[i] - two[j]) % 9 == 0) {
for (n = 8; n
this.cell[this.getX(two[i])[n]] &= opt;
}
}
if ((parseInt(two[i] / 27) * 3 parseInt(two[i] % 9 / 3)) == (parseInt(two[j] / 27) * 3 parseInt(two[j] % 9 / 3) ))) {
for (n = 16; n
this.cell[this.getX(two[i])[n]] &= opt;
}
}
this.cell[two[j]] = ~opt;
}
}
}
}、
BasicSolve: function () {
{
を実行します
if (!this.updateCandidates(this.fixed)) {
this.backForward();
}
this.seekUniqueCandidate();
this.seekMutexCell();
this.seekFilledable();
while (this.fixed.length != 0);
return 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]]);
varwaitingValue = 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]] = トライアル値;
this.trials.push(this.createTrialPoint(this.blank[i],waitingValue,this.cell));
true を返します;
}
}
false を返します;
}、
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));
}、
リセット: 関数(データ) {
this.blank=[];
this.fixed=[];
this.cell=data.concat();
for (var i = 0; i
if (this.bitCount(this.cell[i]) != 1) {
this.blank.push(i);
} else {
this.fixed.push(i);
}
}
}、
TrialSolve: function() {
while (this.blank.length!=0) {
if (this.setTrialCell()) {
this.basicSolve();
} else {
if (this.trials.length==0) {
//console.log("前に戻ることはできません!おそらく解決策はありません!");
休憩;
} else {
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("[" (終了 - 開始) "ms OK!]");
} else {
console.log("[" (終了 - 開始) "ms、解決できませんか?");
}
//return 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
if(n==c)Continue;
隣人[i ]=r*9 n;
}
for(var n=0;n
if(n==r)Continue;
neighbors[i ]=c n*9;
}
for(var n=0;n
var t=xs box[n];
if(t==idx)Continue;
隣人[i ]=t;
}
隣人を返します;
}、
createTrialPoint:function(idx, val, board) {
var tp = {};
tp.idx = idx;
tp.val = val;
tp.data = board.concat();
return tp;
}
};
//Sudoku.init("000000500000008300600100000080093000000000020700000000058000000000200017090000060");
//Sudoku.init("530070000600195000098000060800060003400803001700020006060000280000419005000080079");
Sudoku.init("800000000003600000070090200050007000000045700000100030001000068008500010090000400");
Sudoku.play();
以上は、JavaScript を使用して個数を個別に解読する方法に関するすべてのコードです。