Java [leetcode 37]Sudoku Solver
题目描述:
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.
解题思路:
本题使用回溯和HashSet的方法。对于每一个空白位置,试探的使用‘1’-'9’之间的数字,如果加入该数字在满足数独规则的情况下,将该字符加入该位置,向下去试探;如果试探失败,则回到当前这步,换另一个字符继续进行试探。
代码如下:
public class Solution {
public void solveSudoku(char[][] board) {
HashSet[] row = new HashSet[9];
HashSet[] col = new HashSet[9];
HashSet[] cell = new HashSet[9];
initHashSet(board, row, col, cell);
solve(board, row, col, cell);
}
public boolean solve(char[][] board, HashSet[] row, HashSet[] col,
HashSet[] cell) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
for (char c = '1'; c <= '9'; c++) {
if (isValidSudoku(board, i, j, c, row, col, cell)) {
board[i][j] = c;
row[i].add(c);
col[j].add(c);
cell[3 * (i / 3) + j / 3].add(c);
if (solve(board, row, col, cell))
return true;
else {
board[i][j] = '.';
row[i].remove(c);
col[j].remove(c);
cell[3 * (i / 3) + j / 3].remove(c);
}
}
}
return false;
}
}
}
return true;
}
public boolean isValidSudoku(char[][] board, int i, int j, char c,
HashSet[] row, HashSet[] col, HashSet[] cell) {
if (row[i].contains(c) || col[j].contains(c)
|| cell[3 * (i / 3) + j / 3].contains(c))
return false;
return true;
}
public void initHashSet(char[][] board, HashSet[] row,
HashSet[] col, HashSet[] cell) {
for (int i = 0; i < 9; i++) {
row[i] = new HashSet<Character>();
col[i] = new HashSet<Character>();
cell[i] = new HashSet<Character>();
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
row[i].add(board[i][j]);
col[j].add(board[i][j]);
cell[3 * (i / 3) + j / 3].add(board[i][j]);
}
}
}
}
}
Java [leetcode 37]Sudoku Solver的更多相关文章
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- leetcode 37 Sudoku Solver java
求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- LeetCode 37 Sudoku Solver(求解数独)
题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独 ...
- [leetcode 37]sudoku solver
1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循 ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
随机推荐
- Linux进程间通信IPC学习笔记之管道
基础知识: 管道是最初的Unix IPC形式,可追溯到1973年的Unix第3版.使用其应注意两点: 1)没有名字: 2)用于共同祖先间的进程通信: 3)读写操作用read和write函数 #incl ...
- main函数的正确格式
main函数称之为主函数,一个C程序总是从main()函数开始执行的.在关于C语言的网贴和图书中,可以看到main函数的多种格式,这些格式,有的是正确的,有的是不正确的,为了避免错误,现归纳整理如下. ...
- SQL*Loader使用详解(一)
1.SQL*Loader介绍 1)SQL*Loader是一个从外部文件指加载数据到Oracle数据库的工具.语法类似于DB2的Load语法,但SQL*Loader支持各种load格式.选择性load和 ...
- easy ui 下拉框绑定数据select控件
easy ui 中的下拉框控件叫做select,具体代码如下: html代码:①.这是一个公司等级的下拉框 <tr> <td>公司等级:</td> <td&g ...
- 在虚拟中开启Windows 8.1的Hyper-V平台
VM安装windows8开启Hype-V 今天老魏用VM安装了Windows8.1系统,想用此系统来开发一下Windows Phone8,但是要求确实要开启Hyper-V平台技术,本来是没有任何的问题 ...
- HDU 3339 In Action 最短路+01背包
题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- MapperScannerConfigurer(转)
转:http://blog.csdn.net/ranmudaofa/article/details/8508028 原文:http://www.cnblogs.com/daxin/p/3545040. ...
- sql不重复的查找统计数据(经典)
例表如下: 表名:MYTEST TID COL1 COL2 COL3 1 1 A A2 1 ...
- jquery layout学习
1.官网:http://layout.jquery-dev.com/index.cfm 2.博客园:http://www.cnblogs.com/chen-fan/articles/2044556.h ...
- aop aspect
所以“<aop:aspect>”实际上是定义横切逻辑,就是在连接点上做什么,“<aop:advisor>”则定义了在哪些连接点应用什么<aop:aspect>.Sp ...