数独解题VC++源程序
数独解题VC++源程序,原创的。 // ccc.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include <conio.h> #include <ctype.h> #include <iostream.h> #include <stdlib.h> int main(int argc, char* argv[]) { int i,j,k,p,t,x,y,m,n,pn,tmpi,tmpj,na; int ia,ja,tt=1,no=0,pm; int sd[10][10][10],tmpp[10]; char ch; FILE *fp; for (i=1; i<10; i++){ for (j=1; j<10; j++){ for (k=0; k<10; k++){ sd[i][j][k]=k; } } } if( (fp = fopen( "data.sd", "r" )) == NULL ) printf( "\nThe file 'data.sd' was not opened\n" ); else { for (i=1; i<10; i++){ for (j=1; j<10; j++){ ch=fgetc(fp); sd[i][j][0]= ch-0x30; } fgetc(fp); } fclose(fp); } /* sd[1][1][0]=3; sd[1][2][0]=7; sd[1][3][0]=6; sd[2][1][0]=0; sd[2][2][0]=0; sd[2][3][0]=0; sd[3][1][0]=9; sd[3][2][0]=4; sd[3][3][0]=1; sd[4][1][0]=2; sd[9][2][0]=8; sd[5][1][0]=8; sd[1][1][0]=0; sd[1][2][0]=0; sd[1][3][0]=0; sd[1][4][0]=0; sd[1][5][0]=0; sd[1][6][0]=0; sd[1][7][0]=0; sd[1][8][0]=0; sd[1][9][0]=0; sd[2][1][0]=0; sd[2][2][0]=0; sd[2][3][0]=0; sd[2][4][0]=0; sd[2][5][0]=0; sd[2][6][0]=0; sd[2][7][0]=0; sd[2][8][0]=0; sd[2][9][0]=0; sd[3][1][0]=0; sd[3][2][0]=0; sd[3][3][0]=0; sd[3][4][0]=0; sd[3][5][0]=0; sd[3][6][0]=0; sd[3][7][0]=0; sd[3][8][0]=0; sd[3][9][0]=0; sd[4][1][0]=0; sd[4][2][0]=0; sd[4][3][0]=0; sd[4][4][0]=0; sd[4][5][0]=0; sd[4][6][0]=0; sd[4][7][0]=0; sd[4][8][0]=0; sd[4][9][0]=0; sd[5][1][0]=0; sd[5][2][0]=0; sd[5][3][0]=0; sd[5][4][0]=0; sd[5][5][0]=0; sd[5][6][0]=0; sd[5][7][0]=0; sd[5][8][0]=0; sd[5][9][0]=0; sd[6][1][0]=0; sd[6][2][0]=0; sd[6][3][0]=0; sd[6][4][0]=0; sd[6][5][0]=0; sd[6][6][0]=0; sd[6][7][0]=0; sd[6][8][0]=0; sd[6][9][0]=0; sd[7][1][0]=0; sd[7][2][0]=0; sd[7][3][0]=0; sd[7][4][0]=0; sd[7][5][0]=0; sd[7][6][0]=0; sd[7][7][0]=0; sd[7][8][0]=0; sd[7][9][0]=0; sd[8][1][0]=0; sd[8][2][0]=0; sd[8][3][0]=0; sd[8][4][0]=0; sd[8][5][0]=0; sd[8][6][0]=0; sd[8][7][0]=0; sd[8][8][0]=0; sd[8][9][0]=0; sd[9][1][0]=0; sd[9][2][0]=0; sd[9][3][0]=0; sd[9][4][0]=0; sd[9][5][0]=0; sd[9][6][0]=0; sd[9][7][0]=0; sd[9][8][0]=0; sd[9][9][0]=0; sd[1][1][0]=0; sd[1][2][0]=0; sd[1][3][0]=0; sd[1][4][0]=0; sd[1][5][0]=0; sd[1][6][0]=0; sd[1][7][0]=8; sd[1][8][0]=0; sd[1][9][0]=0; sd[2][1][0]=0; sd[2][2][0]=0; sd[2][3][0]=6; sd[2][4][0]=5; sd[2][5][0]=0; sd[2][6][0]=0; sd[2][7][0]=0; sd[2][8][0]=4; sd[2][9][0]=3; sd[3][1][0]=0; sd[3][2][0]=2; sd[3][3][0]=0; sd[3][4][0]=0; sd[3][5][0]=7; sd[3][6][0]=0; sd[3][7][0]=0; sd[3][8][0]=0; sd[3][9][0]=0; sd[4][1][0]=0; sd[4][2][0]=0; sd[4][3][0]=3; sd[4][4][0]=6; sd[4][5][0]=0; sd[4][6][0]=0; sd[4][7][0]=0; sd[4][8][0]=0; sd[4][9][0]=0; sd[5][1][0]=4; sd[5][2][0]=0; sd[5][3][0]=0; sd[5][4][0]=0; sd[5][5][0]=0; sd[5][6][0]=0; sd[5][7][0]=0; sd[5][8][0]=0; sd[5][9][0]=7; sd[6][1][0]=0; sd[6][2][0]=0; sd[6][3][0]=0; sd[6][4][0]=0; sd[6][5][0]=0; sd[6][6][0]=8; sd[6][7][0]=1; sd[6][8][0]=0; sd[6][9][0]=0; sd[7][1][0]=0; sd[7][2][0]=0; sd[7][3][0]=0; sd[7][4][0]=0; sd[7][5][0]=3; sd[7][6][0]=0; sd[7][7][0]=0; sd[7][8][0]=6; sd[7][9][0]=0; sd[8][1][0]=8; sd[8][2][0]=9; sd[8][3][0]=0; sd[8][4][0]=0; sd[8][5][0]=0; sd[8][6][0]=1; sd[8][7][0]=2; sd[8][8][0]=0; sd[8][9][0]=0; sd[9][1][0]=0; sd[9][2][0]=0; sd[9][3][0]=5; sd[9][4][0]=0; sd[9][5][0]=0; sd[9][6][0]=0; sd[9][7][0]=0; sd[9][8][0]=0; sd[9][9][0]=0; */ if( (fp = fopen( "sd.out", "w" )) == NULL ) printf( "\nThe file 'sd.out' was not opened\n" ); printf ("\n*************************************"); fprintf (fp,"\n*************************************"); for (i=1;i<10;i++){ printf ("\n"); fprintf (fp,"\n"); for (j=1;j<10;j++){ printf ("%d ",sd[i][j][0]); fprintf (fp,"%d ",sd[i][j][0]); } } printf ("\n*************************************\n"); fprintf (fp,"\n*************************************\n"); pn=0; for (i=1;i<10;i++){ for (j=1;j<10;j++){ if (sd[i][j][0]==0) { pn=pn+1; for (k=1;k<10;k++){ if (sd[i][k][0]!=0) sd[i][j][sd[i][k][0]]=0;} for (k=1;k<10;k++){ if (sd[k][j][0]!=0) sd[i][j][sd[k][j][0]]=0;} tmpi=int((i-1)/3);tmpj=int((j-1)/3); for (m=1;m<4;m++){ for(n=1;n<4;n++){ x=tmpi*3+m;y=tmpj*3+n; if (sd[x][y][0]!=0) {sd[i][j][sd[x][y][0]]=0;} } } } else {for (k=1;k<10;k++){sd[i][j][k]=0;} } } } printf ("\n****** pn=%d \n",pn); fprintf (fp,"\n****** pn=%d \n",pn); pm=pn; _getch(); while (pn!=0) { for (i=1;i<10;i++){ for (j=1;j<10;j++){ if (tt==1 && no<pm) { no++; printf ("\n\n** NO:%d ** ",no); fprintf (fp,"\n\n** NO:%d ** ",no); tt=0; } if (sd[i][j][0]==0) { t=0;na=0; for (p=1;p<10;p++) { if (sd[i][j][p]!=0) {t=sd[i][j][p]; na=na+1;} } if (na==1) { //---------------------------------------------------------------------------------------- printf ("i=%d j=%d t=%d **\n",i,j,t); fprintf (fp,"i=%d j=%d t=%d **\n",i,j,t); printf ("\n ------ i=%d --- j=%d ------[%d] ---------",i,j,sd[i][j][0]); fprintf (fp,"\n ------ i=%d --- j=%d ------[%d] ---------",i,j,sd[i][j][0]); for (p=1;p<10;p++){ printf ("%d ",sd[i][j][p]); fprintf (fp,"%d ",sd[i][j][p]); } //---------------------------------------------------------------------------------------- sd[i][j][0]=t; pn=pn-1; } } if (sd[i][j][0]==0) { t=0;na=0; for (k=1;k<10;k++) {tmpp[k]=sd[i][j][k];} for (p=1;p<10;p++) { for (k=1;k<10;k++){ if (sd[i][k][0]==0 && k!=j) {tmpp[p]=tmpp[p]+sd[i][k][p];} } } for (p=1;p<10;p++) { if (sd[i][j][p]!=0 && sd[i][j][p]==tmpp[p]) {t=sd[i][j][p]; na=na+1;} } if (na==1) { //---------------------------------------------------------------------------------------- printf ("i=%d j=%d t=%d **\n",i,j,t); fprintf (fp,"i=%d j=%d t=%d **\n",i,j,t); for (p=1;p<10;p++){printf ("\t%d",sd[i][j][p]);fprintf (fp,"\t%d",sd[i][j][p]);} printf ("\n");fprintf (fp,"\n"); for (p=1;p<10;p++){printf ("\t%d",tmpp[p]);fprintf (fp,"\t%d",tmpp[p]);} for (k=1;k<10;k++){ printf ("\n ------ i=%d --- j=%d ------[%d] ---------",i,k,sd[i][k][0]); fprintf (fp,"\n ------ i=%d --- j=%d ------[%d] ---------",i,k,sd[i][k][0]); for (p=1;p<10;p++){ printf ("%d ",sd[i][k][p]); fprintf (fp,"%d ",sd[i][k][p]); } } //---------------------------------------------------------------------------------------- sd[i][j][0]=sd[i][j][t]; pn=pn-1; } } if (sd[i][j][0]==0) { t=0;na=0; for (k=1;k<10;k++) {tmpp[k]=sd[i][j][k];} for (p=1;p<10;p++) { for (k=1;k<10;k++) { if (sd[k][j][0]==0 && k!=i) {tmpp[p]=tmpp[p]+sd[k][j][p];} } } for (p=1;p<10;p++) { if (sd[i][j][p]!=0 && sd[i][j][p]==tmpp[p]) {t=sd[i][j][p]; na=na+1;} } if (na==1) { //---------------------------------------------------------------------------------------- printf ("i=%d j=%d t=%d **\n",i,j,t); fprintf (fp,"i=%d j=%d t=%d **\n",i,j,t); for (p=1;p<10;p++){ printf ("\t%d",sd[i][j][p]); fprintf (fp,"\t%d",sd[i][j][p]); } printf ("\n"); fprintf (fp,"\n"); for (p=1;p<10;p++){ printf ("\t%d",tmpp[p]); fprintf (fp,"\t%d",tmpp[p]); } for (k=1;k<10;k++){ printf ("\n ------ i=%d --- j=%d ------[%d] ---------",k,j,sd[k][j][0]); fprintf (fp,"\n ------ i=%d --- j=%d ------[%d] ---------",k,j,sd[k][j][0]); for (p=1;p<10;p++){ printf ("%d ",sd[k][j][p]); fprintf (fp,"%d ",sd[k][j][p]); } } //---------------------------------------------------------------------------------------- sd[i][j][0]=sd[i][j][t]; pn=pn-1; } } if (sd[i][j][0]==0) { t=0;na=0; for (k=1;k<10;k++) {tmpp[k]=sd[i][j][k];} tmpi=int((i-1)/3); tmpj=int((j-1)/3); for (p=1;p<10;p++) { for (m=1;m<4;m++) { x=tmpi*3+m; for (n=1;n<4;n++) { y=tmpj*3+n; if (sd[x][y][0]==0 && !(x==i && y==j)) {tmpp[p]=tmpp[p]+sd[x][y][p];} } } } for (p=1;p<10;p++) { if (sd[i][j][p]!=0 && sd[i][j][p]==tmpp[p]) {t=sd[i][j][p]; na=na+1;} } if (na==1) { //---------------------------------------------------------------------------------------- printf ("i=%d j=%d t=%d **\n",i,j,t); fprintf (fp,"i=%d j=%d t=%d **\n",i,j,t); for (p=1;p<10;p++){ printf ("\t%d",sd[i][j][p]); fprintf (fp,"\t%d",sd[i][j][p]); } printf ("\n"); fprintf (fp,"\n"); for (p=1;p<10;p++){ printf ("\t%d",tmpp[p]); fprintf (fp,"\t%d",tmpp[p]); } tmpi=int((i-1)/3); tmpj=int((j-1)/3); for (m=1;m<4;m++){ for (n=1;n<4;n++){ x=tmpi*3+m; y=tmpj*3+n; printf ("\n ------ x=%d --- y=%d ------[%d] ---------",x,y,sd[x][y][0]); fprintf (fp,"\n ------ x=%d --- y=%d ------[%d] ---------",x,y,sd[x][y][0]); for (p=1;p<10;p++){ printf ("%d ",sd[x][y][p]); fprintf (fp,"%d ",sd[x][y][p]); } } } //---------------------------------------------------------------------------------------- sd[i][j][0]=sd[i][j][t]; pn=pn-1; } } if (na==1) { for (k=1;k<10;k++) {sd[i][j][k]=0;sd[i][k][sd[i][j][0]]=0;sd[k][j][sd[i][j][0]]=0;} tmpi=int((i-1)/3); tmpj=int((j-1)/3); for (m=1;m<4;m++){ for (n=1;n<4;n++){ x=tmpi*3+m; y=tmpj*3+n; sd[x][y][sd[i][j][0]]=0; } } printf ("\n"); fprintf (fp,"\n"); for (ia=1;ia<10;ia++){ printf ("\n"); fprintf (fp,"\n"); for (ja=1;ja<10;ja++){ printf ("%d ",sd[ia][ja][0]); fprintf (fp,"%d ",sd[ia][ja][0]); } } na=0;tt=1; _getch(); }//end if }//end j }//end i // _getch(); }//end while printf ("\n\n ***** 结果 *****"); fprintf (fp,"\n\n ***** 结果 *****"); for (i=1;i<10;i++){ printf ("\n"); fprintf (fp,"\n"); for (j=1;j<10;j++){ printf ("%d ",sd[i][j][0]); fprintf (fp,"%d ",sd[i][j][0]); } } fclose(fp); printf("\n\nHello World!\n"); _getch(); return 0; }
- 1
- 代码小农2013-10-17写的很详细,对我很有帮助
- 粉丝: 1
- 资源: 2
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 一组快速入门示例,演示了适用于 Android 和 iOS 的 Google API.zip
- 一款简单但有效的 Go 网站迷你分析器.zip
- 一个线程安全的并发映射.zip
- 一个用于与任意 JSON 交互的 Go 包.zip
- 一个用于 go 的 cron 库.zip
- 基于BJUI + Spring MVC + Spring + Mybatis框架的办公自动化系统设计源码
- 基于百度地图的Java+HTML+JavaScript+CSS高速公路设备管理系统设计源码
- 基于Django Web框架的母婴商城实践项目设计源码
- 一个使用 Go 编程语言和 WebAssembly 构建渐进式 Web 应用程序的包 .zip
- 基于Python桌面画笔的自动画图设计源码