|
|
|
@ -22,9 +22,9 @@ void check(int board[9][9]){
|
|
|
|
|
print(board);
|
|
|
|
|
int i,j,k;
|
|
|
|
|
|
|
|
|
|
//判断每一行数字是否重复
|
|
|
|
|
//„1¤7§Ø„1¤7<EFBFBD>0‹7<EFBFBD>0Ý5„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7<EFBFBD>0¢9„1¤7„1¤7<EFBFBD>1…6„1¤7
|
|
|
|
|
for(i=0;i<9;i++){
|
|
|
|
|
int cnt[10]={}; //统计每个数字出现次数
|
|
|
|
|
int cnt[10]={}; //<EFBFBD>0È1„1¤7„1¤7<EFBFBD>0‹7„1¤7„1¤7„1¤7„1¤7„1¤7<EFBFBD>0ö3„1¤7„1¤7<EFBFBD>0ö4„1¤7„1¤7„1¤7
|
|
|
|
|
for(j=0;j<9;j++){
|
|
|
|
|
cnt[board[i][j]]++;
|
|
|
|
|
}
|
|
|
|
@ -37,9 +37,9 @@ void check(int board[9][9]){
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判断每一列数字是否重复
|
|
|
|
|
//„1¤7§Ø„1¤7<EFBFBD>0‹7<EFBFBD>0Ý5„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7<EFBFBD>0¢9„1¤7„1¤7<EFBFBD>1…6„1¤7
|
|
|
|
|
for(i=0;i<9;i++){
|
|
|
|
|
int cnt[10]={}; //统计每个数字出现次数
|
|
|
|
|
int cnt[10]={}; //<EFBFBD>0È1„1¤7„1¤7<EFBFBD>0‹7„1¤7„1¤7„1¤7„1¤7„1¤7<EFBFBD>0ö3„1¤7„1¤7<EFBFBD>0ö4„1¤7„1¤7„1¤7
|
|
|
|
|
for(j=0;j<9;j++){
|
|
|
|
|
cnt[board[j][i]]++;
|
|
|
|
|
}
|
|
|
|
@ -52,12 +52,12 @@ void check(int board[9][9]){
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判断每个九宫格数字是否重复
|
|
|
|
|
//„1¤7§Ø„1¤7<EFBFBD>0‹7„1¤7„1¤7„1¤7<EFBFBD>0—1„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7<EFBFBD>0¢9„1¤7„1¤7<EFBFBD>1…6„1¤7
|
|
|
|
|
for(k=0;k<9;k++){
|
|
|
|
|
int cnt[10]={}; //统计每个数字出现次数
|
|
|
|
|
//起始行为k/3*3
|
|
|
|
|
int cnt[10]={}; //<EFBFBD>0È1„1¤7„1¤7<EFBFBD>0‹7„1¤7„1¤7„1¤7„1¤7„1¤7<EFBFBD>0ö3„1¤7„1¤7<EFBFBD>0ö4„1¤7„1¤7„1¤7
|
|
|
|
|
//„1¤7„1¤7<EFBFBD>0¶3„1¤7„1¤7<EFBFBD>0Ë2k/3*3
|
|
|
|
|
for(i=k/3*3;i<k/3*3+3;i++){
|
|
|
|
|
//起始列为k%3*3
|
|
|
|
|
//„1¤7„1¤7<EFBFBD>0¶3„1¤7„1¤7<EFBFBD>0Ë2k%3*3
|
|
|
|
|
for(j=k%3*3;j<k%3*3+3;j++){
|
|
|
|
|
cnt[board[i][j]]++;
|
|
|
|
|
}
|
|
|
|
@ -74,7 +74,7 @@ void check(int board[9][9]){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//定义check_plus函数,判断是否为数独矩阵
|
|
|
|
|
//„1¤7„1¤7„1¤7„1¤7check_plus„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7§Ø„1¤7„1¤7<EFBFBD>0¢9„1¤7<EFBFBD>0Ë2„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7
|
|
|
|
|
int check_plus(int board[9][9]){
|
|
|
|
|
int i,j,k;
|
|
|
|
|
for(i=0;i<9;i++){
|
|
|
|
@ -119,31 +119,31 @@ int solve(int board[9][9]){
|
|
|
|
|
int i,j;
|
|
|
|
|
for(i=0;i<9;i++){
|
|
|
|
|
for(j=0;j<9;j++){
|
|
|
|
|
if(board[i][j]==0){ //找到待填的位置
|
|
|
|
|
int num; //遍历填写1~9
|
|
|
|
|
if(board[i][j]==0){ //„1¤7<EFBFBD>0Ü9„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7¦Ë„1¤7„1¤7
|
|
|
|
|
int num; //„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7§Õ1~9
|
|
|
|
|
for(num=1;num<=9;num++){
|
|
|
|
|
board[i][j]=num;
|
|
|
|
|
//若可以填,则继续递归求解新数独
|
|
|
|
|
//„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7ƒ6Ý5„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7<EFBFBD>1¥7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7
|
|
|
|
|
if(check_plus(board)){
|
|
|
|
|
if(solve(board)){
|
|
|
|
|
return 1; //成功填写,找到一组解
|
|
|
|
|
return 1; //„1¤7<EFBFBD>0¯6„1¤7„1¤7„1¤7§Õ„1¤7„1¤7„1¤7<EFBFBD>0Ü9„1¤7<EFBFBD>0Ý5„1¤7„1¤7„1¤7
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
board[i][j]=0; //无解,继续尝试下一个数
|
|
|
|
|
board[i][j]=0; //„1¤7<EFBFBD>1¬5<EFBFBD>7í0„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7<EFBFBD>0Ý5„1¤7„1¤7„1¤7„1¤7
|
|
|
|
|
}
|
|
|
|
|
return 0; //若不管填几都无法满足,说明无解
|
|
|
|
|
return 0; //„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7„1¤7ƒ8‚7„1¤7„1¤7„1¤7<EFBFBD>1«9„1¤7„1¤7„1¤7„1¤7‚0î4<EFBFBD>0»5„1¤7„1¤7„1¤7<EFBFBD>1¬5„1¤7
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void final_step(board){
|
|
|
|
|
void final_step(int board[9][9]){
|
|
|
|
|
check(board);
|
|
|
|
|
if(solve(board)){
|
|
|
|
|
printf("The solution of Sudoku matrix:\n");
|
|
|
|
|
print(board);
|
|
|
|
|
}
|
|
|
|
|
else printf("No solution!\n");
|
|
|
|
|
else printf("No solution„1¤7„1¤7\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|