当前位置: 首页 > article >正文

37. Sudoku Solver

题目描述

37. Sudoku Solver

回溯

class Solution {vector<vector<bool>> row_used;vector<vector<bool>> col_used;vector<vector<bool>> box_used;public:void solveSudoku(vector<vector<char>>& board) {row_used.resize(9,vector<bool>(9,false));col_used.resize(9,vector<bool>(9,false));box_used.resize(9,vector<bool>(9,false));for(int row= 0;row < 9;row++){for(int col = 0;col < 9;col++){if(board[row][col] != '.'){int digit = board[row][col] - '0' -1;row_used[row][digit] = true;col_used[col][digit] = true;box_used[(row/3)*3 + col/3][digit] = true;}}}backtrack(board);}bool backtrack(vector<vector<char>>& board){for(int row = 0;row < 9;row++){for(int col = 0;col < 9;col++){if(board[row][col] != '.')continue;for(int digit = 0;digit<9;digit++){if(row_used[row][digit])continue;if(col_used[col][digit])continue;if(box_used[(row/3)*3+ col/3][digit])continue;row_used[row][digit] = true;col_used[col][digit] = true;box_used[(row/3)*3+ col/3][digit] = true;board[row][col] = '0'+ digit + 1;if(backtrack(board)) return true;board[row][col] = '.';row_used[row][digit] = false;col_used[col][digit] = false;box_used[(row/3)*3+ col/3][digit] = false;}return false;}}return true;}
};

可以事先把需要填数的空格存起来

class Solution {vector<vector<bool>> row_used;vector<vector<bool>> col_used;vector<vector<bool>> box_used;vector<pair<int,int>> spaces;//事先把需要填数的空格存起来
public:void solveSudoku(vector<vector<char>>& board) {row_used.resize(9,vector<bool>(9,false));col_used.resize(9,vector<bool>(9,false));box_used.resize(9,vector<bool>(9,false));for(int row= 0;row < 9;row++){for(int col = 0;col < 9;col++){if(board[row][col] != '.'){int digit = board[row][col] - '0' -1;row_used[row][digit] = true;col_used[col][digit] = true;box_used[(row/3)*3 + col/3][digit] = true;}else{spaces.emplace_back(row,col);}}}backtrack(board,0);}//每次调用,处理一个需要填数的空格bool backtrack(vector<vector<char>>& board,int space_idx){if(space_idx == spaces.size())return true;auto [row,col] = spaces[space_idx];for(int digit = 0;digit<9;digit++){if(row_used[row][digit])continue;if(col_used[col][digit])continue;if(box_used[(row/3)*3+ col/3][digit])continue;row_used[row][digit] = true;col_used[col][digit] = true;box_used[(row/3)*3+ col/3][digit] = true;board[row][col] = '0'+ digit + 1;if(backtrack(board,space_idx+1)) return true;board[row][col] = '.';row_used[row][digit] = false;col_used[col][digit] = false;box_used[(row/3)*3+ col/3][digit] = false;}return false;}
};

http://www.lryc.cn/news/2395874.html

相关文章:

  • C# Renci.SshNet 登陆 suse配置一粒
  • RV1126-OPENCV 图像叠加
  • 修改 vscode 左侧导航栏的文字大小 (更新版)
  • 从C++编程入手设计模式2——工厂模式
  • 云原生 Cloud Native Build (CNB)使用初体验
  • 格式工厂 FormatFactory v5.20.便携版 ——多功能媒体文件转换工具 长期更新
  • 数据可视化--使用matplotlib绘制高级图表
  • 卷积神经网络(CNN)完全指南:从原理到实战
  • 如何做好一个决策:基于 Excel的决策树+敏感性分析应用
  • 【模拟电子电路-工具使用】
  • [ElasticSearch] ElasticSearch的初识与基本操作
  • Spring AI 代理模式(Agent Agentic Patterns)
  • 搜索引擎2.0(based elasticsearch6.8)设计与实现细节(完整版)
  • ps中前景色和背景色
  • 网页前端开发(基础进阶2--JS)
  • Go 即时通讯系统:客户端与服务端 WebSocket 通信交互
  • 2025年5月AI科技领域周报(5.19-5.25):大模型多模态突破 具身智能开启机器人新纪元
  • 某航后缀混淆逆向与顶像风控分析
  • [Protobuf]常见数据类型以及使用注意事项
  • 【C/C++】面试基础题目收集
  • 模拟实现线程池(线程数目为定值)和定时器
  • 数据结构之队列实验
  • Java求职者面试题详解:计算机网络、操作系统、设计模式与数据结构
  • 每日八股文6.1
  • 【Ubuntu】摸鱼技巧之虚拟机环境复制
  • 室内VR全景助力房产营销及装修
  • jenkins集成gitlab实现自动构建
  • 【C语言练习】070. 编写代码处理C语言中的异常情况
  • Java基本数据类型、抽象类和接口、枚举、时间类、String类全面介绍
  • Spring Boot微服务架构(八):开发之初就引入APM工具监控