37. 解数独

编写一个程序,通过已填充的空格来解决数独问题。

一个数独的解法需遵循如下规则:

数字 1-9 在每一行只能出现一次。

数字 1-9 在每一列只能出现一次。

数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

空白格用 ‘.’ 表示。

一个数独。

答案被标成红色。

Note:

给定的数独序列只包含数字 1-9 和字符 ‘.’ 。

你可以假设给定的数独只有唯一解。

给定数独永远是 9x9 形式的。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/sudoku-solver

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
public void solveSudoku(char[][] board) {
/**
* 记录某行,某位数字是否已经被摆放
*/
boolean[][] row = new boolean[9][9];
/**
* 记录某列,某位数字是否已经被摆放
*/
boolean[][] col = new boolean[9][9];
/**
* 记录某 3x3 宫格内,某位数字是否已经被摆放
*/
boolean[][] block = new boolean[9][9]; for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int num = board[i][j] - '1';
row[i][num] = true;
col[j][num] = true;
// blockIndex = i / 3 * 3 + j / 3,取整
block[i / 3 * 3 + j / 3][num] = true;
}
}
}
dfs(board, row, col, block, 0, 0);
} private boolean dfs(char[][] board, boolean[][] row, boolean[][] col, boolean[][] block, int i, int j) {
// 找寻空位置
while (board[i][j] != '.') {
if (++j >= 9) {
i++;
j = 0;
}
if (i >= 9) {
return true;
}
}
for (int num = 0; num < 9; num++) {
int blockIndex = i / 3 * 3 + j / 3;
if (!row[i][num] && !col[j][num] && !block[blockIndex][num]) {
// 递归
board[i][j] = (char) ('1' + num);
row[i][num] = true;
col[j][num] = true;
block[blockIndex][num] = true;
if (dfs(board, row, col, block, i, j)) {
return true;
} else {
// 回溯
row[i][num] = false;
col[j][num] = false;
block[blockIndex][num] = false;
board[i][j] = '.';
}
}
}
return false;
} private void printBoard(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
}

Java实现 LeetCode 37 解数独的更多相关文章

  1. [leetcode] 37. 解数独(Java)(dfs,递归,回溯)

    37. 解数独 1A 这个题其实15分钟左右就敲出来并且对了...但是由于我输错了一个数..导致我白白debug一个多小时.. 没啥难度,练递归-dfs的好题 class Solution { pri ...

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

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

  3. Leetcode——37.解数独 [##]

    @author: ZZQ @software: PyCharm @file: leetcode37_solveSudoku.py @time: 2018/11/20 16:41 思路:递归回溯 首先, ...

  4. Leetcode之回溯法专题-37. 解数独(Sudoku Solver)

    Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...

  5. leetcode刷题-37解数独

    题目 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1-9 在每一列只能出现一次.数字 1-9 在每一个以粗实线分隔的 3x ...

  6. 【LeetCode】解数独

    做题之前先复习下[STL中的Tuple容器] 我们知道,在Python中,大家都知道tuple这个概念,是一个只读的元素容器,容器内的元素数据类型可以不同,而在CPP中大部分的容器只能储存相同数据类型 ...

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

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

  8. java解数独

    先输入要解的数独,采用多维数组来保存其中的值,未填数字的地方,初始化为0,然后采用递归的方法来解数独. 直接上代码: /** * * @author walker * */ public class ...

  9. [LeetCode] Sudoku Solver 解数独,递归,回溯

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

随机推荐

  1. 正则表达式在java中的用法

    /** * 测试正则表达式的基本用法 Pattern 和 Matcher * @author 小帆敲代码 * */public class Demo01 {  public static void m ...

  2. Spring整合Durid数据源

    Spring整合Durid数据源 一.导入依赖 <!-- Druid数据源 --> <dependency> <groupId>com.alibaba</gr ...

  3. 学习docker的一点记录

    0x00 docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化 ...

  4. jQuery学习笔记——jQuery基础核心

    代码风格 在jQuery程序中,不管是页面元素的选择.内置的功能函数,都是美元符号“$”来起始的.而这个“$”就是jQuery当中最重要且独有的对象:jQuery对象,所以我们在页面元素选择或执行功能 ...

  5. IndexDB

    参考:http://www.ruanyifeng.com/blog/2018/07/indexeddb.html   cookie localStorage IndexedDB 大小 不超过4KB 2 ...

  6. Codeforces1144C(C题)Two Shuffled Sequences

    C. Two Shuffled Sequences Two integer sequences existed initially — one of them was strictly increas ...

  7. css概述四

    八.文本格式化 2.文本属性 ①文本颜色 color:合法的颜色值 ②文本对齐方式 text-align: 取值 left/center/right/justify 注意  1.一个元素写了text- ...

  8. Python 如何随机打乱列表(List)排序

    场景: 现在有一个list:[1,2,3,4,5,6],我需要把这个list在输出的时候,是以一种随机打乱的形式输出. 专业点的术语:将一个容器中的数据每次随机逐个遍历一遍. 注意:不是生成一个随机的 ...

  9. easyui及读取xml

    本地测试地址例如http://localhost:6541/TreeExam/AuthorityTree TreeExam 是TreeExamController AuthorityTree是Tree ...

  10. 愉快地使用Windows Terminal

    有了Windows Terminal 你再也不需要其他任何终端了-- 下载 直接到Windows 10 自带的应用商店下载安装.参考: https://github.com/Microsoft/Ter ...