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 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.


A sudoku puzzle...


...and its solution numbers marked in red.

Note:

    • The given board contain only digits 1-9 and the character '.'.
    • You may assume that the given Sudoku puzzle will have a single unique solution.
    • The given board size is always 9x9.

判断当前插入的值有没有错误,没有继续填下面的

 class Solution {//剪枝 mytip
public void solveSudoku(char[][] board) {
if(null==board ||0==board.length)
return;
help(board);
}
private boolean help(char[][] board){
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board.length; col++) {
if('.'==board[row][col]){
for (char i = '1'; i <= '9'; i++) {
if(isValidSudoku(board,row,col,i)){
board[row][col]=i;
if(help(board)){
return true;
}
else{
board[row][col]='.';
}
}
}
return false;
}
}
}
return true;
}
private boolean isValidSudoku(char[][] board,int row,int col,char ch) {
for (int i = 0; i <board.length ; i++) {
if(ch==board[row][i]||ch==board[i][col]){
return false;
}
}
int r = row/3*3;
int c = col/3*3;
for (int i = r; i <r+3 ; i++) {
for (int j = c; j < c+3 ; j++) {
if(ch==board[i][j]){
return false;
}
}
} return true;
}
}

另外可以通过以下方式加速

相关题

有效的数独 LeetCode36 https://www.cnblogs.com/zhacai/p/10622779.html

LeetCode-37.Sudok Solver的更多相关文章

  1. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  2. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  3. LeetCode 37 Sudoku Solver(求解数独)

    题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description   Problem : 解决数独问题,给出一个二维数组,将这个数独 ...

  4. [leetcode 37]sudoku solver

    1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循 ...

  5. leetcode 37 Sudoku Solver java

    求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution ...

  6. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  7. [leetcode]37. Sudoku Solver 解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  8. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  9. leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独

    leetcode 36 感觉就是遍历. 保存好状态,就是各行各列还有各分区divide的情况 用数组做. 空间小时间大 class Solution { public: bool isValidSud ...

  10. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

随机推荐

  1. Oracle HAVING子句 - 转

    使用 HAVING 子句选择行 HAVING 子句对 GROUP BY 子句设置条件的方式与 WHERE 子句和 SELECT 语句交互的方式类似.WHERE 子句搜索条件在进行分组操作之前应用:而 ...

  2. Ubuntu 14.04 下搭建SVN服务器 (转载自 http://www.linuxidc.com/Linux/2015-01/111956.htm)-------------我所用到是红色字体

    http://www.linuxidc.com/Linux/2015-01/111956.htm Ubuntu 14.04 下搭建SVN服务器 svn:// 安装软件包: sudo apt-get i ...

  3. Schema中elementFormDefault="qualified"所起的作用

    有的xsd文件的开头是这样写的: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.or ...

  4. Ubuntu上查内存情况

    free -h

  5. 设计模式-行为型模式,python 中介者模式

    中介者模式 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性.这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护.中介者模式属于行 ...

  6. [Bayes] Point --> Line: Estimate "π" by R

    “半个数学系 + 一个计算机科学系 = Deep Learning初级班” simulation = function(sampleSize){ c = rep(0,sampleSize) // &l ...

  7. css3整理--Animation

    animation语法: 1.动画的定义 @keyframes IDENT { from { Properties:Properties value; } Percentage { Propertie ...

  8. G - SDOI

    The Annual National Olympic of Information(NOI) will be held.The province of Shandong hold a Select( ...

  9. Understanding Flash: Blocks, Pages and Program / Erases

    https://flashdba.com/2014/06/20/understanding-flash-blocks-pages-and-program-erases/ In the last pos ...

  10. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...