javascript实现数独解法
作者:hebedich 时间:2023-10-17 17:18:28
生生把写过的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);
} else {
this.cell[i] = 1 << chr - 49;
this.fixed.push(i);
}
}
},
showBoard: function () {
var board = "";
for (var i = 0; i < 81; i++) {
if (i % 9 == 0) {
board = board.concat("\n");
}
board = board.concat("[");
for (var j = 0; j < 9; j++) {
if ((this.cell[i] >> j & 1) == 1) {
board = board.concat(String.fromCharCode(j + 49));
}
}
board = board.concat("]");
}
return board;
},
check: function () {
var checkpoint = [0, 12, 24, 28, 40, 52, 56, 68, 80];
for (var i in checkpoint) {
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) {
return false;
}
}
return true;
},
bitCount: function (i) {
var n = 0;
for (var j = 0; j < 9; j++) {
if ((i >> j & 1) == 1)
n++;
}
return n;
},
numberOfTrailingZeros: function(i){
var n = 0;
for (var j = 0; j < 9; j++) {
if ((i >> j & 1) ==0)
n++;
else{
break;
}
}
return 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;
//!notice
if (this.cell[this.getX(this.fixed[i])[j]] == 0) {
//console.log("Error-0 candidate:"+x[this.fixed[i]][j]);
return false;
}
}
}
return true;
},
seekUniqueCandidate: 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;
continue;
}
if (this.bitCount(this.cell[this.blank[bidx]] & ~col) == 1) {
this.cell[this.blank[bidx]] &= ~col;
continue;
}
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[i]]);
//this.blank.splice(i, 1);//to delete it in the loop would cause bug
_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) {
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[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 < 8; n++) {
this.cell[this.getX(two[i])[n]] &= opt;
}
}
if ((two[i] - two[j]) % 9 == 0) {
for (n = 8; n < 16; 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 < 24; n++) {
this.cell[this.getX(two[i])[n]] &= opt;
}
}
this.cell[two[j]] = ~opt;
}
}
}
},
basicSolve: function () {
do {
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]]);
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));
return true;
}
}
return false;
},
backForward: function() {
if (this.trials.length==0) {
console.log("Maybe no solution!");
return;
}
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));
},
reset: function(data) {
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);
} 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("Can't go backforward! Maybe no solution!");
break;
} 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("[" + (end - start) + "ms OK!]");
} else {
console.log("[" + (end - start) + "ms, cannot solve it?");
}
//return this.showBoard();
},
getX:function(idx){
var neighbors=new Array(24);
var box=new Array(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)continue;
neighbors[i++]=r*9+n;
}
for(var n=0;n<9;n++){
if(n==r)continue;
neighbors[i++]=c+n*9;
}
for(var n=0;n<9;n++){
var t=xs+box[n];
if(t==idx)continue;
neighbors[i++]=t;
}
return neighbors;
},
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,数独解法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp 解析一个xml文件的公用函数集合
2008-02-29 13:40:00
Python数据结构栈实现进制转换简单示例
2022-10-21 13:48:21
浅谈discuz密码加密的方式
2024-05-02 17:07:51
Python爬虫实现爬取百度百科词条功能实例
2021-01-09 06:23:30
![](https://img.aspxhome.com/file/2023/1/65421_0s.png)
Webpack中publicPath使用详解
2024-04-28 10:21:11
![](https://img.aspxhome.com/file/2023/1/132681_0s.jpg)
利用python在大量数据文件下删除某一行的例子
2023-08-24 09:15:22
MySQL函数CONCAT、CONCAT_WS、GROUP_CONCAT用法详解
2024-01-22 08:57:56
![](https://img.aspxhome.com/file/2023/1/101541_0s.png)
TensorFlow和Numpy矩阵操作中axis理解及axis=-1的解释
2021-02-14 05:59:09
![](https://img.aspxhome.com/file/2023/3/91933_0s.png)
python 列表推导式使用详解
2021-10-13 01:53:28
wxPython的安装与使用教程
2023-01-07 17:46:33
![](https://img.aspxhome.com/file/2023/0/92430_0s.png)
ASP进阶学习之认识数学函数
2007-10-08 13:15:00
python 串口读取+存储+输出处理实例
2023-03-30 01:46:26
Python pkg_resources模块动态加载插件实例分析
2023-10-31 16:48:39
![](https://img.aspxhome.com/file/2023/4/72834_0s.png)
[译]图片优化 第五章:AlphaImageLoader
2010-08-29 18:39:00
![](https://img.aspxhome.com/file/UploadPic/20108/29/png-transparency-64s.png)
细化解析:MySQL 数据库中对XA事务的限制
2009-01-14 11:59:00
JavaScript DOM节点操作方法总结
2024-04-16 09:24:36
![](https://img.aspxhome.com/file/2023/0/136690_0s.png)
python matplotlib绘画十一种常见数据分析图
2022-09-02 04:50:41
![](https://img.aspxhome.com/file/2023/0/65850_0s.png)
python实现二级登陆菜单及安装过程
2023-09-16 16:05:44
![](https://img.aspxhome.com/file/2023/5/65955_0s.jpg)
详解PyTorch批训练及优化器比较
2023-08-23 11:52:54
![](https://img.aspxhome.com/file/2023/1/95481_0s.png)
时间序列分析之ARIMA模型预测餐厅销量
2023-07-09 13:53:59
![](https://img.aspxhome.com/file/2023/2/114622_0s.png)