Javascript 实现的数独解题算法网页实例(5)

function calculate(){  //功能入口
    //读取数据
    var start=new Date();
    for (var i = 0; i < 9; i++)
        {   
            for (var j = 0; j < 9; j++)
            {
                var text = document.getElementById("input"+(i*9+j));
                grid[i][j]=parseInt(text.value);
        }
    }

    //刷新网格   
    refreshStat();
    out(grid);
    //计算矩阵
    while(true){
        var a=setOne();
        var b=refreshStat();
        if(!a||b){ //如果 a==false 或者 b==ture,则可以跳出循环
            break;
        }
    }
    out(grid);    //答案
    alert("用时:"+(new Date()-start)+"ms");
    success = true;
    //计算结束

    //验证答案是否唯一
    if (papers != discards){
            if (steps.length>0) {// 回退
                pop();           // 当前标签废弃
                //计算矩阵
                while(true){
                    var a=setOne();
                    var b=refreshStat();
                    if(!a||b){ //如果 a==false 或者 b==ture,则可以跳出循环
                        break;
                    }
                }
                if (b) {
                    alert("答案不唯一!记录!");
                    out(grid);    //答案
                    }
                else {
                    alert("答案唯一!!");    //答案唯一
                }
            } else {
                alert("出错 结束!");
                return false;
            }
    }
}
function clearGrid(){
    for (var i = 0; i < 9; i++){
        for (var j = 0; j < 9; j++){
            grid[i][j]=0;
            document.getElementById("input"+(i*9+j)).value=grid[i][j];
        }
    }
    out(grid);
}
function init(){
     for (var i = 0; i < 9; i++)
        {    candidatNum[i]=new Array();

            for (var j = 0; j < 9; j++)
            {    candidatNum[i][j]=new Array();

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjxzwy.html