当前位置:网站首页>LeetCode 37. Solve Sudoku
LeetCode 37. Solve Sudoku
2022-08-09 13:49:00 【A pig to】
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
pair<int,int> p = getNext(board);
int sx = p.first,sy = p.second;
dfs(sx,sy,board);
}
bool dfs(int x,int y,vector<vector<char>>& board){
if(isFinished(board)) {
return true;
}
for(int n=1;n<=9;n++){
board[x][y] = char(n+'0');
if(isValidSudoku(board)){
pair<int,int> p = getNext(board);
int sx = p.first,sy = p.second;
if(dfs(sx,sy,board)){
return true;
}
}
}
board[x][y] = '.';
return false;
}
pair<int,int> getNext(vector<vector<char>>& board) {
int sx=-1,sy=-1;
for(int i=0;i<9;i++){
if(sx!=-1){
break;
}
for(int j=0;j<9;j++){
if(board[i][j]=='.'){
sx = i, sy = j;
break;
}
}
}
return make_pair(sx,sy);
}
bool isFinished(vector<vector<char>>& board){
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j]=='.'){
return false;
}
}
}
return true;
}
bool isValidSudoku(vector<vector<char>>& board) {
bool rv[9][10]={
0},cv[9][10]={
0},gv[9][10]={
0};
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j]=='.') {
continue;
}
int v = board[i][j]-'0';
if(rv[i][v] || cv[j][v] || gv[i/3*3+j/3][v]){
return false;
}
rv[i][v] = 1;
cv[j][v] = 1;
gv[ i/3*3+j/3 ][v] = 1;
}
}
return true;
}
};
边栏推荐
- RTSP协议的实现
- telnet+ftp 对设备进行 操控 和 升级
- 激光熔覆在农机修复强化中的应用及研究方向
- [FPGA Tutorial Case 48] Image Case 8 - Realization of Converting RGB Image to HSV Image Based on FPGA, Assisted Verification by MATLAB
- 【奖励公示】第23期 2022年7月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
- FFmpeg multimedia file processing (FFMPEG logging system)
- npm install失败
- jenkins api创建自定义pipeline
- [HCIP Continuous Update] Principle and Configuration of IS-IS Protocol
- ctfshow七夕杯2022
猜你喜欢
Rust 入门指南(使用JSON)
FFmpeg多媒体文件处理(ffmpeg处理流数据的基本概念)
注:检测到当前使用的ADB不是HBuilder内置或自定义ADB:PID为:9544进程名称为:adb.exe 路径为:c:\users\administrator\appdata\local\and
kustomize entry example and basic syntax instructions
ctfshow七夕杯2022
How to save Simulink simulation model as image or PDF
电脑重装系统还原0x80070005错误如何解决
GET POST PUT DELETE request in GIN
Clock frequency and baud rate count for serial communication in FPGA
Map mixed density function and quantile added line
随机推荐
How to save Simulink simulation model as image or PDF
WSA toolkit installed app store tip doesn't work how to solve?
ctfshow七夕杯2022
glibc 内存管理模型 释放 C库内存缓存
JVM内存泄漏和内存溢出的原因
Flutter introduction advanced trip (5) Image Widget
Professor Chen Qiang's "Machine Learning and R Application" course Chapter 15 Homework
ERP不规范,同事两行泪 (转载非原创)
5G China unicom AP:B SMS ASCII 转码要求
Rmarkdown教程
5G 联通网管设计思路
How to solve the 0x80070005 error when the computer is reinstalled and the system is restored
read stream 特别注意
Unicom network management protocol block diagram
leetcode 20. Valid Parentheses 有效的括号(中等)
#WeArePlay | 与更多开发者一起,探索新世界
30行代码实现蚂蚁森林自动偷能量
Introduction to Flutter advanced trip Dialog&Toast (10)
陈强教授《机器学习及R应用》课程 第十三章作业
FPGA-近日工作总结