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.
10-29/tiankong.c

117 lines
3.0 KiB

#include <stdio.h>
int correct(int board[9][9]){//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7>Ϸ<EFBFBD>
int col[9]={0},row[9]={0},block[9]={0};
int i,j;
for(i=0;i<9;++i){
for(j=0;j<9;j++){//<2F><><EFBFBD><EFBFBD>
int index=1<<(board[i][j]-1);//<2F><><EFBFBD>ó<EFBFBD>ʼλ<CABC><CEBB><EFBFBD><EFBFBD>
int blockindex=i/3*3+j/3;//<2F>ֿ<EFBFBD>
if(board[i][j]!=0){//<2F><>Ҫ<EFBFBD>жϵ<D0B6>λ<EFBFBD><CEBB>
if(row[i]&index){//<2F>кϷ<D0BA>
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the row %d has been used!",board[i][j],i+1);
return 0;
}
row[i] |=index;//<2F>ж<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>
if(col[j]&index){//<2F>кϷ<D0BA>
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the rol %d has been used!",board[i][j],j+1);
return 0;
}
col[j] |=index;
if(block[blockindex]&index){//<2F><><EFBFBD>Ϸ<EFBFBD>
printf("False:Invalid initial Sudoku matrix!\n");
printf("The number %d in the block %d has been used!",board[i][j],blockindex+1);
return 0;
}
block[blockindex] |=index;
}
}
}
printf("True:Valid initial Sudoku matrix!\n");//<2F><><EFBFBD>϶<EFBFBD>ͨ<EFBFBD><CDA8> <20><><EFBFBD><EFBFBD>
return 1;
}
int isSafe(int board[9][9],int row,int col,int num){//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7>Ϸ<EFBFBD><CFB7>ĺ<EFBFBD><C4BA><EFBFBD>
int x,i,j;
for (x=0;x<=8;x++)//<2F><><EFBFBD><EFBFBD><EFBFBD>кϷ<D0BA>
if(board[row][x]==num)
return 0;
for (x=0;x<= 8;x++)//<2F><><EFBFBD><EFBFBD><EFBFBD>кϷ<D0BA>
if(board[x][col]==num)
return 0;
int sRow=row-row%3,sCol=col-col%3;//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i=0;i<3;i++)
for(j=0;j<3;j++)//<2F><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD>
if(board[i+sRow][j+sCol]==num)
return 0;
return 1;
}
int solve(int board[9][9]){
int num;
int i=0,j=0;
for(i=0;i<9;i++){
for(j=0;j<9;j++){
if(board[i][j]==0){//<2F><>Ҫ<EFBFBD><D2AA><EFBFBD>յIJ<D5B5><C4B2><EFBFBD>
for(num=1;num<=9;++num){
if(isSafe(board,i,j,num)){//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҲҪ<D2B2><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
board[i][j]=num;
if(solve(board)){//<2F><><EFBFBD><EFBFBD>Ƕ<EFBFBD><C7B6>
return 1;
}
board[i][j]=0;
}
}
return 0;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>
}
}
}
//return 1;
}
void print(int board[9][9]){//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
printf("|-----------------------|\n");
int i=0,j=0,s=0,k=0;
for(i=0;i<9;++i){
for(j=0;j<9;j++){
while(j%3==0){
printf("| ");
break;
}
printf("%d ",board[i][j]);
}
printf("|\n");
while((i+1)%3==0){
printf("|-----------------------|\n");
break;
}
}
}
int main(){
int board1[9][9]={{5, 2, 0, 0, 7, 0, 0, 0, 0},//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{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}};
printf("The original Sudoku matrix:\n");
print(board1);//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD>
if(correct(board1)){//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7>Ϸ<EFBFBD>
if(solve(board1)){//<2F>Ƿ<EFBFBD><C7B7>н<EFBFBD>
printf("The solution of Sudoku matrix:\n");
print(board1);//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}else{
printf("No solutions!\n");//<2F>޽<EFBFBD>
}
}
return 0;
}