You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.1 KiB

#include <stdio.h>
void print(int board[9][9]){
printf("|-----------------------|\n");
int i,j;
for(i=0;i<9;i++){
printf("| ");
for(j=0;j<9;j++){
if(board[i][j]==0) printf(". ");
else printf("%d ",board[i][j]);
if(j%3==2) printf("| ");
}
printf("\n");
if(i%3==2){
printf("|-----------------------|\n");
}
}
}
void check(int board[9][9]){
printf("The original Sudoku matrix:\n");
print(board);
int i,j,k;
//<2F>ж<EFBFBD>ÿһ<C3BF><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
for(i=0;i<9;i++){
int cnt[10]={}; //ͳ<><CDB3>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3>ִ<EFBFBD><D6B4><EFBFBD>
for(j=0;j<9;j++){
cnt[board[i][j]]++;
}
for(j=1;j<=9;j++){
if(cnt[j]>1){
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the col %d has been used!\n",j,i+1);
return;
}
}
}
//<2F>ж<EFBFBD>ÿһ<C3BF><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
for(i=0;i<9;i++){
int cnt[10]={}; //ͳ<><CDB3>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3>ִ<EFBFBD><D6B4><EFBFBD>
for(j=0;j<9;j++){
cnt[board[j][i]]++;
}
for(j=1;j<=9;j++){
if(cnt[j]>1){
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the row %d has been used!\n",j,i+1);
return;
}
}
}
//<2F>ж<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>Ź<EFBFBD><C5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
for(k=0;k<9;k++){
int cnt[10]={}; //ͳ<><CDB3>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>ֳ<EFBFBD><D6B3>ִ<EFBFBD><D6B4><EFBFBD>
//<2F><>ʼ<EFBFBD><CABC>Ϊk/3*3
for(i=k/3*3;i<k/3*3+3;i++){
//<2F><>ʼ<EFBFBD><CABC>Ϊk%3*3
for(j=k%3*3;j<k%3*3+3;j++){
cnt[board[i][j]]++;
}
}
for(j=1;j<=9;j++){
if(cnt[j]>1){
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the block %d has been used!\n",j,k+1);
return;
}
}
}
printf("True:Valid initial Sudoku matrix!\n");
return;
}
//<2F><><EFBFBD><EFBFBD>check_plus<75><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int check_plus(int board[9][9]){
int i,j,k;
for(i=0;i<9;i++){
int cnt[10]={};
for(j=0;j<9;j++){
cnt[board[i][j]]++;
}
for(j=1;j<=9;j++){
if(cnt[j]>1){
return 0;
}
}
}
for(i=0;i<9;i++){
int cnt[10]={};
for(j=0;j<9;j++){
cnt[board[j][i]]++;
}
for(j=1;j<=9;j++){
if(cnt[j]>1){
return 0;
}
}
}
for(k=0;k<9;k++){
int cnt[10]={};
for(i=k/3*3;i<k/3*3+3;i++){
for(j=k%3*3;j<k%3*3+3;j++){
cnt[board[i][j]]++;
}
}
for(j=1;j<=9;j++){
if(cnt[j]>1){
return 0;
}
}
}
return 1;
}
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){ //<2F>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
int num; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д1~9
for(num=1;num<=9;num++){
board[i][j]=num;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݹ<EFBFBD><DDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(check_plus(board)){
if(solve(board)){
return 1; //<2F>ɹ<EFBFBD><C9B9><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ҵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
}
}
board[i][j]=0; //<2F>޽⣬<DEBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
}
return 0; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEBCB8><EFBFBD>޷<EFBFBD><DEB7><EFBFBD><EFBFBD>㣬˵<E3A3AC><CBB5><EFBFBD>޽<EFBFBD>
}
}
}
}
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<6F><6E>\n");
}
int main(){
int board0[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
int board1[9][9] = {{8, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
int board2[9][9] = {{5, 2, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
final_step(board0);
final_step(board1);
final_step(board2);
return 0;
}