網站首頁 編程語言 正文
什么是數獨
數獨是源自18世紀瑞士的一種數學游戲。是一種運用紙、筆進行演算的邏輯游戲。玩家需要根據9×9盤面上的已知數字,推理出所有剩余空格的數字,并滿足每一行、每一列、每一個粗線宮(3*3)內的數字均含1-9,不重復。
數獨盤面是個九宮,每一宮又分為九個小格。在這八十一格中給出一定的已知數字和解題條件,利用邏輯和推理,在其他的空格上填入1-9的數字。使1-9每個數字在每一行、每一列和每一宮中都只出現一次,所以又稱“九宮格”。
解決思路
1、遍歷數獨表,找出數字為空(以0填充)的表格;
2、找出每個數據中空的表格中可以填充的數字;
3、找到其中可以填充的數字個數最少的表格;
4、將每個數字分別填充到該表格中;
5、遞歸重復步驟1-4,直到表格中不再有數字為0的表格
#include <iostream> #include <ctime> using namespace std; struct Position { ? ? int row; ? ? int col; ? ? int *res; }; Position* findMinBlank(int board[][9]) { ? ? int *validNums(int board[][9], int row, int col); ? ? Position *pos = new Position(); ? ? pos->res = 0; ? ? int *res; ? ? int total=0, minum = 10; ? ? for(int i=0; i<9; ++i) ? ? ? ? for(int j=0; j<9; ++j) ? ? ? ? { ? ? ? ? ? ? if(board[i][j]!=0) ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? res = validNums(board, i, j); ? ? ? ? ? ? total = 0; ? ? ? ? ? ? for(int p=0; p<9; ++p) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if(res[p]!=0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ++ total; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? if(total<minum) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? delete []pos->res; ? ? ? ? ? ? ? ? pos->row = i; ? ? ? ? ? ? ? ? pos->col = j; ? ? ? ? ? ? ? ? pos->res = res; ? ? ? ? ? ? ? ? minum = total; ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? ? ? delete []res; ? ? ? ? } ? ? return pos; } int *validNums(int board[][9], int row, int col) { ? ? int *res = new int[9] {1,2,3,4,5,6,7,8,9}; ? ? for (int i = 0; i < 9; i++) ? ? { ? ? ? ? res[board[row][i]-1] = 0; ? ? ? ? res[board[i][col]-1] = 0; ? ? } ? ? int p = row / 3 * 3; ? ? int q = col / 3 * 3; ? ? for (int x = p; x < p + 3; x++) ? ? ? ? for (int y = q; y < q + 3; y++)? ? ? ? ? { ? ? ? ? ? ? res[board[x][y]-1] = 0; ? ? ? ? } ? ? return res; } void printResult(int result[][9] ) { ? ? for (int i = 0; i < 9; i++)? ? ? { ? ? ? ? for (int j = 0; j < 9; j++)? ? ? ? ? { ? ? ? ? ? ? cout << result[i][j] << " ?"; ? ? ? ? } ? ? ? ? cout << endl; ? ? } ? ? cout << endl; } void sudoku(int board[][9]) { ? ? Position *pos = findMinBlank(board); ? ? if(!pos->res) ? ? { ? ? ? ? cout<<"time:"<<clock()/1e6<<endl; ? ? ? ? printResult(board); ? ? ? ? return; ? ? } ? ? for(int i=0;i<9;++i) ? ? { ? ? ? ? if(pos->res[i]==0) ? ? ? ? ? ? continue; ? ? ? ? board[pos->row][pos->col] = pos->res[i]; ? ? ? ? sudoku(board); ? ? } ? ? board[pos->row][pos->col] = 0; ? ? delete pos->res; ? ? delete pos; } int main() { ? ? int start = clock(); ? ? cout<<start/1e6<<endl; ? ? int board[][9] = ? ? ? ? { ? ? ? ? ? ? 0, 0, 0, 0, 0, 0, 0, 1, 0, ? ? ? ? ? ? 4, 0, 0, 0, 0, 0, 0, 0, 0, ? ? ? ? ? ? 0, 2, 0, 0, 0, 0, 0, 0, 0, ? ? ? ? ? ? 0, 0, 0, 0, 5, 0, 4, 0, 7, ? ? ? ? ? ? 0, 0, 8, 0, 0, 0, 3, 0, 0, ? ? ? ? ? ? 0, 0, 1, 0, 9, 0, 0, 0, 0, ? ? ? ? ? ? 3, 0, 0, 4, 0, 0, 2, 0, 0, ? ? ? ? ? ? 0, 5, 0, 1, 0, 0, 0, 0, 0, ? ? ? ? ? ? 0, 0, 0, 8, 0, 6, 0, 0, 0 ? ? ? ? }; ? ? printResult(board); ? ? sudoku(board); ? ? int end = clock(); ? ? cout <<"time:" << (end - start)/1e6 << endl; ? ? return 0; }
原文鏈接:https://blog.csdn.net/zhaoqianglao4005/article/details/119997366
相關推薦
- 2022-06-15 axios?gin的GET和POST請求實現示例_Golang
- 2022-07-04 .NET性能優化之為結構體數組使用StructLinq的問題解析_實用技巧
- 2022-10-31 Golang?template?包基本原理分析_Golang
- 2022-04-02 詳解Android如何自定義view實現圓形進度條_Android
- 2022-02-16 小div在大div中水平垂直居中(兩個div都固定寬高)
- 2023-06-02 Hadoop部署的基礎設施操作詳解_服務器其它
- 2022-08-05 Entity?Framework映射TPH、TPT、TPC與繼承類_C#教程
- 2022-03-14 軟件文檔編寫規范(技術文件編寫規范)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支