题目:

Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3sub-boxes of the grid.

Empty cells are indicated by the character '.'.


A sudoku puzzle...


...and its solution numbers marked in red.

Note:

  • The given board contain only digits 1-9 and the character '.'.
  • You may assume that the given Sudoku puzzle will have a single unique solution.
  • The given board size is always 9x9.

分析:

数独应该很多人都有玩过,其规则是每列,每行,每个九宫格数字都是1-9,且没有重复的。

那么这道题的解法就是在数独中依次添加1-9,同时判断是否符合条件,如果不符合就回溯,符合条件就在下个格子填数字。可以维护几个数组,用来标记哪行哪列哪个九宫格中的数字是否被使用过。

程序:

C++

class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
cols = vector<vector<int>>(9, vector<int>(10, 0));
rows = vector<vector<int>>(9, vector<int>(10, 0));
//boxs index
// 0, 1, 2
// 3, 4, 5
// 6, 7, 8
boxs = vector<vector<int>>(9, vector<int>(10, 0));
for(int i = 0; i < board.size(); ++i){
for(int j = 0; j < board[i].size(); ++j){
if(board[i][j] != '.'){
int k = board[i][j] - '0';
rows[i][k] = 1;
cols[j][k] = 1;
int bx = i / 3;
int by = j / 3;
boxs[bx + by * 3][k] = 1;
}
}
}
dfs(board, 0, 0);
}
private:
vector<vector<int>> cols, rows, boxs;
bool dfs(vector<vector<char>>& board, int r, int c){
if(r == 9)
return true;
int nextR = r;
int nextC = (c + 1) % 9;
if(nextC == 0)
nextR = r + 1;
if(board[r][c] != '.')
return dfs(board, nextR, nextC);
for(int i = 1; i <= 9; ++i){
int bx = r / 3;
int by = c / 3;
int boxIndex = bx + by * 3;
if(!rows[r][i] && !cols[c][i] && !boxs[boxIndex][i]){
rows[r][i] = 1;
cols[c][i] = 1;
boxs[boxIndex][i] = 1;
board[r][c] = i + '0';
if(dfs(board, nextR, nextC))
return true;
rows[r][i] = 0;
cols[c][i] = 0;
boxs[boxIndex][i] = 0;
board[r][c] = '.';
}
}
return false;
}
};

Java

class Solution {
public void solveSudoku(char[][] board) {
rows = new int[9][10];
cols = new int[9][10];
boxs = new int[9][10];
for(int i = 0; i < board.length; ++i){
for(int j = 0; j < board[i].length; ++j){
if(board[i][j] != '.'){
int k = board[i][j] - '0';
rows[i][k] = 1;
cols[j][k] = 1;
int bx = i / 3;
int by = j / 3;
boxs[bx + by * 3][k] = 1;
}
}
}
dfs(board, 0, 0);
}
private boolean dfs(char[][] board, int r, int c){
if(r == 9)
return true;
int nextR = r;
int nextC = (c + 1) % 9;
if(nextC == 0)
nextR = r + 1;
if(board[r][c] != '.')
return dfs(board, nextR, nextC);
for(int i = 1; i <= 9; ++i){
int bx = r / 3;
int by = c / 3;
int boxIndex = bx + by * 3;
if(rows[r][i] == 0 && cols[c][i] == 0 && boxs[boxIndex][i] == 0){
rows[r][i] = 1;
cols[c][i] = 1;
boxs[boxIndex][i] = 1;
board[r][c] = (char)(i + '0');
if(dfs(board, nextR, nextC))
return true;
rows[r][i] = 0;
cols[c][i] = 0;
boxs[boxIndex][i] = 0;
board[r][c] = '.';
}
}
return false;
}
private int[][] rows;
private int[][] cols;
private int[][] boxs;
}

LeetCode 37. Sudoku Solver II 解数独 (C++/Java)的更多相关文章

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

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

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

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

  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. Java [leetcode 37]Sudoku Solver

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

  6. [leetcode 37]sudoku solver

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

  7. leetcode 37 Sudoku Solver java

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

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

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

  9. 【LeetCode】37. Sudoku Solver

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

  10. 【leetcode】Sudoku Solver

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

随机推荐

  1. 力扣69(java&python)-x的平方根(简单)

    题目: 给你一个非负整数 x ,计算并返回 x 的 算术平方根 . 由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 . 注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0. ...

  2. 如何可视化编写和编排你的 K8s 任务

    简介: 通过任务调度 SchedulerX 来调度你的 K8s 任务,能够降低学习成本,加快开发效率,让你的任务失败可报警,出问题可排查,打造云原生可观测体系下的可视化 K8s 任务. 作者:学仁   ...

  3. WPF 使用 Dispatcher 的 InvokeAsync 和 BeginInvoke 的异常处理差别

    一般认为 WPF 的 Dispatcher 的 InvokeAsync 方法是 BeginInvoke 方法的平替方法和升级版,接近在任何情况下都应该在业务层使用 InvokeAsync 方法代替 B ...

  4. 他又来了,.net开源智能家居之小米米家的c#原生sdk【MiHome.Net】1.0.0发布,快来打造你的私人智能家居吧

    背景介绍 hi 大家好,我是三合,作为一个非著名懒人,智能家居简直刚需,在上一篇文章他来了他来了,.net开源智能家居之苹果HomeKit的c#原生sdk[Homekit.Net]1.0.0发布,快来 ...

  5. Dubbo SPI-Wrapper

    前言 在Dubbo SPI中是通过Wrapper实现AOP,对于AOP相信大家都不陌生,这里不做的过多的介绍,我们主要来了解Dubbo SPI中是如何使用Wrapper类以及实现的细节. 使用场景 D ...

  6. 基于权电阻网络的VGA色条显示#DE10-lite#verilog#qp

  7. [2]自定义Lua解析方式

    [2]自定义Lua解析方式 在上文中我们学会学会更改加载路径,加载对应文件夹下的Lua脚本. 默认解析加载的lua脚本存在的文件位置非AB包或者Resources文件夹下往往不能随包体更新,这显然不符 ...

  8. 检索增强生成(RAG)实践:基于LlamaIndex和Qwen1.5搭建智能问答系统

    检索增强生成(RAG)实践:基于LlamaIndex和Qwen1.5搭建智能问答系统 什么是 RAG LLM 会产生误导性的 "幻觉",依赖的信息可能过时,处理特定知识时效率不高, ...

  9. ansible(6)--ansible的copy和fetch模块

    1. copy模块 功能:从 ansible 服务端主控端复制文件到远程主机: copy模块的主要参数如下: 参数 说明 src 复制的源文件路径,若源文件为目录,默认进行递归复制,如果路劲以&quo ...

  10. 在jeecg-boot中密码的使用

    1.生成密码并入库保存 String id= SnowflakeIdUtil.nextValue();//生成id operatCompany.setId(id); String salt = oCo ...