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 ...
随机推荐
- uniapp小程序页面实现元素与胶囊进行居中对齐
无论是否为uni,关键在于获取胶囊中点的位置,如果是原生小程序根据小程序文档获取,其余逻辑处理是一致的 代码语法都只是技术选择,重点是逻辑处理,对于技术的运用,代码技术好比是积木,好的程序就是好的组合 ...
- Understand Abstraction and Interface
Foreword 抽象和接口是Java中的两个关键字,也是两种最基本的优化软件项目手段.为什么说它们是一种优化项目的手段? 人分三六九等,不同等级的人,所接触的事和处理的事是不一样的.同理,项目也分大 ...
- iLogtail使用入门-iLogtail本地配置模式部署(For Kafka Flusher)
简介:iLogtail使用入门-iLogtail本地配置模式部署(For Kafka Flusher). 阿里已经正式开源了可观测数据采集器iLogtail.作为阿里内部可观测数据采集的基础设施,i ...
- KubeVela + KEDA:为应用带来“与生俱来”的弹性伸缩能力
简介: 在这篇博文中,我们将简要解释需要考虑的领域,KEDA 如何使应用自动伸缩变得简单,以及为什么阿里云企业分布式应用服务(EDAS)在 KEDA 上完全标准化. 联合作者 | Yan Xun,阿里 ...
- [GPT] vue 的 quasar 框架 在 layout 模版中 如何获取 子页面当前使用的 useMeta
在 Quasar 框架中,用 Vue Router 的 meta 字段来获取子页面当前使用的 useMeta . 首先,您需要在路由配置中设置子页面的 meta 字段.例如: const rout ...
- Oracle "脑残" CBO 优化案例
今天晚上下班回来才有空看群,群友发了一条很简单的慢SQL问怎么优化. 非常简单,我自己模拟的数据. 表结构: -- auto-generated definition CREATE TABLE HHH ...
- vue-axios设置公共的请求ip
1.安装axios,网上找方法 2.src->network->request.js并复制: import axios from 'axios' export function reque ...
- Spirng 当中 Bean的作用域
Spirng 当中 Bean的作用域 @ 目录 Spirng 当中 Bean的作用域 每博一文案 1. Spring6 当中的 Bean的作用域 1.2 singleton 默认 1.3 protot ...
- LLM基础能力实现-书生浦语大模型实战营学习笔记2&大语言模型4
大语言模型-4.LLM基础能力实现 书生浦语大模型实战营学习笔记-2.LLM基础能力实现 本文包括第二期实战营的第2课内容.本来是想在笔记中给官方教程做做补充的,没想到官方教程的质量还是相当高的,跟着 ...
- FFmpeg开发笔记(二十)Linux环境给FFmpeg集成AVS3解码器
AVS3是中国AVS工作组制定的第三代音视频编解码技术标准,也是全球首个已推出的面向8K及5G产业应用的视频编码标准.AVS工作组于2019年3月9日完成第三代AVS视频标准(AVS3)基准档次的制 ...