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 ...
随机推荐
- 零信任策略下K8s安全监控最佳实践(K+)
简介: 本文重点将围绕监控防护展开,逐层递进地介绍如何在复杂的分布式容器化环境中借助可观测性平台,持续监控K8s集群,及时发现异常的 API 访问事件.异常流量.异常配置.异常日志等行为,并且结合合理 ...
- Serverless 时代下微服务应用全托管解决方案
简介: 本文介绍了 Serverless 时代下微服务的发展以及过程中遇到的相对较复杂的需求,面对这些,阿里云 Serverless 应用引擎 SAE 将"Serverless"的 ...
- 深度解读 OpenYurt:从边缘自治看 YurtHub 的扩展能力
作者 | 新胜 阿里云技术专家 导读:OpenYurt 开源两周以来,以非侵入式的架构设计融合云原生和边缘计算两大领域,引起了不少行业内同学的关注.阿里云推出开源项目 OpenYurt,一方面是把阿 ...
- 双龙贺岁,龙蜥 LoongArch GA 版正式发布
简介:Anolis OS 8.4 LoongArch 正式版发布产品包括 ISO.软件仓库.虚拟机镜像.容器镜像. 简介 继 Anolis OS LoongArch 预览版发布后,现迎来龙蜥 ...
- LlamaIndex 安装与配置(不含OpenAI)
pip install llama-index 这是一个包含以下组件的启动包: llama-index-core llama-index-legacy (暂时包含) llama-index-llms- ...
- [Blockchain] 前后端完全去中心化的思路, IPFS 与 Ethereum Contract
我们在使用智能合约的时候,一般是把它当成去中心.减少信任依赖的后端存在. 如果没有特殊后端功能要求,一个 DApp 只需要前端驱动 web3js 就可以实现了. 可以看到,现在前端部分依旧是一个中心化 ...
- dotnet 8 破坏性改动 在 AssemblyInformationalVersionAttribute 添加上 git 的 commit 号
我在一个 WPF 项目里面,在界面显示应用的版本号,更新到 dotnet 8 的 SDK 之后,发现我的界面布局损坏了.本质上这个破坏性改动和 WPF 没有什么关系,是 dotnet 的 SDK 或编 ...
- C++指针与引用(Pointers OR References)
一.Pointers Pointer是指针,可以用来指向任何一个objects,包括一般变量: 1 int i = 3; 2 int * pi = &i; 3 cout << pi ...
- 对C语言符号的一些冷门知识运用的剖析和总结
符号 目录 符号 注释 奇怪的注释 C风格的注释无法嵌套 一些特殊的注释 注释的规则建议 反斜杠'\' 反斜杠有续行的作用,但要注意续行后不能添加空格 回车也能起到换行的作用,那续行符的意义在哪? 反 ...
- 虚拟机中安装mysql 完整教程( CentOS7 版本)
一.检查是否安装了Mysql Yum检查 yum list installed | grep mysql 安装则直接删除 yum remove mysql-community-client.x86_6 ...