其他分享
首页 > 其他分享> > js测试生成的数独是否正确

js测试生成的数独是否正确

作者:互联网

 


function equals45(n){
  return n == 45;
}

function validSolution(board){
  var sumh = [0,0,0,0,0,0,0,0,0];
  var sumv = [0,0,0,0,0,0,0,0,0];
  var osums = [[0,0,0],[0,0,0],[0,0,0]];
  for (var i=0;i<9;i++){
    for (var j=0;j<9;j++){
      sumh[i] += board[i][j];
      sumv[j] += board[i][j];
      osums[Math.floor(i/3)][Math.floor(j/3)] += board[i][j];
    }
  }
  for (var i=0;i<3;i++) if (!osums[i].every(equals45)) return false;
  return (sumh.every(equals45) && sumv.every(equals45));
}

module.exports={validSolution}

标签:function,equals45,return,测试,sumv,js,osums,var,数独
来源: https://blog.csdn.net/qq_38332693/article/details/119278781