LeetCode 37. Sudoku Solver II 解数独 (C++/Java)
题目:
A sudoku solution must satisfy all of the following rules:
- Each of the digits
1-9must occur exactly once in each row. - Each of the digits
1-9must occur exactly once in each column. - Each of the the digits
1-9must occur exactly once in each of the 93x3sub-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-9and 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)的更多相关文章
- LeetCode 37 Sudoku Solver(求解数独)
题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独 ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [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 ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- [leetcode 37]sudoku solver
1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循 ...
- leetcode 37 Sudoku Solver java
求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution ...
- [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 ...
随机推荐
- 【Oracle】 管道函数pipelined function简单的使用
Oracle 管道函数pipelined function简单的使用 如果在函数(function)中加关键字 pipelined,就表明这是一个oracle管道函数,其返回值类型必为 集合,体现出来 ...
- Flink on Zeppelin 流计算处理最佳实践
简介: 欢迎钉钉扫描文章底部二维码进入 EMR Studio 用户交流群 直接和讲师交流讨论~ 点击以下链接直接观看直播回放:https://developer.aliyun.com/live/247 ...
- 日志审计携手DDoS防护助力云上安全
简介: 本文主要介绍日志审计结合DDoS防护保障云上业务安全的新实践. 日志审计携手DDoS防护助力云上安全 1 背景介绍 设想一下,此时你正在高速公路上开车去上班,路上还有其他汽车,总体而言,大家 ...
- dotnet 在 UOS 国产系统上安装 dotnet sdk 的方法
本文告诉大家如何在 UOS 国产系统上安装 dotnet sdk 的方法 使用的 UOS 是 UOS 20 x64 版本,这个系统版本是基于 debian 10 的,可以使用 debian 10 的方 ...
- pnpm的基本原理及快速使用
基本原理 前置知识:软件链接与硬链接 软链接(符号链接Symbolic link):是一类特殊的文件, 其包含有一条以绝对路径或者相对路径的形式指向其它文件或者目录的引用.在window快捷方式上和其 ...
- Vue3 和 Vue2 的异同及开发中具体区别
目录 总体来说 性能提升 树摇(Tree shaking) 碎片化节点(Fragment) 传送门 (Teleport) Suspense 更好的TypeScript支持 Composition AP ...
- K8s控制器(8)---Deployment
一.Deployment控制器概念.原理解读 1.1 Deployment概述 # Deployment官方文档 https://kubernetes.io/docs/concepts/workloa ...
- java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider
解决错误: java.lang.NoSuchMethodException: tk.mybatis.mapper.provider.base.BaseSelectProvider 整合一遍通用mapp ...
- Ubuntu 上安装 Docker
步骤 1:删除任何现有的 Docker 包 但在跳到安装部分之前,有必要删除所有以前安装的 Docker. 要 卸载以前的 Docker,请使用以下命令. sudo apt remove docker ...
- jeecgboot项目swagger2在线接口转word
1.先找到接口文档地址 2.根据url获取接口数据 3.利用在线工具进行转换生成word 在线工具地址:在线swagger转word文档 生成的word文档如下: