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 ...
随机推荐
- .NET Emit 入门教程:第六部分:IL 指令:9:详解 ILGenerator 指令方法:运算操作指令(指令篇结束)
前言: 经过前面几篇的学习,我们了解到指令的大概分类,如: 参数加载指令,该加载指令以 Ld 开头,将参数加载到栈中,以便于后续执行操作命令. 参数存储指令,其指令以 St 开头,将栈中的数据,存储到 ...
- eclipse 汉化语言包/中文补丁/简中设置/中英互换
eclipse 汉化语言包/中文补丁/简中设置/中英互换 汉化很简单,使用eclipse内置的软件下载就可以,不需要下载压缩包 官方的说明 Open the install wizard with ' ...
- ESXI 6.5 零基础从安装到批量生成/管理虚拟机简易教程
制造U盘安装盘 1 先提前下载好,ESXI 6.5 ISO文件. 2 下载制作U盘安装工具,RUFUS. Rufus非常小巧的绿色EXE文件,默认配置选中ISO文件就可以,点击开始,就自动制作,非常方 ...
- 零信任策略下K8s安全监控最佳实践(K+)
简介: 本文重点将围绕监控防护展开,逐层递进地介绍如何在复杂的分布式容器化环境中借助可观测性平台,持续监控K8s集群,及时发现异常的 API 访问事件.异常流量.异常配置.异常日志等行为,并且结合合理 ...
- Duang,您的钉钉应用已上线!云开发5分钟快速打造钉钉会议室预定系统
简介: 5分钟可以干什么?喝一杯咖啡,回一封邮件,还是开发上线一个钉钉应用.云开发平台联合钉钉开发平台推出0门槛打造你的第一个钉钉应用的活动,完成相应任务后,即可领取精美奖品.春暖花开,领个背包去踏春 ...
- 获国际架构顶会ATC2021最佳论文!Fuxi2.0去中心化的调度架构详解
简介: 近日,在国际体系架构顶会USENIX ATC2021上,阿里云飞天伏羲团队与香港中文大学合作的一篇论文<Scaling Large Production Clusters with Pa ...
- 模仿Spring实现一个类管理容器
简介: 项目的初衷是独立作出一个成熟的有特色的IOC容器,但由于过程参考Spring太多,而且也无法作出太多改进,于是目的变为以此项目作为理解Spring的一个跳板,与网上的一些模仿Spring的框 ...
- [FE] ServerSideRender 加上 PWA 特性的一种处理方式
SSR 和 PWA 这两块分开讲,需要做不少的处理,现在我们有了一种简便的方式来处理它,就是使用 Quasar 框架. Quasar 支持了 SPA.SSR.PWA.Mobile APP.Electr ...
- [Blockchain] 去中心化与互联网分布式的联系与区别
去中心化和传统分布式都是多机应用,这是它们的共同之处,但是背后有着不一样的用途. 我们所理解的传统分布式及其应用可以解决两个问题:冗余备份/扩容 和 并行计算. 而去中心化应用的目的是维护不可逆转数据 ...
- 2019-8-31-dotnet-使用-Environment.FailFast-结束程序
title author date CreateTime categories dotnet 使用 Environment.FailFast 结束程序 lindexi 2019-08-31 16:55 ...