题目: 给定一个不完整的数独,要求填充好数独;最初给出的数独是有效的,且假设一定有答案;

举例:

A sudoku puzzle...

解题思路:

该题与青蛙走迷宫问题很相似,都是用深度优先;

代码如下:

 public class Solution {
public void solveSudoku(char[][] board) {
if(board == null || board.length < 9 || board[0].length < 9)
return;
solve(board, 0);
}
public boolean solve(char[][] board, int position)
{
if(position == 81) // position可以唯一确定一个坐标
return true;
int row = position / 9;
int col = position % 9;
if(board[row][col] == '.')
{
for(int i = 1; i <= 9; i++)
{
board[row][col] = (char)('0' + i);
if(checkValid(board, position)) // 检查将board[row][col]修改后,行列块是否有效
{
if(solve(board, position + 1)) // 深度搜索
return true;
}
board[row][col] = '.';
}
}
else
{
if(solve(board, position + 1))
return true;
}
return false;
}
public boolean checkValid(char[][] board, int position)
{
int row = position / 9;
int col = position % 9;
char target = board[row][col];
for(int j = 0; j < 9; j++)
{
if(j != col)
{
if(target == board[row][j]) // 判断除过col列,row行是否有taeget
return false;
}
if(j != row)
{
if(target == board[j][col]) // 判断除过row行,col列是否有target
return false;
}
}
int beginx = row / 3 * 3;
int beginy = col / 3 * 3;
for(int i = beginx; i < beginx + 3; i ++) // 块中是否有target
{
for(int j = beginy; j < beginy + 3; j ++)
{
if(i != row && j != col)
{
if(target == board[i][j])
return false;
}
}
}
return true; }
}

Leetcode37--->Sudoku Solver(填充数独)的更多相关文章

  1. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  2. Sudoku Solver, 求数独

    问题描述:填充数独表中空元素.空元素为'.' 算法分析:没填充一个数,都要看这个数所在的行,列,小矩阵是否合法.然后还要看整个数独表是否正确,而判断整个数独表只能通过递归,因为前一个结果的判断要依赖后 ...

  3. [LeetCode] Sudoku Solver 求解数独

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

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

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

  5. LeetCode37 Sudoku Solver

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

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

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

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

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

  8. 037 Sudoku Solver 解数独

    写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...

  9. LeetCode OJ:Sudoku Solver(数独游戏)

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

随机推荐

  1. CF1142A The Beatles

    思路: 令p表示步数,l表示步长.由于p是使(l * p) % (n * k) == 0的最小的p,所以p = (n * k) / gcd(n * k, l). 设l = k * x + r,则由题意 ...

  2. JS计算24节气的方法

    function getjq(yyyy,mm,dd){ mm = mm-1; var sTermInfo = new Array(0,21208,42467,63836,85337,107014,12 ...

  3. 零基础逆向工程12_C语言06_switch语句反汇编

    12_C语言06_switch语句反汇编 switch语句反汇编 测试环境:VC++6.0 分支少于4的时候没有意义,编译器会生成类似if...else之类的反汇编,不超过三个分支,不会生成索引表. ...

  4. <Android 应用 之路> 天气预报(三)

    昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码 基本ListView显示 搜索框,查询城市 上一篇文章中,载入界面通过showWeatherInfo()方法跳转到天气信息显示界面 priv ...

  5. JFinal教程:JFinal极速开发企业实战百集JFinal视频教程发布

    课程名称:JFinal极速开发企业实战 课程长度:100课时 课程作者:小木(909854136) 课程地址:http://edu.csdn.net/course/detail/1968 官网网址:h ...

  6. TCP连接建立与关闭

    http://hi.baidu.com/psorqkxcsfbbghd/item/70f3bd91943b9248f14215cd TCP连接建立与关闭 TCP 是一个面向连接的协议,无论哪一方向另一 ...

  7. 监测元素resize

    前言 近来有需求要做分页,听起来可能有点Low. 所以我要把Low的事情做得有点逼格. 分页本身没啥,但是数据量起来了,比如十万. 要是不做点处理, 那你的页面估计爽得很,机器也爽得很. 放心,我不会 ...

  8. jquery.page.js插件在使用时重复触发“上一页”和“下一页”操作

    jquery.page.js使用demo HTML代码 <div class="result"> <div class="tcdPageCode&quo ...

  9. JS给数字加千位分隔符

    本文原链接:https://www.jianshu.com/p/928c68f92c0c JavaScript实现千位分隔符 将普通的数字转换为带千位分隔符格式的数字字符串是一个非常常见的问题,千位分 ...

  10. stixel-world和psmnet结合出现的问题

    float32位,4字节 原本的stixel-world是用sgbm生成深度图,并且转成了float型 psmnet保存最终的disparity图是保存成uint16的,skimage.io.imsa ...