题目描述:

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的更多相关文章

  1. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  2. leetcode 37 Sudoku Solver java

    求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution ...

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

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

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

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

  5. LeetCode 37 Sudoku Solver(求解数独)

    题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description   Problem : 解决数独问题,给出一个二维数组,将这个数独 ...

  6. [leetcode 37]sudoku solver

    1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循 ...

  7. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  8. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  9. 【leetcode】Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

随机推荐

  1. zhuan: ubuntu 安装 apache2

    安装 用  sudo apt-get install apache2 sudo /etc/init.d/apache2 restart 如果发现错误: 以下from:http://cache.baid ...

  2. wpf 实现全屏与取消全屏

    /// <summary> /// 全屏 /// </summary> public void ToFullscreen() { //存储窗体信息 m_WindowState ...

  3. 十八、mysql 内存优化 之 myisam

    .key_buffer 索引块大小 set global hot_cache.key_buffer_size = ; //设置大小 show variables like 'key_buffer_si ...

  4. 【分享】SQL Server优化50法

    虽然查询速度慢的原因很多,但是如果通过一定的优化,也可以使查询问题得到一定程度的解决. 查询速度慢的原因很多,常见如下几种: 没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) I/ ...

  5. linq and rest api in sharepoint

    //1.make sure your application using the .net fromwork 3.5 //2.create entity classes using the instr ...

  6. 响应式菜单(bootstrap)

    <nav class="navbar navbar-default" role="navigation"> <div class=" ...

  7. klayge 4.2.0 编译vc9

    CMake Error at CMakeLists.txt:442 (ADD_PRECOMPILED_HEADER): Unknown CMake command "ADD_PRECOMPI ...

  8. [Firefly引擎][学习笔记三][已完结]所需模块封装

    原地址:http://www.9miao.com/question-15-54671.html 学习笔记一传送门学习笔记二传送门 学习笔记三导读:        笔记三主要就是各个模块的封装了,这里贴 ...

  9. XSS 攻击在它的面前都弱爆了!

    虽然双十一刚刚过去不久,但是对很多工程师来说,连续熬夜加班的「噩梦」似乎还没有过去.尤其是像双十一这种活动,对于电商网站的工程师们来说,他们需要彻夜的加班加点来保障网站的稳定性和安全性.当然,面对上千 ...

  10. C++11新特性:右值引用和转移构造函数

    问题背景 #include <iostream> using namespace std; vector<int> doubleValues (const vector< ...