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.

思路1:使用暴力DFS
public class Solution {
private boolean isValidSudoku(char[][] board, int row, int column) {
int i, j;
int[] valid1 = new int[10];
int[] valid2 = new int[10];
int[] valid3 = new int[10];
for (i = 0; i <9; i++) {
if (board[i][column] != '.') {
if (valid1[board[i][column] - '0'] > 0)
return false;
valid1[board[i][column] - '0']++;
}
if (board[row][i] != '.') {
if (valid2[board[row][i] - '0'] > 0)
return false;
valid2[board[row][i] - '0']++;
}
}
for (i = (row / 3) * 3; i < (row / 3 + 1) * 3; i++) {
for (j = (column / 3) * 3; j < (column / 3 + 1) * 3; j++) {
if (board[i][j] != '.') {
if (valid3[board[i][j] - '0'] > 0)
return false;
valid3[board[i][j] - '0']++;
}
}
}
return true;
} private boolean internalSolveSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
for (int k = 1; k <= 9; k++) {
board[i][j] = (char) ('0' + k);
if (isValidSudoku(board, i, j)) {
if (internalSolveSudoku(board)) {
return true;
}
}
board[i][j] = '.';
return false;
}
} }
}
return true;
} public void solveSudoku(char[][] board) {
internalSolveSudoku(board);
}
}

思路2:使用Dancing Links,代码后补。


版权声明:本文博客原创文章。博客,未经同意,不得转载。

LeetCode 36 Sudoku Solver的更多相关文章

  1. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

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

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

  3. 【leetcode】Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  4. LeetCode 037 Sudoku Solver

    题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...

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

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

  6. Java for LeetCode 037 Sudoku Solver

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

  7. Java [leetcode 37]Sudoku Solver

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

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

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

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

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

随机推荐

  1. 博客搬家啦! -----> http://ronghaopger.github.io/

    新地方: http://ronghaopger.github.io/ 以后这里就不更新了,感谢博客园!

  2. 30行js rem弹性布局适配所有分辨率

    <script> /* # 按照宽高比例设定html字体, width=device-width initial-scale=1版 # @pargam win 窗口window对象 # @ ...

  3. Scrapy系列教程(1)------命令行工具

    默认的Scrapy项目结构 在開始对命令行工具以及子命令的探索前,让我们首先了解一下Scrapy的项目的文件夹结构. 尽管能够被改动,但全部的Scrapy项目默认有类似于下边的文件结构: scrapy ...

  4. Git提交到多个远程仓库(多看两个文档)

    Git提交到多个远程仓库(多看两个文档) 一.总结 一句话总结: 二. Git提交到多个远程仓库(多看两个文档) 有两种做法,先看第一种 一.通过命令行进行操作 例如我有下面两个仓库: Mybatis ...

  5. Methods and systems to control virtual machines

    Methods and systems are provided to control the execution of a virtual machine (VM). A VM Monitor (V ...

  6. FMDB数据库框架

    nFMDB   nFMDB n什么是FMDB pFMDB是iOS平台的SQLite数据库框架 pFMDB以OC的方式封装了SQLite的C语言API p nFMDB的优点 p使用起来更加面向对象,省去 ...

  7. 新技能 get —— 使用 python 生成词云

    什么是词云(word cloud)呢?词云又叫文字云,是对文本数据中出现频率较高的"关键词"在视觉上的突出呈现,形成关键词的渲染形成类似云一样的彩色图片,从而一眼就可以领略文本数据 ...

  8. Sqlplus的一般操作

    Sqlplus一些必要操作指令 1, 登陆sys 用户,需要加上 as sysdba Connect sys as sysdba; Input your password; Connected; 2, ...

  9. 【t095】拯救小tim

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小tim在游乐场,有一天终于逃了出来!但是不小心又被游乐场的工作人员发现了... 所以你的任务是安全地 ...

  10. 【27.66%】【codeforces 592D】Super M

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...