奥门巴黎人手机网址【app】

【科技世界】javascript达成数独解法,javascript实现解法

2019-09-06 10:31·巴黎人手机登录

生生把写过的java版改成javascript版,第三遍写,很不标准,见谅。唉,小编是有多闲。

javascript兑现数独解法,javascript达成解法

生生把写过的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完毕数独解法的任何代码了,希望我们能够欣赏。

生生把写过的java版改成javascript版,第二次写,很不标准,见谅。唉,作者是有多闲。 复制代码 代码...

js 落成win7职分栏拖动作效果果

在有些时刻, 笔者认知了二个朋友.

 

这个人在本身的煽动下, 踏上了js的不归路.

 

前几天她问作者, Win7职责栏拖动效果怎么实现.

 

本身随口就跟他说, 那简单的一逼.

 

在自己一晚上的折腾之后, 一份潦草的代码总算实现了作用.

 

PS: 小编是搞C++的, js略懂一二..

 

 

 

 

 

源码

 

话相当少说, 上源码.

 

复制代码

  1 //    常量

  2 var CELL_WIDTH    = 100;

  3 var CELL_HEIGHT = 50;

  4 

  5 var Utils = {

  6     pixelToInt: function(str)

  7     {

  8         return parseInt( str.replace("px", "") );

  9     }, 

 10     getTagLeft: function($tag)

 11     {

 12         return this.pixelToInt( $tag.css("left") );

 13     },

 14     getTagTop: function($tag)

 15     {

 16         return this.pixelToInt( $tag.css("top") );

 17     },

 18     getTagWidth: function($tag)

 19     {

 20         return this.pixelToInt( $tag.css("width") );

 21     },

 22     getTagHeight: function($tag)

 23     {

 24         return this.pixelToInt( $tag.css("height") );

 25     },

 26     setTagLeft: function($tag, x)

 27     {

 28         $tag.css("left", x + "px");

 29     },

 30     setTagTop: function($tag, y)

 31     {

 32         $tag.css("top", y + "px");

 33     },

 34     setTagWidth: function($tag, width)

 35     {

 36         $tag.css("width", width + "px");

 37     },

 38     setTagHeight: function($tag, height)

 39     {

 40         $tag.css("left", height + "px");

 41     },

 42     swapNode: function(ary, idx1, idx2)

 43     {

 44         var t = ary[idx1];

 45         ary[idx1] = ary[idx2];

 46         ary[idx2] = t;

 47     }

 48 };

 49 

 50 function Taskbar()

 51 {

 52     this._cells = [];

 53     this._frameTag = null;

 54     this._cellWidth = 0;

 55     this._cellHeight = 0;

 56     this._selNode = null;

 57     this._selIndex = -1;

 58 

 59     this._swapQueue = [];

 60     //    考虑优化.

 61     this._offsetPoint = {"x": 0, "y": 0};

 62 }

 63 

 64 Taskbar.prototype.getTag = function()

 65 {

 66     return this._frameTag;

 67 }

 68 

 69 Taskbar.prototype.init = function(x, y, width, height, rgb)

 70 {

 71     this._frameTag = $("<div></div>");

 72     this.setPosition(x, y);

 73     this.setContentSize(width, height);

 74     this.setBackgroundColor(rgb);

 75 

 76     var self = this;

 77     this._frameTag.bind("mousedown", {"bar": self}, this.mouseDown);

 78     this._frameTag.bind("mouseup", {"bar": self}, this.mouseUp);

 79     this._frameTag.bind("mousemove", {"bar": self}, this.mouseMove);

 80     // this._frameTag.bind("mouseout", {"bar": self}, this.mouseOut);

 81 }

 82 

 83 Taskbar.prototype.setPosition = function(x, y)

 84 {

 85     this._frameTag.css("position", "absolute");

 86     this._frameTag.css("left", x + "px");

 87     this._frameTag.css("top", y + "px");

 88 }

 89 

 90 Taskbar.prototype.setContentSize = function(width, height)

 91 {

 92     this._frameTag.css("width", width + "px");

 93     this._frameTag.css("height", height + "px");

 94 }

 95 

 96 Taskbar.prototype.setBackgroundColor = function(rgb)

 97 {

 98     //    rgb => "rgb(0, 0, 0)".

 99     this._frameTag.css("background", rgb);

100 }

101 

102 Taskbar.prototype.appendNode = function($node)

103 {

104     var frameWidth = Utils.getTagWidth( this._frameTag );

105     var frameHeight = Utils.getTagHeight( this._frameTag );

106     var length = this._cells.length + 1;

107     this._cellWidth = frameWidth / length;

108     this._cellHeight = frameHeight;

109     this._cells.push($node);

110     $node.appendTo( this._frameTag );

111 

112     for ( var i = 0; i != length; ++i )

113     {

114         Utils.setTagLeft( this._cells[i], i * this._cellWidth );

115         Utils.setTagWidth( this._cells[i], this._cellWidth);

116     }    

117 }

118 

119 Taskbar.prototype.mouseDown = function(e)

120 {

121     var bar = e.data["bar"];

122 

123     if ( bar._selNode )

124     {

125         return ;

126     }

127 

128     var index = bar.hitTest(e.clientX, e.clientY);

129     if ( !bar.isInvalidIndex(index) )

130     {

131         //    激活.

132         bar._selIndex = index;

133         bar._selNode = bar._cells[ index ];

134         bar._selNode.css("z-index", 99);

135         bar._cells[ index ] = null;

136 

137         //    保存偏移量, 保持鼠标拖动.

138         var point = bar.converPoint(e.clientX, e.clientY);

139         bar._offsetPoint.x = point.x - index * bar._cellWidth;

140         bar._offsetPoint.y = point.y;

141         console.log("down");

142     }

143 

144 }

145 

146 Taskbar.prototype.mouseUp = function(e)

147 {

148     var bar = e.data["bar"];

149 

150     if ( bar._selNode )

151     {

152         //    插手交流.

153         bar.appendSwap(bar._selNode, bar._selIndex);

154 

155         //    鼠标抬起后, 把选中的节点重新设置.

156         // bar._cells[ bar._selIndex ] = bar._selNode;

157         bar._cells[ bar._selIndex ].css("z-index", 1);

158         bar._selIndex = -1;

159         bar._selNode = null;

160         console.log("up");

161     }

162 }

163 

164 Taskbar.prototype.mouseOut = function(e)

165 {

166     var bar = e.data["bar"];

167     bar.mouseUp(e);

168     console.log("mouseout");

169 }

170 

171 Taskbar.prototype.mouseMove = function(e)

172 {

173     var bar = e.data["bar"];

174     if ( bar._selNode )

175     {

176         var point = bar.converPoint(e.clientX, e.clientY);

177         var moveX = point.x - bar._offsetPoint.x;

178 

179         //    防止地方溢出.

180         bar.noOverflow( bar._selNode, moveX );

181 

182         //    挤开旁边的 float block.

183         var curX = Utils.getTagLeft(bar._selNode),

184             width = Utils.getTagWidth(bar._selNode),

185             testX = curX + width / 2,

186             hitIndex = bar.hitTest(testX, 0);

187         if ( bar._selIndex != hitIndex )

188         {

189             bar.appendSwap(bar._cells[hitIndex], bar._selIndex);

190             bar._selIndex = hitIndex;

191         }

192     }

193 }

194 

195 Taskbar.prototype.appendSwap = function($node, index)

196 {

197     this._cells[index] = $node;

198 

199     this._swapQueue.push({"node": $node, "index": index});

200     this.resetNode();

201 }

202 

203 Taskbar.prototype.noOverflow = function($node, moveX)

204 {

205     var width = Utils.getTagWidth( $node ),

206         frameWidth = Utils.getTagWidth( this._frameTag );

207 

208     if (moveX < 0)

209         moveX = 0;

210     else if ( moveX + width > frameWidth )

211         moveX = frameWidth - width;

212 

213     Utils.setTagLeft( $node, moveX );

214 }

215 

216 Taskbar.prototype.resetNode = function()

217 {

218     var self = this;

219     var call = function($node, index)

220     {

221         var oldX = Utils.getTagLeft($node),

222             newX = index * self._cellWidth,

223             diff = newX - oldX,

224             stepCount = 10,

225             step = diff / stepCount,

226             curX = oldX;

227         (

228             function call()

229             {

230                 if ( stepCount != 0 )

231                 {

232                     curX += step;

233                     Utils.setTagLeft($node, curX);

234                     setTimeout(call, 10);

235                 }

236                 else

237                 {

238                     $node.css("z-index", 0);

239                     Utils.setTagLeft($node, newX);

240                 }

241                 --stepCount;

242             }

243         )();

244     };

245 

246     for (var i in this._swapQueue)

247     {

248         call(this._swapQueue[i].node, this._swapQueue[i].index);

249     }

250     this._swapQueue = [];

251 }

252 

253 Taskbar.prototype.hitTest = function(x, y)

254 {

255     //    y参数完全部是个老抽.

256     var point = this.converPoint(x, y);

257     return parseInt(point.x / this._cellWidth);

258 }

259 

260 Taskbar.prototype.converPoint = function(x, y)

261 {

262     var frameX = Utils.getTagLeft( this._frameTag );

263         frameY = Utils.getTagTop( this._frameTag );

264     return {

265         "x": x -= frameX,

266         "y": y -= frameY

267     };

268 }

269 

270 Taskbar.prototype.isInvalidIndex = function(index)

271 {

272     return index < 0 || index >= this._cells.length;

273 }

274 

275 function init()

276 {

277     var getCell = function(cls, left, top, name)

278     {

279         return $(

280             "<div class='_cls' name='_name' style='left: _leftpx; top: _toppx; width: _widthpx; height: _heightpx;'></div>"

281             .replace("_cls", cls)

282             .replace("_left", left)

283             .replace("_top", top)

284             .replace("_name", name)

285             .replace("_width", CELL_WIDTH)

286             .replace("_height", CELL_HEIGHT) );

287     };

288 

289      for (var i = 0; i != 5; ++i)

290      {

291          var taskbar = new Taskbar();

292          taskbar.init(0, i * 60, 500, 50, "rgb(0, 0, 0)");

293          taskbar.getTag().appendTo( $("body") );

294          for (var j = 0; j != i + 5; ++j)

295          {

296              taskbar.appendNode( getCell("cell", 0, 0, 0) );

297          }

298      }

299 }

300 

301 $(document).ready(init);

复制代码

其一思路其实很简单.

 

创建贰个Taskbar对象, 这一个目的设定好坐标, 尺寸, 背景观.

 

随后往这些目的appendChild, 子节点会自行适配大小.

 

 

 

笔者们用三个 作业队列 来保存须要活动的职分.

 

以此行列保存必要被挪动的节点, 和消沉到哪些岗位上.

 

接着会接触二个交流的动作, 这几个动作是绵延的, 因而能够观看节点平滑移动.

 

 

 

在咱们down下操作之后, 被down下的十二分节点地方设置null.

 

随即大家用一个 _selNode 保存这一个节点.

 

同时用 _selIndex 保存这几个节点本应当属于的地点.(那句话很难形容, 可是自身想不出怎么说!)

 

随着在move操作下, 判别那个 _selNode 是或不是"越线", 一旦越线则push多少个置换作业.

 

跟着就疑似上边所说, 触发调换动作.

 

 

 

在up操作触发之后, 只必要把 _selNode和_selIndex push到作业队列就能够.

 

 

 

因为兼具的mouse响应都在背景的div里, 由此鼠标跨越范围则不会响应..

 

诸如, 作者down之后, 鼠标移出范围再up, 此时的up将不会被响应..

 

小编在down中加了一条推断来消除此bug..

 

在超越范围up, 回到范围之后, 还是是down状态.

 

 

 

html 文件内容

 

复制代码

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript" src="xy.js"></script>

 

<style type="text/css">

    * {

        margin: 0;

        border: 0;

    }

 

    .cell {

        position: absolute;

        width: 100px;

        height: 50px;

        border: solid;

        background: rgb(21, 98, 231);

    }

</style>

</head>

<body>

 

 

</body>

 

</html>

达成win7职分栏拖动作效果果 在某些时刻, 笔者认知了四个对象. 这个人在自家的煽动下, 踏上了js的不归路. 昨日她问小编, Win7职务栏拖动作效果果怎么落实...

以下部分是任何代码,为方便阅读,调节和测验新闻未删减。

如上正是关于利用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();

一定于填写出9宫格里,全体的“分明项”,以及标识“恐怕选项”。

你可能感兴趣的篇章:

  • python完成数独算法实例
  • JQuery开荒的数独游戏代码
  • java使用回溯法求解数独示例
  • Jquery数独游戏分析(一)-页面布局
  • c++递归解数独方法亲自过问
  • javascript sudoku 数独智力游戏生成代码
  • Javascript 完毕的数独解题算法兰西网球国际比赛页实例
  • Go语言达成的最简便数独解法
  • JavaScript遍历求解数独难点的要害思路小结
  • Python如何判定数独是或不是合法

        }
    }
    if (result == false) { //对于(candidatNum[i][j][0] != 1)的事态,举办筛选
        return choose();
    }
    return result;
}
function refreshStat3( num, x, y) {   //  更新行列宫
        rows[x] |= 1<<num;
        columns[y] |= 1<<num;
        blook[parseInt(x / 3) * 3 + parseInt(y / 3)] |= 1 << num ;

复制代码 代码如下:

}
function CheckNumInput(array,num,  x,  y)  {
    //  目标:
    //      争论检查  参数 array:矩阵 num:检查评定值 x/y:检验地方
    //      行列宫均无争辨,return true;
    //            发掘争辩,return false;
    if (((rows[x] & (1 << num)) == 0) && (columns[y] & (1 << num)) == 0
        && (blook[parseInt(x / 3) * 3 + parseInt(y / 3)] & (1 << num)) == 0) {
        return true;
        }
    return false;
}

实际上人脑思虑的历程,也是要先遍历选项相当少的分层。

您恐怕感兴趣的稿子:

  • python完结数独算法实例
  • JQuery开拓的数独游戏代码
  • java使用回溯法求解数独示例
  • Jquery数独游戏深入分析(一)-页面布局
  • c++递归解数独方法言传身教
  • javascript sudoku 数独智力游戏生成代码
  • javascript贯彻数独解法
  • Go语言达成的最简易数独解法
  • JavaScript遍历求解数独难题的非常重要思路小结
  • Python怎样推断数独是或不是合法