您好,欢迎访问一九零五行业门户网

javascript实现数独解法_javascript技巧

生生把写过的java版改成javascript版,第一次写,很不专业,见谅。唉,我是有多闲。
复制代码 代码如下:
var sudoku = {
    init: function (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 board = ;
        for (var i = 0; i             if (i % 9 == 0) {
                board = board.concat(\n);
            }
            board = board.concat([);
            for (var j = 0; 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                 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             if ((i >> j & 1) == 1)
                n++;
        }
        return n;
    },
    numberoftrailingzeros: function(i){
        var n = 0;
        for (var j = 0; 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                 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                 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             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 () {
        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                 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             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            if(n==c)continue;
            neighbors[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;
            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实现数独解法的全部代码了,希望大家能够喜欢。
其它类似信息

推荐信息