Leetcode37--->Sudoku Solver(填充数独)
题目: 给定一个不完整的数独,要求填充好数独;最初给出的数独是有效的,且假设一定有答案;
举例:
![]()
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(填充数独)的更多相关文章
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- Sudoku Solver, 求数独
问题描述:填充数独表中空元素.空元素为'.' 算法分析:没填充一个数,都要看这个数所在的行,列,小矩阵是否合法.然后还要看整个数独表是否正确,而判断整个数独表只能通过递归,因为前一个结果的判断要依赖后 ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- LeetCode37 Sudoku Solver
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- [LeetCode] Sudoku Solver 解数独,递归,回溯
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 037 Sudoku Solver 解数独
写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...
- LeetCode OJ:Sudoku Solver(数独游戏)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
随机推荐
- Kendo MVVM 数据绑定(四) Disabled/Enabled
Kendo MVVM 数据绑定(四) Disabled/Enabled Disabled 和 Enabled 绑定可以根据 ViewModel 的某个属性值的 true,false 来设置 DOM 元 ...
- Chrome Java插件过期
企业应用软件中,基本都是基于某个版本的JDK进行开发的,更新跟不上Oracle更新的步伐,Chrome浏览器自动默认关闭了过期插件导致用Chrome无法打开应用软件. 解决办法如下:
- 程序员的智囊库系列之3--分布式文件系统(Distributed file systems)
程序员的智囊库系列之3--分布式文件系统(Distributed file systems) 这是程序员的智囊库系列的第三篇文章.上一篇文章本来打算介绍几个搭建网站的框架,但由于这部分的内容较多,还需 ...
- fastjson中转字符串时格式化、显示null值等
fastjson中object转string时的配置项,包括 1. 是否显示value为null的项 2. 是否格式化显示字符串 3. 日期是否格式化显示为可读字符串 ... 这些的配置均在Seria ...
- 免费手机号码归属地API查询接口
免费手机号码归属地API查询接口 一.淘宝网API API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=1585078144 ...
- 字符串的驻留(String Interning)
http://www.cnblogs.com/artech/archive/2007/03/04/663728.html 关于字符串的驻留的机制,对于那些了解它的人肯定会认为很简单,但是我相信会有很大 ...
- 关于List的remove方法我遇到的坑
结果是下标越界异常,原因是remove方法的参数不是value,而是index 唉~~~ 年少轻狂啊
- 使用HTML5语义标签时要注意的问题
header,nav,section,article,aside,figue,figcaption,footer以上这些标签(除figcaption标签外)都是块级标签,为了让这些标签及元素在所有的浏 ...
- Xcode中的Project和Target
新创建工程(如下图e.g.),APP的属性包括了 PROJECT 和 TARGETS 两块内容.且一个工程只有一个 PROJECT,但可以有一个或多个 TARGETS(从苹果的命名上也可以看出,这个 ...
- 【动态规划】bzoj1705: [Usaco2007 Nov]Telephone Wire 架设电话线
可能是一类dp的通用优化 Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设 ...