Leetcode 37 解数独

news/2024/11/9 0:49:19/

Leetcode解数独

  • 题目描述
    • 题解1(按Board行列回溯:较直接)

题目描述

编写一个程序,通过填充空格来解决数独问题。

数独的解法需 遵循如下规则:

  1. 数字 1-9 在每一行只能出现一次
  2. 数字 1-9 在每一列只能出现一次
  3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
    数独部分空格内已填入了数字,空白格用 ‘.’ 表示。

来源:力扣(LeetCode)题目链接
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

示例1
在这里插入图片描述
输入:

board = [["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]]
输出:[["5","3","4","6","7","8","9","1","2"],
["6","7","2","1","9","5","3","4","8"],[
"1","9","8","3","4","2","5","6","7"],
["8","5","9","7","6","1","4","2","3"],
["4","2","6","8","5","3","7","9","1"],
["7","1","3","9","2","4","8","5","6"],
["9","6","1","5","3","7","2","8","4"],
["2","8","7","4","1","9","6","3","5"],
["3","4","5","2","8","6","1","7","9"]]

解释:输入的数独如上图所示,唯一有效的解决方案如下所示:
在这里插入图片描述

题解1(按Board行列回溯:较直接)

class Solution {
public:
// 为什么回溯有返回值:因为只需要做一次解,需要及时返回;没有返回值的一般是需要穷举所有的解bool backtrace(vector<vector<char>>& board, int i, int j){if(9 == i){    // 回溯结束条件:以行为准return true;}else if(9 == j){return backtrace(board, i+1, 0);  // i行结束,换行+1}else if('.' != board[i][j]){return backtrace(board, i, j+1);  // 此处有数字了,换列}// 处理节点else{for(char c = '1'; c <= '9'; c++){ // 回溯枚举if(isvaild(board, i, j, c)){board[i][j] = c;if(backtrace(board, i, j+1))return true;  // 完成了一种解,剪枝board[i][j] = '.'; // 还没填完所有的空,回退}}}return false;}void solveSudoku(vector<vector<char>>& board) {backtrace(board, 0, 0);}bool isvaild(vector<vector<char>>& board, int i, int j, char c){for(int k=0; k<9; k++){// 同行if(c == board[i][k])return false;// 同列if(c == board[k][j])return false;// subboard:行/3 列/3 是找组别信息// 规律可循:一个subboard里,行列变化不同,行变一次,列变3次:// 所以一个除法,一个取余if(c == board[(i/3)*3+k/3][(j/3)*3+k%3])return false;}return true;}
};

在这里插入图片描述


http://www.ppmy.cn/news/53039.html

相关文章

Redis 主从复制

一、主从复制 1.简介 主从库之间采用读写分离的方式 读操作: 主库、从库都可以处理 写操作&#xff1a;首先写到主库执行&#xff0c;然后再将主库同步给从库。 实现读写分离&#xff0c;性能扩展 容灾快速恢复 2 主从复制步骤 创建一个目录 ,在root下创建一个myredis的目录…

销量破亿,董洁直播间凭何出圈?

近期&#xff0c;“没有五位数走不出”的董洁直播间火了…… 纵观这几年各大平台直播带货&#xff0c;火已不是什么新鲜事&#xff0c;而为何董洁的直播能火上热搜呢&#xff1f;本期&#xff0c;千瓜将解析董洁直播&#xff0c;同品牌方聊聊小红书直播的那些事儿。董洁「种草式…

RSA加解密三方调用

RSA三方调用 github项目demo 整体项目结构 ├─ExposedInterface 服务提供方的单独接口依赖包&#xff08;里面只有实体和暴露接口&#xff09; 根据服务提供者每次打包release 正式版本包 │ ├─src │ │ ├─main │ │ │ ├─java │ │ │ │ └─com │ │ │ │ └…

【Python】【进阶篇】7、Django模板系统

目录 7、Django模板系统1. Django的模板系统2. 模板系统的应用1) 模板传参2) render方法 7、Django模板系统 本节我们继续使用《Django视图函数》一节中的“Hello_my_django”函数来完成相关知识的讲解。 from django.http import HttpResponse def Hello_my_django(request)…

【mmdeploy】【TODO】使用mmdeploy将mmdetection模型转tensorrt

mmdetection转换 文章目录 mmdetection转换mmdetection 自带转换ONNX——无法测试使用mmdeploy(0.6.0)使用mmdeploy转onnx使用mmdeploy直接转tensorRT调试记录 先上结论&#xff1a;作者最后是转tensorrt的小图才成功的&#xff0c;大图一直不行。文章仅作者自我记录使用&#…

四、MyBatis获取参数值的两种方式(重点)

文章目录 四、MyBatis获取参数值的两种方式&#xff08;重点&#xff09;4.1 单个字面量类型的参数4.2 多个字面量类型的参数4.3 map集合类型的参数4.4 实体类类型的参数4.5 使用Param标识参数 四、MyBatis获取参数值的两种方式&#xff08;重点&#xff09; MyBatis获取参数值…

【无标题】c++异常机制的一些总结以及思考

在谈及c处理异常机制的方法之前我们不妨来回顾一下c语言是如何应对这块的。 终止程序&#xff0c;如assert&#xff0c;缺陷&#xff1a;用户难以接受。如发生内存错误&#xff0c;除0错误时就会终止程序。 返回错误码&#xff0c;缺陷&#xff1a;需要程序员自己去查找对应的…

Java API操作Jenkins和自定义Java构建镜像

文章目录 1、Java操作Jenkins1.1、Jenkins API支持格式json和xml1.2、Java API操作 2、自定义Java构建镜像 1、Java操作Jenkins 1.1、Jenkins API支持格式json和xml # json格式&#xff0c;{host}/api/json http://192.168.245.139:8080/api/json # 视图接口&#xff0c;json…