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. 这才是真正的裸眼3D!超级震撼!!

    大家有没认为有意思啦,反正俺是被震撼到了. 好奇异-- ! " src="http://www.yixieshi.com/uploads/allimg/141116/1446431 ...

  2. ABBYY FineReader Pro for Mac有哪些特性(下)

    使用ABBYY FineReader Pro for Mac轻松转换纸质文档.PDF文件和数字文本照片为可编辑和可搜索的文件,再也不需要手动重新输入或格式化了,相反,可以编辑.搜索.共享.归档和复制文 ...

  3. [JS] ECMAScript 6 - Destructuring

    C#里没有这种变态的方法. 虽然变态,但看起来不错的样子. 变量的解构赋值 完全解构:本质上,这种写法属于“模式匹配”,: 不完全解构:同时支持“不完全解构” let [x, y] = [1, 2, ...

  4. [UI] 06 - jQuery

    前言 From : http://www.runoob.com/jquery/jquery-intro.html Ref: jQuery 实例 一.什么是 jQuery ? jQuery是一个Java ...

  5. mysql无法生成log文件

    做服务器主从配置时,发现/var/log/mysql下面一直没有log文件,明明配置文件/etc/mysql/mysql.conf.d/mysqld.cnf也开启log了. general_log_f ...

  6. Python中StringIO和BytesIO

    介绍一下Python在内存中读写数据,用到的模块是StringIO和BytesIO StringIO >>> from io import StringIO >>> ...

  7. MongoDB数据库基础

    MongoDB简介 MongoDB是一种文档型的非关系型数据库(NoSQL),举例如下: {“foo”:,"greeting":"Hello,world!"} ...

  8. AIX文件系统和存储部署(转)

    文件系统和存储部署 文件系统的管理是AIX存储结构中的最后一环.定义完lv后,可采用如下两种方式使用lv: a.作为裸设备(raw)使用,一般是数据库型的应用 b.在lv上定义文件系统,并提供文件和数 ...

  9. 洛谷试炼场-简单数学问题-P1088 火星人

    洛谷试炼场-简单数学问题 A--P1088 火星人 Description 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法 ...

  10. [No0000150]VSVisualStudio提示图标,信号图标的含义

    其右侧的图标表示这是一个接口类型__interface(或者是结构体类型) 其右侧图标表示这是一个类类型 其右侧图标表示这是一个.cpp文件(貌似还可以是.hpp等文件) 其右侧图标表示这是一个枚举类 ...