37. Sudoku Solver(js)

37. Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3sub-boxes of the grid.

Empty cells are indicated by the character ‘.‘.


A sudoku puzzle...

题意:填满剩余数字,使其满足数独

代码如下:

/**
 * @param {character[][]} board
 * @return {void} Do not return anything, modify board in-place instead.
 */
var solveSudoku = function(board) {
    if(board.length===0 || board.length!==9 || board[0].length!==9) return ;
    dfs(board,0,0);
};
// 判断是否符合要求
/**
*1.每一行值各不相同
*2.每一列值各不相同
*3.每个3X3的小棋盘里的值各不相同
*/
var isValid=function(board,i,j){
    for(let col=0;col<9;col++){
        if(col!==j && board[i][j]===board[i][col]) return false;
    }
    for(let row=0;row<9;row++){
        if(row!==i && board[i][j]===board[row][j]) return false;
    }
    for(let row=parseInt(i/3)*3;row<parseInt(i/3)*3+3;row++){
        for(let col=parseInt(j/3)*3;col<parseInt(j/3)*3+3;col++){
            if((row!==i || col!==j)&&board[i][j]===board[row][col]) return false;
        }
    }
    return true;
};
/**
*递归遍历,边界判断,逐个数字带入值为‘.‘的方格,判断其是否符合要求
*/
var  dfs=function(board,i,j){
    if(i===9) return true;
    if(j>=9) return dfs(board,i+1,0);
    if(board[i][j]===‘.‘){
        for(let k=1;k<=9;k++){
            board[i][j]=k+‘‘;
            if(isValid(board,i,j)){
                if(dfs(board,i,j+1)) return true;
            }
            board[i][j]=‘.‘;
        }
    }else{
        return dfs(board,i,j+1);
    }
    return false;
};

原文地址:https://www.cnblogs.com/xingguozhiming/p/10415253.html

时间: 2024-10-12 02:18:24

37. Sudoku Solver(js)的相关文章

LeetCode 37 Sudoku Solver(求解数独)

题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独进行求解. 思路: 嵌套循环,三层循环体,每一行,每一列,填入从1到9的数字.判断填入之后是否合理 判断数独是否合理的函数 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 求解数独问题 */ public class Solutio

[LeetCode] Sudoku Solver(迭代)

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 方法:解此问题的关键是

LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku boa

JavaScript(JS)之Javascript对象

JavaScript(JS)之Javascript对象 简介: 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,String.Math.Array.Date.RegExp都是JavaScript中重要的内置对象,在JavaScript程序大多数功能都是基于对象实现的 <script language="javascript"> var aa=Number.MAX_VALUE; //利用数字对象获取可

百度静态资源(JS)公共库

     例如: chosen http://apps.bdimg.com/libs/chosen/1.1.0/chosen.jquery.min.js classlist http://apps.bdimg.com/libs/classlist/2014.01.31/classList.min.js cookies.js http://apps.bdimg.com/libs/Cookies.js/0.4.0/cookies.min.js dojo http://apps.bdimg.com/l

前台强大的图表(js)

http://www.jqplot.com/ 网址,自己下载下例子看一下就明白了. 这个是我之前做的项目中的例子(仅是部分代码): function publicMethod(){                         var plot1 = $.jqplot(chart, [currYear], {                 seriesColors: ["rgb(23, 108, 238)"],                 title: titles,     

创建HTML新元素(js)

1 <!-- 2 创建新的HTML元素 3 1.创建新的元素 4 2.创建新的节点 5 3.追加节点 6 4.向已有元素追加新的元素 7 --> 8 <html> 9 <body> 10 11 <div id="div1"> 12 <p id="p1">这是一个段落</p> 13 <p id="p2">这是另一个段落</p> 14 </div&g

给数组添加一个根据指定下标删除元素的方法、得到0-100的随机数不重复(js)、得到外联样式的css样式值

/** *删除数组指定下标或指定对象 */ Array.prototype.remove=function(obj){ for(var i =0;i <this.length;i++){ var temp = this[i]; if(!isNaN(obj)){ temp=i; } if(temp == obj){ for(var j = i;j <this.length;j++){ this[j]=this[j+1]; } this.length = this.length-1; } } }

JavaScript(JS)之简单介绍

JavaScript(JS)之简单介绍 一.JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.(客户端执行的语言) Netscape(网景)接收Nombas的理念,(Brendan Eich)在其Netscape Navigator 2.0产品中开发出一套livescript的脚本语言.Sun和Netscape共同完成.后改名叫Javascript 微软随后模仿在其IE3.0