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. 【30.36%】【codeforces 740D】Alyona and a tree

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

  2. 卸载、指定卸载 .NET Core Runtime and SDK

    原文:卸载.指定卸载 .NET Core Runtime and SDK 项目使用的 Nuget 包,比如 Microsoft.AspNetCore.App等的版本号要与 .NET Core 版本号( ...

  3. acdream 1430 SETI 后缀数组+height分组

    这题昨天比赛的时候逗了,后缀想不出来,由于n^2的T了,就没往后缀数组想--并且之后解题的人又说用二分套二分来做.然后就更不会了-- 刚才看了题解,唉--原来题讲解n^2的也能够过,然后就--这样了! ...

  4. 从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)

    这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_n ...

  5. 编辑器sublime、终端运行python

    sublime编辑器 Sublime Text 是一个代码编辑器(Sublime Text 2是收费软件,但可以无限期试用) Sublime Text是由程序员Jon Skinner于2008年1月份 ...

  6. 小强的HTML5移动开发之路(36)——jQuery中的DOM操作

    1.查询 利用选择器查找节点 使用 html() / text() / attr() 输出节点文本和属性值. 注意:下拉列表使用 val() <html> <head> < ...

  7. [搜索]Trie树的实现

    trie这种树也被称为线索,搜索树. 正如图 以下是用stl 的map来实现 class trie_item_c { public: trie_item_c(){} trie_item_c(const ...

  8. hadoop 3.x 无法访问hdfs(50070,8088)的web界面

    1.启动hadoop.然后netstat -nltp|grep 50070,如果,没有找到进程,说明没有配置web界面的端口修改hdfs-site,xml中加上如下配置 再次启动后,netstat - ...

  9. Android分享介绍

    一.使用系统分享 public void execShare(Activity context,String title,String text){ Intent intent = new Inten ...

  10. Ultra-wideband (UWB) secure wireless device pairing and associated systems

    Methods and systems are disclosed for ultra-wideband (UWB) secure wireless device pairing. Secure pa ...