问题描述:填充数独表中空元素。空元素为'.'

算法分析:没填充一个数,都要看这个数所在的行,列,小矩阵是否合法。然后还要看整个数独表是否正确,而判断整个数独表只能通过递归,因为前一个结果的判断要依赖后一个结果。这应该属于动态规划问题。要递归回溯。

public void solveSudoku(char[][] board) {
solve(board);
} public boolean solve(char[][] board)
{
for(int i = 0; i < 9; i ++)
{
for(int j = 0; j < 9; j ++)
{
if(board[i][j] == '.')
{
for(int k = 1; k <= 9; k ++)
{
board[i][j] = (char) (k+'0');
if(isValid(board, i, j) && solve(board))//当前有多个选择要递归回溯
{
return true;
}
board[i][j] = '.';
}
return false;
}
}
}
return true;
} public boolean isValid(char[][] board, int i ,int j)
{
HashSet<Character> set = new HashSet<>();
for(int k = 0; k < 9; k ++)
{
if(set.contains(board[i][k]))
{
return false;
}
if(board[i][k]!='.')
{
set.add(board[i][k]);
}
}
set.clear();
for(int k = 0; k < 9; k ++)
{
if(set.contains(board[k][j]))
{
return false;
}
if(board[k][j]!='.')
{
set.add(board[k][j]);
}
}
set.clear();
for(int m = 0; m < 3; m ++)
{
for(int n = 0; n < 3; n ++)
{
int x = i/3*3+m;
int y = j/3*3+n;
if(set.contains(board[x][y]))
{
return false;
}
if(board[x][y]!='.')
{
set.add(board[x][y]);
}
}
}
return true;
}

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. [LeetCode] Sudoku Solver 求解数独

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

  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] Sudoku Solver 解数独,递归,回溯

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

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

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

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

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

  7. 037 Sudoku Solver 解数独

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

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

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

  9. Leetcode之回溯法专题-37. 解数独(Sudoku Solver)

    Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...

随机推荐

  1. 【Double】double精度问题和int、long除不尽取舍问题

    看了老半天,真心没搞懂,留下几篇文章,后面继续跟进吧.... 一.如何理解double精度丢失问题? - 知乎 https://www.zhihu.com/question/42024389/answ ...

  2. Java版接口自动化--初稿

    一.接口参数的获取: 1.参数通过Excel读取,并将结果写入Excel中 package org.fanqi.operateExcel; import java.io.FileInputStream ...

  3. 关于jQuery中nth-child和nth-of-type的详解

    首先贴出来HTML的代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  4. caffe自定义layer

    caffe自带layers: http://caffe.berkeleyvision.org/tutorial/layers.html Layers: Image Data - read raw im ...

  5. 008-shiro与spring web项目整合【二】认证、授权、session管理

    一.认证 1.添加凭证匹配器 添加凭证匹配器实现md5加密校验. 修改applicationContext-shiro.xml: <!-- realm --> <bean id=&q ...

  6. Angular学习笔记—基础(转载)

    创建简单组件 新建组件 $ ng generate component simple-form --inline-template --inline-style # Or $ ng g c simpl ...

  7. MariaDB日志

    1.查询日志:一般来说不开开启(会产生额外压力,并且不一定有价值),query log 记录查询操作:可以记录到文件(file)中也可记录到表(table)中 general_log=ON|OFF g ...

  8. python-绘图matplotlib

    <Python编程:从入门到实践>读书笔记 1.使用plot()绘制简单的折线图 import matplotlib.pyplot as plt va=[1,2,3,4,5] sq=[1, ...

  9. python中math常用函数

    python中math的使用 import math #先导入math包 1 三角函数 print math.pi #打印pi的值 3.14159265359 print math.radians(1 ...

  10. js验证真实姓名

    var regName = /^[\u4e00-\u9fa5]{2,4}$/; if (!regName.test(examinee.name)) { wx.showToast({ title: &q ...