最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript實現(xiàn)數(shù)獨解法

 更新時間:2015年03月14日 09:34:31   投稿:hebedich  
數(shù)獨(すうどく,Sūdoku)是一種運用紙、筆進行演算的邏輯游戲。玩家需要根據(jù)9×9盤面上的已知數(shù)字,推理出所有剩余空格的數(shù)字,并滿足每一行、每一列、每一個粗線宮內(nèi)的數(shù)字均含1-9,不重復(fù)。

生生把寫過的java版改成javascript版,第一次寫,很不專業(yè),見諒。唉,我是有多閑。

復(fù)制代碼 代碼如下:

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();

以上就是關(guān)于使用javascript實現(xiàn)數(shù)獨解法的全部代碼了,希望大家能夠喜歡。

相關(guān)文章

  • JS模擬百度搜索框和選項卡的實現(xiàn)

    JS模擬百度搜索框和選項卡的實現(xiàn)

    本文主要介紹了JS模擬百度搜索框和選項卡的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • js 實現(xiàn)省市區(qū)三級聯(lián)動菜單效果

    js 實現(xiàn)省市區(qū)三級聯(lián)動菜單效果

    本文主要分享了js實現(xiàn)省市區(qū)三級聯(lián)動菜單效果的示例代碼。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • javascript 強制刷新頁面的實現(xiàn)代碼

    javascript 強制刷新頁面的實現(xiàn)代碼

    javascript 強制刷新頁面的代碼,大家經(jīng)常能用的到。
    2009-12-12
  • js獲取css的各種樣式并且設(shè)置他們的方法

    js獲取css的各種樣式并且設(shè)置他們的方法

    下面小編就為大家?guī)硪黄猨s獲取css的各種樣式并且設(shè)置他們的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • uniapp實現(xiàn)滾動觸底加載項目實戰(zhàn)

    uniapp實現(xiàn)滾動觸底加載項目實戰(zhàn)

    這篇文章主要為大家介紹了uniapp實現(xiàn)滾動觸底加載項目實戰(zhàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • js/jQuery簡單實現(xiàn)選項卡功能

    js/jQuery簡單實現(xiàn)選項卡功能

    本篇文章主要是對js/jQuery簡單實現(xiàn)選項卡功能的示例代碼進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • 前端BOM操作常用命令詳解及代碼案例

    前端BOM操作常用命令詳解及代碼案例

    瀏覽器對象模型(BOM)是瀏覽器提供的JavaScript操作瀏覽器的API,提供了與網(wǎng)頁無關(guān)的瀏覽器功能對象,這篇文章主要給大家介紹了關(guān)于前端BOM操作常用命令詳解及代碼案例的相關(guān)資料,需要的朋友可以參考下
    2024-10-10
  • Webpack實戰(zhàn)加載SVG的方法

    Webpack實戰(zhàn)加載SVG的方法

    本篇文章主要介紹了Webpack實戰(zhàn)加載SVG的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • 針對BootStrap中tabs控件的美化和完善(推薦)

    針對BootStrap中tabs控件的美化和完善(推薦)

    這篇文章主要介紹了針對BootStrap中tabs控件的美化和完善的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧
    2016-07-07
  • JavaScript遍歷數(shù)組的三種方法map、forEach與filter實例詳解

    JavaScript遍歷數(shù)組的三種方法map、forEach與filter實例詳解

    這篇文章主要介紹了JavaScript遍歷數(shù)組的三種方法map、forEach與filter,結(jié)合實例形式詳細分析了javascript針對數(shù)組遍歷的map、forEach與filter三種方法相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2019-02-02

最新評論

东丰县| 亳州市| 孟津县| 太仓市| 普陀区| 新津县| 高清| 宣城市| 郴州市| 习水县| 开远市| 永吉县| 沂南县| 利川市| 南乐县| 建瓯市| 临高县| 乳山市| 台州市| 忻州市| 工布江达县| 阿拉善左旗| 临澧县| 大悟县| 饶阳县| 临沧市| 金溪县| 福鼎市| 东宁县| 永昌县| 广宁县| 友谊县| 万山特区| 江门市| 西丰县| 合山市| 潞城市| 思南县| 景泰县| 杨浦区| 全南县|