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. 今天竟然有人模仿我的QQ号进行骗钱

    今天下午,CoderGeek同学告诉我,有个叫"小雷FansUnion"的QQ正在找他要钱,他直接和我这个正宗的"小雷FansUnion"探听虚实.这时才发现, ...

  2. Java的面向AOP编程

    一. 引言 AOP(Aspect-Oriented Programming,面向切面的编程),是一种新型的编程范式,主张关注软件流程中的一个切面,将相同功能的代码整合打包在一起,减少系统的耦合性,增强 ...

  3. 【U205】最大值

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 找到一个数组的最大值的一种方法是从数组开头从前到后对数组进行扫描,令max=a[0](数组下表从0.. ...

  4. 【hdu 1067】Gap

    Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission( ...

  5. Java序列化机制中的类版本号问题

    原文地址:http://yanwushu.sinaapp.com/java_serialversionuid/ 内容简单介绍 某些实现了serializable接口的java类中会看到名称为seria ...

  6. 【矩阵】概念的理解 —— span、基

    span:全部列向量的线性组合构成的集合: span[a1,-,an]={y∈Rm|y=∑k=1nckak}=S 注:ak∈Rm,共 n 个列向量: 集合 S 可以有不同的一组基,但是基中向量的个数是 ...

  7. 详细回复某个CSDN网友,对我的文章和技术实力以及CSDN的吐槽

    貌似被大学生鄙视了,我也是醉了,现在的大学生水平和信心,都这么高了~ 看来,我得加把劲了~ o(︶︿︶)o 电子商务系列文章,是我闲来无事,分享自己的一些业余实践经验的文章.其中关于数据库设计的这一篇 ...

  8. HDU 树型dp

    HDU 4123 Bob's Race 题意:定义每个点的值为它到树上最远点的距离,每次询问q,回答最长的极值差小于等于q且编号连续的一段点的长度. 题解:求距离两次dp,求极值ST表+尺取法. HD ...

  9. 【codeforces 791D】 Bear and Tree Jumps

    [题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...

  10. oracle的sql查询结果拼接

    oracle数据库中,使用wm_concat(column)函数,可以进行字段合并 oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oracle wm_co ...