LeetCode 37题:解数独

题目

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

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

  1. 数字 1-9 在每一行只能出现一次。
  2. 数字 1-9 在每一列只能出现一次。
  3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)

数独部分空格内已填入了数字,空白格用 '.' 表示。

示例 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"]]
解释:输入的数独如上图所示,唯一有效的解决方案如下所示:

提示:

  • board.length == 9
  • board[i].length == 9
  • board[i][j] 是一位数字或者 '.'
  • 题目数据 保证 输入数独仅有一个解

代码

可以参考前身:有效的数独

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>void solveSudoku(char **board, int boardSize, int *boardColSize);
bool isValidSudoku(char **board, int boardSize, int *boardColSize);int main()
{// char b[9][10] = //     {"53..7....",//      "6..195...",//      ".98....6.",//      "8...6...3",//      "4..8.3..1",//      "7...2...6",//      ".6....28.",//      "...419..5",//      "....8..79"};char b[9][10]={"..9748...","7........",".2.1.9...","..7...24.",".64.1.59.",".98...3..","...8.3.2.","........6","...2759.."};int t, *te;char **board = (char **)malloc(sizeof(char *) * 9);for (int i = 0; i < 9; i++){board[i] = b[i];printf("%s ", b[i]);}printf("\n");solveSudoku(board, t, te);for (int i = 0; i < 9; i++){printf("%s ", board[i]);}return 0;
}void solveSudoku(char **board, int boardSize, int *boardColSize)
{int r, c;for (int p = 0; p < 9; p++){for (int q = 0; q < 9; q++){if (board[p][q] == '.'){r = p;c = q;continue;}}}static int sign = 0;for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){if (board[i][j] == '.'){int p;for (p = 1; p <= 9 && sign == 0; p++){board[i][j] = p + '0';if (isValidSudoku(board, boardSize, boardColSize) == 1){if (i == r && j == c){sign = 1;}solveSudoku(board, boardSize, boardColSize);if (sign == 1){return;}}else{sign = 0;continue;}}if (p == 10){board[i][j] = '.';}return;}}}for (int i = 0; i < 9; i++){printf("%s ", board[i]);}printf("\n");
}bool isValidSudoku(char **board, int boardSize, int *boardColSize)
{int rownums[10], colnums[10];memset(rownums, 0, sizeof(rownums));memset(colnums, 0, sizeof(colnums));for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){if (board[i][j] != '.'){int number = board[i][j] - '0';if (rownums[number] == 0){rownums[number] = 1;}elsereturn false;}if (board[j][i] != '.'){int number = board[j][i] - '0';if (colnums[number] == 0){colnums[number] = 1;}elsereturn false;}}memset(rownums, 0, sizeof(rownums));memset(colnums, 0, sizeof(colnums));}int i = 0, j = 0;for (int p = 3; p <= 9; p = p + 3){for (int q = 3; q <= 9; q = q + 3){i = p - 3;for (; i < p; i++){j = q - 3;for (; j < q; j++){if (board[i][j] != '.'){int number = board[i][j] - '0';if (rownums[number] == 0){rownums[number] = 1;}elsereturn false;}}}memset(rownums, 0, sizeof(rownums));}}return true;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/68398.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue2中使用mixins(混入)和vue3中使用composable

文章目录 一、mixins混入1、 新建mixins文件夹&#xff0c;新建myMixins.js2、myMixins.js 文件3、index.vue 文件&#xff08;要使用的文件&#xff09; mixins 总结二、composable(组合式api composition )1、 新建composables文件夹&#xff0c;新建useEdit.js2、useEdit.js…

AMS的起源,SystemServer的前世今生

作者&#xff1a;OpenGL 前世 SystemServer的创建还得追溯到之前的Zygote进程中的ZygoteInit代码中。这里的Zygote.forkSystemServer()方法就是实现分裂的关键代码。它内部调用了native方法实现了进程分裂。 执行完这行代码之后。我们的系统中就会出现两个一模一样的进程&…

对任意类型数都可以排序的函数:qsort函数

之前我们学习过冒泡排序&#xff1a; int main() {int arr[] { 9,7,8,6,5,4,3,2,1,0 };int sz sizeof(arr)/sizeof(arr[0]);int i 0;for (i 0; i < sz-1; i) {int j 0;for (j 0; j < sz-1-i; j) {if (arr[j] > arr[j 1]){int temp 0;temp arr[j];arr[j] ar…

Java通过文件流和文件地址下载文件

通过文件流下载文件 如何使用 MultipartFile 进行文件上传、下载到本地&#xff0c;并返回保存路径呢&#xff1a; import org.springframework.web.multipart.MultipartFile;import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOExcep…

laravel框架中批量更新数据

在php框架中 tp中就有批量更新封装好的 SaveAll 在laravel中有批量插入没有批量更新操作;因此我们可以自己去封装一个 然后批量进行更新操作 封装参考代码: /*** 批量更新** param $tableName 表名称* param string $pk 更新的字段* param array $multipleData 要更新的数据*…

多智能体共识算法的粗略数学证明

这篇文章是对论文《Consensus and Cooperation in Networked Multi-Agent Systems》中定理一的粗略数学证明。 论文中的定理一&#xff1a; 对一个由 n 个智能体以拓扑结构 G 组成的网络&#xff0c;使用以下共识算法&#xff1a; x ˙ i ( t ) Σ j ∈ N i a i j ( x j ( t…

【BEV】3D视觉 PRELIMINARY

这里的知识来自于论文 Delving into the Devils of Bird’s-eye-view Perception: A Review, Evaluation and Recipe 的 Appendix B.1 部分来自 这篇文章 从透视图转向鸟瞰图。&#xff08;Xw、Yw、Zw&#xff09;、&#xff08;Xc、Yc、Zc&#xff09;表示世界World坐标和相…

React - useEffect函数的理解和使用

文章目录 一&#xff0c;useEffect描述二&#xff0c;它的执行时机三&#xff0c;useEffect分情况使用1&#xff0c;不写第二个参数 说明监测所有state&#xff0c;其中一个变化就会触发此函数2&#xff0c;第二个参数如果是[]空数组&#xff0c;说明谁也不监测3&#xff0c;第…

Word 2019打开.doc文档后图片和公式不显示(呈现为白框)的解决办法

Word 2019打开.doc文档后图片和公式不显示&#xff08;呈现为白框&#xff09;的解决办法 目录 Word 2019打开.doc文档后图片和公式不显示&#xff08;呈现为白框&#xff09;的解决办法一、问题描述二、解决方法1.打开 WORD 2019&#xff0c;点击菜单中的“文件”&#xff1b;…

无服务器架构发布啦!

导读Serverless 1.15.2 已发布。The Serverless Framework (无服务器架构&#xff09;允许你自动扩展、按执行付费、将事件驱动的功能部署到任何云。 目前支持 AWS Lambda、Apache OpenWhisk、Microsoft Azure&#xff0c;并且正在扩展以支持其他云提供商。 Serverless 降低了…

iOS-砸壳篇(两种砸壳方式)

CrackerXI砸壳呢&#xff0c;当时你要是使用 frida-ios-dump 也是可以的&#xff1b; https://github.com/AloneMonkey/frida-ios-dump frida-ios-dump: 代码中需要更改的&#xff1a;手机中的内网ip 密码 等 最后放到我的砸壳路径里&#xff1a; python dump.py -l查看应用…

uniapp 自定义手机顶部状态栏不生效问题

想要的效果想淘宝一样&#xff0c;底色覆盖到手机顶部&#xff0c;找了两天都没找到原因&#xff0c;过程很艰苦&#xff0c;直接上结果吧 项目是后来接手的&#xff0c;最终原因出在这&#xff0c; "immersed" : false>设置为 true 就可以了&#xff0c;沉浸式样…