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 ...
随机推荐
- 多线程串口通信 MFC CSerialPort
写在前面: 晚上应该继续完成未写完的代码,但Chrome上打开的标签实在太多了,约30个了,必须关掉一些,所以需要把自己看的整理一下然后关掉.本次主要写点MFC环境下多线程串口通信相关的东西,这包括线 ...
- 【开发小结】Two Steps from Deadline
进度条可以救我也可以杀死我 # START 2018年4月17日晚我测试了11组四则运算的UI,每个exe程序生成的每一道题都有恐怖的倒计时.PSP表格清晰的记录了开发过程中消耗的时间,但是在结对作业 ...
- Java中protected方法访问权限的问题
先看Test.java 此时出现上文提到的错误:The method clone from the type Object is not visiuable. 我们已经清楚Object.clone() ...
- 关系代数演算So Easy
关系代数运算So Easy 关系代数是以关系为运算的一组高级运算的集合.由于定义为属性个数 相同的元组的集合,因此集合代数的操作就可以引入到关系代数中.关系代数也可以看做是一种抽象的查询语言,是对关系 ...
- Hybrid App开发之Html基本标签使用
前言: 前面简单学习了html简单标签的使用,今天学习一下其他的标签的使用. HTML的超链接 1.)创建一个超链接 <div> <p> <a href="ht ...
- 读书笔记-《深入理解Java虚拟机:JVM高级特性与最佳实践》
目录 概述 第一章: 走进Java 第二章: Java内存区域与内存溢出异常 第三章: 垃圾收集器与内存分配策略 第四章: 虚拟机性能监控与故障处理 第五章: 调优案例分析与实战 第六章: 类文件结构 ...
- 汉明码(Hamming Code)原理及实现
汉明码实现原理 汉明码(Hamming Code)是广泛用于内存和磁盘纠错的编码.汉明码不仅可以用来检测转移数据时发生的错误,还可以用来修正错误.(要注意的是,汉明码只能发现和修正一位错误,对于两位或 ...
- LiteIDE 错误: go build xxxxxx: no non-test Go files in xxxx
问题 c:/go/bin/go.exe build [C:/Users/Administrator/Desktop/go] go build _/C_/Users/Administrator/Desk ...
- php常见验证
/** * 文件上传 * @param $file 要上传的文件 * @param $size 大小设置 * @param $ext 文件类型 * @return bool 是否上传成功 */func ...
- 03_6_package和import语句
03_6_package和import语句 1. package和import语句 为便于管理大型软件系统中数目众多的类,解决类的命名冲突问题,Java引入包(package)机制,提供类的多重命名空 ...