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.

解题思路:

经典的NP问题,采用Dancing Links可以优化算法,参考链接:https://www.ocf.berkeley.edu/~jchu/publicportal/sudoku/sudoku.paper.html

本题方法多多,优化算法也是多多,本例仅给出最简单的DFS暴力枚举算法。

JAVA实现如下:

static public void solveSudoku(char[][] board) {
int count = 0;
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[0].length; j++)
if (board[i][j] == '.')
count++;
dfs(board, count);
} public static int dfs(char[][] board, int count) {
if (count == 0)
return 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == '.') {
for (int k = 1; k <= 10; k++) {
if (k == 10)
return count;
board[i][j] = (char) ('0' + k);
if (!isValid(board, i, j))
board[i][j] = '.';
else {
count--;
count = dfs(board, count);
if (count == 0)
return count;
count++;
board[i][j] = '.';
}
}
}
}
}
return count;
} static public boolean isValid(char[][] board, int row, int col) {
HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
for (int j = 0; j < board[0].length; j++) {
if (board[row][j] != '.') {
if (hashmap.containsKey(board[row][j]))
return false;
hashmap.put(board[row][j], 1);
}
} hashmap = new HashMap<Character, Integer>();
for (int i = 0; i < board.length; i++) {
if (board[i][col] != '.') {
if (hashmap.containsKey(board[i][col]))
return false;
hashmap.put(board[i][col], 1);
}
} hashmap = new HashMap<Character, Integer>();
int rowTemp = (row / 3) * 3;
int colTemp = (col / 3) * 3; for (int k = 0; k < 9; k++) {
if (board[rowTemp + k / 3][colTemp + k % 3] != '.') {
if (hashmap
.containsKey(board[rowTemp + k / 3][colTemp + k % 3]))
return false;
hashmap.put(board[rowTemp + k / 3][colTemp + k % 3], 1);
}
}
return true;
}

Java for LeetCode 037 Sudoku Solver的更多相关文章

  1. LeetCode 037 Sudoku Solver

    题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...

  2. 【leetcode】Sudoku Solver

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

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

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

  4. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  5. leetcode 37 Sudoku Solver java

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

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

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

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

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

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

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

  9. 037 Sudoku Solver 解数独

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

随机推荐

  1. js常用插件

    1.jQuery Shortcuts 是个超轻量级的方法,使用 jQuery 来绑定快捷键(热键). 2.Underscore封装了常用的JavaScript对象操作方法,用于提高开发效率. 3.Kn ...

  2. CVE: 2014-6271、CVE: 2014-7169 PATCH方案分析

    目录 . RedHat官方给的PATCH第一套方案 . RedHat官方给的PATCH临时方案 . RedHat官方给的PATCH第二套方案 1. RedHat官方给的PATCH第一套方案 0x1: ...

  3. Linux X Window System运行原理和启动过程

    本文主要说明X Window System的基本运行原理,其启动过程,及常见的跨网络运行X Window System. 一) 基本运行原理 X Window System采用C/S结构,但和我们常见 ...

  4. C# 抓取网页Html源码 (网络爬虫)

    http://www.cnblogs.com/wxxian001/archive/2011/09/07/2169519.html 刚刚完成一个简单的网络爬虫,因为在做的时候在网上像无头苍蝇一样找资料. ...

  5. android经典实战项目视频教程下载

    注:这是一篇转载的文章,原文具体链接地址找不到了,将原文分享如下,希望能对看到的朋友有所帮助! 最近在学习android应用方面的技术,自己在网上搜集了一些实战项目的资料,感觉挺好的,发布出来跟大伙分 ...

  6. std::thread

    std::shared_ptr<std::thread> m_spThread; m_spThread.reset(new std::thread(std::bind(&GameS ...

  7. 初学JDBC,JDBC工具类的简单封装

    //工具类不需要被继承 public final class JdbcUtils{ //封装数据库连接参数,便于后期更改参数值 private static String url="jdbc ...

  8. mysql zip 版本配置方法

    -\bin 指 C:\Program Files\MySQL\MySQL Server 5.6\bin 1.增加环境变量 "PATH"-"-\bin" 2.修改 ...

  9. 微信公众平台项目中遇到的小问题40016,Invalid button size

    刚辞职的同事用JAVA给客户开发的微信公众平台,今天晚上客户给我打电话说出现错误,此时我正跟朋友在外吃饭,联系已辞职的同事也联系不上,便答应回去之后我给调试看下. 问明客户说就修改了appkey和ap ...

  10. 数据库的模糊查询mybatis

    <!-- oracle --> <select id="searchUserBySearchName" parameterType="java.lang ...