首頁 > web前端 > js教程 > javascript實作數獨解法_javascript技巧

javascript實作數獨解法_javascript技巧

WBOY
發布: 2016-05-16 16:09:39
原創
1311 人瀏覽過

生生把寫過的java版改成javascript版,第一次寫,很不專業,見諒。唉,我有多閒。

複製程式碼 程式碼如下:

var 數獨 = {
    初始化:函數(str){
        this.blank = [];
        this.fixed = [];
        this.cell = [];
        this.Trials=[];
        對於 (i = 0; i             var chr = str.charCodeAt(i);
            if (chr == 48) {
                this.cell[i] = 511;
                this.blank.push(i);
            }其他{
                this.cell[i] = 1                 this.fixed.push(i);
            }
        }
    },
    顯示板:函數 () {
        var 板 = "";
        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("]");
        }
        返回板;
    },
    檢查:函數 () {
        var 檢查點 = [0, 12, 24, 28, 40, 52, 56, 68, 80];
        for (檢查點中的 var i) {
            var r, b, c;
            r = b = c = this.cell[檢查點[i]];
            對於 (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) {
                回傳錯誤;
            }
        }
        返回真;
    },
    位計數:函數 (i) {
        var n = 0;
        for (var j = 0; j             if ((i >> j & 1) == 1)
                n ;
        }
        返回 n;
    },
    numberOfTrailingZeros: 函數(i){
        var n = 0;
        for (var j = 0; 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                 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]);
                    回傳錯誤;
                }
            }
        }
        返回真;
    },
    eekUniqueCandidate: 函數 () {
        for (var bidx in this.blank) {
            var 行 = 0,列 = 0,框 = 0;
            for (i = 0; i                 row |= this.cell[this.getX(this.blank[bidx])[i]];
                框 |= 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;
            }
        }
    },
    eekFilledable: 函數 () {
        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);//在循環中刪除它會導致bug
    _del.push(i);
            }
        }
  while(_del.length>0){
   this.blank.splice(_del.pop(), 1);
  }
    },
    eekMutexCell: 函數 () {
        var 二 = [];
        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)) {
                        對於 (n = 0; n                             this.cell[this.getX(two[i])[n]] &= opt;
                        }
                    }
                    if ((二[i] - 二[j]) % 9 == 0) {                       
                        對於 (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 ) ) )){
                        對於 (n = 16; n                             this.cell[this.getX(two[i])[n]] &= opt;
                        }
                    }
                    this.cell[two[j]] = ~opt;
                }
            }
        }
    },
    basicSolve: 函數 () {
        做{
            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 試驗值 = 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.trys.push(this.createTrialPoint(this.blank[i], waitingValue, this.cell));
                返回真;
            }
        }
        回傳錯誤;
    },
    實體:函數(){
        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);
            }其他{
                this.fixed.push(i);
            }
        }
    },
    TrialSolve: 函數() {
        while (this.blank.length!=0) {
            if (this.setTrialCell()) {
                this.basicSolve();
            }其他{
                if (this.Trials.length==0) {
                    //console.log("無法後退!可能沒有解決方法!");
                    休息;
                }其他{
                    this.backForward();
                    this.basicSolve();
                }
            }
        }
    },
    播放:函數(){
        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,無法解決?");
        }
  //返回this.showBoard();
    },
    getX:函數(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) 繼續;
            鄰居[i]=r*9 n;
        }
        for(var n=0;n             if(n==r)繼續;
            鄰居[i]=c n*9;
        }
        for(var n=0;n             var t=xs box[n];
            if(t==idx)繼續;
            鄰居[i]=t;
        }
          返回鄰居;
    },
 createTrialPoint:function(idx, val, 板) {
        var tp = {};
        tp.idx = idx;
        tp.val = val;
        tp.data = board.concat();
        返回 tp;
 }
};
//數獨.init("000000500000008300600100000080093000000000020700000000058000000000200017090000060 //數獨.init("530070000600195000098000060800060003400803001700020006060000280000419005000008 數獨.init("800000000003600000070090200050007000000045700000100030001000068008500010090000400"); 數獨.play();



以上就是關於使用javascript實作數獨解法的全部程式碼了,希望大家能夠喜歡。
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板