Leetcode---Sudoku Solver

从光斑图到锤形优化:一次搞定Zemax双胶合透镜的像散与场曲问题 本文详细解析了Zemax双胶合透镜设计中像散与场曲问题的诊断与优化方法。从光斑图分析到锤形优化技巧,提供了完整的光学设计实战指南,帮助设计师快速定位问题并有效改善系统性能,特别适合光学设计工程师和Zemax使用者参考。 阅读详情

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

A sudoku puzzle...


...and its solution numbers marked in red.


Have you been asked this question in an interview?

首先leetcode只要求9*9的输入,所以不可能出现时间和空间不够用的问题。
基本思路是回溯,回溯类似就是DFS,不同的是某层失败后要回到上一层,失败的一层要恢复原样。
另外,回溯也要使用递归,递归需要有收敛条件,就是要有一段code,不会调用自身而返回。

这里的关键点是对于一旦出现解出现的情况,要防止递归函数返回后修改数据,所以要标记解出现的情况,可以用返回值或全局变量标记:

全局变量:

bool flag= false;
bool check(int k, vector<vector<char> > &board){
        int x=k/9;
        int y=k%9;
        for (int i = 0; i < 9; i++)
            if (i != x && board[i][y] == board[x][y])
                return false;
        for (int j = 0; j < 9; j++)
            if (j != y && board[x][j] == board[x][y])
                return false;
        for (int i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)
            for (int j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)
                if (i != x && j != y && board[i][j] == board[x][y])
                    return false;
        return true;
    }
void dfs(int num,vector<vector<char> > &board){
    if(num==81){
        flag=true;
        return;
    }
    else{
        int x=num/9;
        int y=num%9;
        if(board[x][y]=='.'){
            for(int i=1;i<=9;i++){
                board[x][y]=i+'0';
                if(check(num,board)){
                    dfs(num+1,board);
                    if(flag)
                        return;
                }
            }
            board[x][y]='.';
        }
        else{
            dfs(num+1,board);
        }
    }
}
void solveSudoku(vector<vector<char> > &board) {
    dfs(0,board);
}

返回值:

    int n,m;
    bool check(int k, vector<vector<char> > &board){
        int x=k/n;
        int y=k%m;
        for (int i = 0; i < n; i++)
            if (i != x && board[i][y] == board[x][y])
                return false;
        for (int j = 0; j < m; j++)
            if (j != y && board[x][j] == board[x][y])
                return false;
        for (int i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)
            for (int j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)
                if (i != x && j != y && board[i][j] == board[x][y])
                    return false;
        return true;
    }
    bool f(int i, vector<vector<char> > &board){
        if(i==n*m)
            return true;
        if(board[i/n][i%m]=='.'){
            for(int k=1;k<=9;k++){
                board[i/n][i%m]=k+'0';
                    if(check(i,board) && f(i+1,board))
                            return true;
            }
            board[i/n][i%m]='.';
            return false;
        }
        else
            return f(i+1,board);
    }
    void solveSudoku(vector<vector<char> > &board) {
        n=board.size();
        m=board[0].size();
        f(0,board);
    }

上面两个都是得到一个解就返回,还有是可以得到所有解的情况,也就是在解出现的时候存下来。

vector<vector<vector<char> >> sum;
bool check(int k, vector<vector<char> > &board){
        int x=k/9;
        int y=k%9;
        for (int i = 0; i < 9; i++)
            if (i != x && board[i][y] == board[x][y])
                return false;
        for (int j = 0; j < 9; j++)
            if (j != y && board[x][j] == board[x][y])
                return false;
        for (int i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)
            for (int j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)
                if (i != x && j != y && board[i][j] == board[x][y])
                    return false;
        return true;
    }
void dfs(int num,vector<vector<char> > &board){
    if(num==81){
        sum.push_back(board);
        return;
    }
    else{
        int x=num/9;
        int y=num%9;
        if(board[x][y]=='.'){
            for(int i=1;i<=9;i++){
                board[x][y]=i+'0';
                if(check(num,board)){
                    dfs(num+1,board);
                    //if(flag)
                      //  return;
                }
            }
            board[x][y]='.';
        }
        else{
            dfs(num+1,board);
        }
    }
}
void solveSudoku(vector<vector<char> > &board) {
    dfs(0,board);
}
int main()
{
    vector<string> myboard({"...748...","7........",".2.1.9...","..7...24.",".64.1.59.",".98...3..","...8.3.2.","........6","...2759.."});
    vector<char> temp(9,'.');
    vector<vector<char> > board(9,temp);
    for(int i=0;i<myboard.size();i++){
        for(int j=0;j<myboard[i].length();j++){
            board[i][j]=myboard[i][j];
        }
    }
    solveSudoku(board);
    for(int k=0;k<sum.size();k++){
    for(int i=0;i<sum[k].size();i++){
        for(int j=0;j<sum[k][i].size();j++){
            cout<<sum[k][i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<"######"<<endl;
    }
    cout<<"sum is "<<sum.size()<<endl;
    cout << "Hello world!" << endl;
    return 0;
}

输出了8个解。




双臂复合机器人眼在手外标定实战:从原理到D435标定流程详解 本文详细解析了双臂复合机器人中眼在手外标定的核心原理与实战流程。通过阐述坐标系转换与AX=XB方程,重点介绍了使用Intel RealSense D435相机进行标定的完整步骤,包括环境搭建、数据采集、计算与验证,并提供了关键的避坑指南,帮助工程师实现相机与机械臂基座间的精确坐标对齐。 阅读详情

相关推荐

使用PlotNeuralNet绘制深度学习网络图的基本操作

使用PlotNeuralNet绘制深度学习网络图的基本操作

weixin_46877907的博客 2110

LeetCode 第 37 题 (Sudoku Solver)

LeetCode 第 37 题 (Sudoku Solver) Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character ‘.’. You may assume that there will be only on...

Ivan 的专栏 1939

一文入门Qwen-Agent【附实战代码】

Qwen-Agent 是一个开源框架,用于开发基于 Qwen 模型的 AI 智能体应用,一文带你入门使用

m0_55303420的博客 1874

Leetcode Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle...

popfish的算法路 5607

【STC32G12K128开发板】——从零到一:Keil C251开发环境全攻略

本文详细介绍了为STC32G12K128开发板搭建Keil C251开发环境的完整流程。从环境准备、软件安装、驱动配置到创建第一个工程并下载程序,提供了避坑指南和实用技巧,帮助开发者高效入门这款基于251内核的国产32位单片机开发。

weixin_29192141的博客 363

【国产MCU】-认识CH32V307及开发环境搭建

CH32V307是[沁恒](https://www.wch.cn/)推出的一款基于32位RISC-V设计的互联型微控制器,配备了硬件堆栈区、快速中断入口,在标准RISC-V基础上大大提高了中断响应速度。加入单精度浮点指令集,扩充堆栈区,具有更高的运算性能。扩展串口UART数量到8组,电机定时器到4组。提供USB2.0高速接口(480Mbps)并内置了PHY收发器,以太网MAC升级到千兆并集成了10M-PHY模块。

视觉与物联智能 2343

LeetCode Sudoku Solver

通过程序来解决数独问题。

DRFish 1737

PCL 直通滤波

内容抄自优快云点云侠:[【2024最新版】PCL点云处理算法汇总(C++长期更新版)](https://blog.youkuaiyun.com/qq_36686437/article/details/114160640)。质量无忧,永久免费,可放心复制粘贴。

m0_51204289的博客 903

Leetcode之Sudoku Solver

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each...

qq_35455503的博客 273

mybaits-plus通用CRUD详解及代码案例

文章目录mybaitsplus通用CRUD插入操作方法定义测试用例mybaitsplus主键生成策略设置自增主键设置输入主键:UUID:ID_WORKER_STR:ID_WORKER:更新操作根据id更新方法定义:方法步骤根据条件更新方法定义:更新步骤:根据条件更新(可以更新NULL)删除操作根据id删除方法定义:删除步骤:根据 entity 条件删除方法定义:删除步骤:根据ID 批量删除方法定义:删除步骤:查询操作根据id查询方法定义:查询步骤:根据id批量查询方法定义:查询步骤:根据 entity 条件

流楚丶格念的博客 3405

【Android 11】使用Android Studio调试系统应用之Settings移植(三):构建Settings、SettingsLib项目文件

基于android 11,本系列文章力争实用,讲解使用AS调试系统app的方法,并提供了配套源下载,供学习调试。上一篇文章[Android 11]使用Android Studio调试系统应用(二):依赖分析、android.bp、静态和动态依赖,讲解了如何搭建编译服务器,如何下载AOSP源码,如何切换到AOSP的指定分支(android-11-r39),以及如何编译、解决编译错误、运行模拟器,本文将开始讲解如何构建Settings的AS工程文件! 走过、路过,有用过的同学,记得给个赞O(∩_∩)O哈!

上周的博客 2679

LeetCode :Sudoku Solver

Sudoku Solver  Total Accepted: 13937 Total Submissions: 66832My Submissions Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character

AresGod的专栏 2314

Matplotlib之bar3d画3D柱状图

3D柱状图通常用于展示数据集中不同分类项的数据大小或数量。与普通的柱状图相比,3D柱状图更适合展示数据的三个维度 3D 柱状图 按行/列涂颜色 柱加阴影、描黑边 自定义座标轴名、刻度标签、范围

我走的每一步都算数 1121

leetcode Sudoku Solver 解法优化

题目 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution.

zyl26的专栏 1051

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

[Leetcode]Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku

NoooName的专栏 496

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

[Leetcode] Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle......

weixin_33896069的博客 85

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

LeetCode - Sudoku Solver

https://leetcode.com/problems/sudoku-solver/ 这道题我发现大部分的解决方法都是加一个isValid函数,来判断当前数字是否合法。 比如: http://blog.youkuaiyun.com/linhuanmars/article/details/20748761 http://www.ninechapter.com/solutions/sudoku-s

chilakaka的专栏 312

【leetcode】 Sudoku Solver

问题: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution.

shiquxinkong的专栏 2448

LeetCode:Valid Sudoku与Sudoku Solver

Valid Sudoku1、题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’. Note: A

zems 418

[LeetCode] Sudoku Solver

Description Sudoku Solver: Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactl

sinat_24796205的博客 148

leetcode Sudoku Solver

dfs

GadyPu的专栏 367

[LeetCode 37] Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Asudoku solution must satisfyall ofthe following rules: Each of the digits1-9must occur exactlyonce in each row. Each of t...

ExcitedZhang的博客 193

【leetcode】Sudoku Solver

【leetcode】Sudoku Solver   题目如上,我先说一句实话,在想算法和实现的过程中,遇到各种小错误。写算法和做应用程序实在是不同,平时做个应用,借助各种工具还能debug个所以然来,可是写算法题目要么想不出,想出了debug起来和再重新想一遍更不没差别……折腾了两小时总算是把这个心结解开了。 题目用的是回溯法。其实不论是回溯法还是DP(我是指这一类绕来绕去的),想清楚出口,

猫和鱼爪的鸟窝 560

leetcode笔记:Sudoku Solver

该题规定了一个合法的数独矩阵,编写函数填充这个矩阵,可使用回朔算法完成。

liyuefeilong的专栏 1981

LeetCode(37) Sudoku Solver

题目如下: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution

BUILD 1890

Sudoku Solver -- LeetCode

Sudoku Solver -- LeetCode

qq_44910471的博客 142
上一篇: Leetcode---Palindrome Partitioning
下一篇: Leetcode---Surrounded Regions
我把葡萄酿成酒
博客等级 码龄12年 275粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值