網站首頁 編程語言 正文
本文實例為大家分享了C語言實現數獨小游戲的具體代碼,供大家參考,具體內容如下
輸入包含9x9的已知數字,空位用0補齊,中間用空格隔開。(輸入數獨題目確保正確)
輸出為輸入數獨題目的解。
玩家需要根據9×9盤面上的已知數字,推理出所有剩余空格的數字,并滿足每一行、每一列、每一個同色九宮內的數字均含1-9,不重復。
樣例輸入:
樣例輸出:
代碼:
#include <stdio.h> #include <stdlib.h> int *fillnumber(int a[9][9][10],int number,int i,int j){ ? ? for (int k=0;k<9;k++){ ? ? ? ? a[i][k][number]++; ? ? ? ? a[k][j][number]++; ? ? } ? ? for (int n=0;n<3;n++){ ? ? ? ? for (int m=0;m<3;m++){ ? ? ? ? ? ? a[i/3*3+n][j/3*3+m][number]++; ? ? ? ? } ? ? } ? ? return a; } int *resetnumber(int a[9][9][10],int number,int i,int j){ ? ? for (int k=0;k<9;k++){ ? ? ? ? a[i][k][number]--; ? ? ? ? a[k][j][number]--; ? ? } ? ? for (int n=0;n<3;n++){ ? ? ? ? for (int m=0;m<3;m++){ ? ? ? ? ? ? a[i/3*3+n][j/3*3+m][number]--; ? ? ? ? } ? ? } ? ? return a; } void search(int step, int a[9][9],int g[9][9][10]){ ? ? if (step==0) { ? ? ? ? for (int i=0;i<9;i++){ ? ? ? ? ? ? for (int j=0;j<9;j++){ ? ? ? ? ? ? ? ? printf("%d ",a[i][j]); ? ? ? ? ? ? } ? ? ? ? ? ? printf("\n"); ? ? ? ? } ? ? ? ? exit(0); ? ? } ? ? for (int i=0;i<9;i++){ ? ? ? ? for (int j=0;j<9;j++){ ? ? ? ? ? ? if (a[i][j]==0) { ? ? ? ? ? ? ? ? for (int number=1;number<10;number++){ ? ? ? ? ? ? ? ? ? ? if (g[i][j][number]==0){ ? ? ? ? ? ? ? ? ? ? ? ? fillnumber(g,number,i,j); ? ? ? ? ? ? ? ? ? ? ? ? a[i][j]=number; ? ? ? ? ? ? ? ? ? ? ? ? search(step-1,a,g); ? ? ? ? ? ? ? ? ? ? ? ? a[i][j]=0; ? ? ? ? ? ? ? ? ? ? ? ? resetnumber(g,number,i,j); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? } ? ? } } int main(int argc, const char * argv[]) { ? ? int a[9][9]; ? ? int abool[9][9][10]; ? ? int step=0; ? ? for (int i =0;i<9;i++){ ? ? ? ? for (int j=0;j<9;j++){ ? ? ? ? ? ? for (int k=1;k<10;k++) { ? ? ? ? ? ? ? ? abool[i][j][k]=0; ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? for (int i =0;i<9;i++){ ? ? ? ? for (int j=0;j<9;j++){ ? ? ? ? ? ? scanf("%d",&a[i][j]); ? ? ? ? ? ? if (a[i][j]==0) { ? ? ? ? ? ? ? ? step++; ? ? ? ? ? ? } ? ? ? ? ? ? else{ ? ? ? ? ? ? ? ? fillnumber(abool,a[i][j],i,j); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? search(step,a,abool); ? ? return 0; }
原文鏈接:https://blog.csdn.net/qq_46957383/article/details/108802713
相關推薦
- 2022-11-17 Python+OpenCV之圖像輪廓詳解_python
- 2022-05-08 Python?matplotlib實現散點圖的繪制_python
- 2022-03-30 Android使用Retrofit上傳文件功能_Android
- 2022-11-05 在jupyter?notebook中使用pytorch的方法_python
- 2023-05-23 numpy增加維度、刪除維度的方法_python
- 2022-10-14 Mybatis一對多使用PageHelper分頁遇到的問題
- 2022-05-20 springboot心跳機制,定時任務
- 2022-05-23 python中的netCDF4批量處理NC文件的操作方法_python
- 最近更新
-
- 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同步修改后的遠程分支