输入一个
的不完整数独矩阵,矩阵中的数字为
。其中,
表示该位置的数字尚未填入,
表示该位置的数字已经填入。
输出一个
的完整数独矩阵。
0 9 2 4 8 1 7 6 3 4 1 3 7 6 2 9 8 5 8 6 7 3 5 9 4 1 2 6 2 4 1 9 5 3 7 8 7 5 9 8 4 3 1 2 6 1 3 8 6 2 7 5 9 4 2 7 1 5 3 8 6 4 9 3 8 6 9 1 4 2 5 7 0 4 5 2 7 6 8 3 1
5 9 2 4 8 1 7 6 3 4 1 3 7 6 2 9 8 5 8 6 7 3 5 9 4 1 2 6 2 4 1 9 5 3 7 8 7 5 9 8 4 3 1 2 6 1 3 8 6 2 7 5 9 4 2 7 1 5 3 8 6 4 9 3 8 6 9 1 4 2 5 7 9 4 5 2 7 6 8 3 1
在这个样例中,输入的数独如下图一所示,输出的是数独的唯一解,如下图二所示。
#include <stdio.h>
#define N 50
#define MASK 0b111111111
int matrix[9][9],row_mask[9], col_mask[9], box_mask[9], z_row[N], z_col[N], z_box[N], n;
int count_zero(int zero_idx, int * x){
int mask =row_mask[z_row[zero_idx]]|col_mask[z_col[zero_idx]]|box_mask[z_box[zero_idx]],result=0;
mask ^=MASK, *x=0;
while (mask) {
result+= mask&1,mask>>=1,(*x)++;
}
return result;
}
int main() {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
int x;
scanf("%d", &x), matrix[row][col]=x;
if (x) {
row_mask[row] |= 1 << (x-1),col_mask[col]|= 1 << (x-1),box_mask[row / 3 * 3 + col / 3]|= 1 << (x-1);
} else {
z_row[n] = row, z_col[n] = col, z_box[n] = row / 3 * 3 + col / 3, n++;
}
}
}
int solu=1,last=0; // test case do have multiple solution, remove this for only solution
for(int i =0;i<n;i++){
int current =0;
for (int j=0;j<n;j++){
if( z_row[j]!=-1 && count_zero(j,&matrix[z_row[j]][z_col[j]])==solu){
int mask =1<<(matrix[z_row[j]][z_col[j]]-1);
row_mask[z_row[j]]|=mask, col_mask[z_col[j]]|=mask, box_mask[z_box[j]]|=mask;
z_row[j]=-1;
if (solu>1)solu=1; // test case do have multiple solution, remove this for only solution
}else current++; // test case do have multiple solution, remove this for only solution
}
if(current==last)solu++; // test case do have multiple solution, remove this for only solution
last=current;// test case do have multiple solution, remove this for only solution
}
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
printf("%d ", matrix[row][col]);
}
printf("\n");
}
return 0;
} #include <stdio.h>
#include <stdlib.h>
char sd[9][9] = { 0 };
char** pdfk(int x, int y)
{
char* res[3];
res[0]=(char**)malloc(sizeof(char*));
res[1] = (char**)malloc(sizeof(char*));
res[2] = (char**)malloc(sizeof(char*));
if (x < 3)
{
if (y < 3)
{
res[0] = &sd[0][0];
res[1] = &sd[1][0];
res[2] = &sd[2][0];
}
else if (y < 6)
{
res[0] = &sd[3][0];
res[1] = &sd[4][0];
res[2] = &sd[5][0];
}
else
{
res[0] = &sd[6][0];
res[1] = &sd[7][0];
res[2] = &sd[8][0];
}
}
else if (x < 6)
{
if (y < 3)
{
res[0] = &sd[0][3];
res[1] = &sd[1][3];
res[2] = &sd[2][3];
}
else if (y < 6)
{
res[0] = &sd[3][3];
res[1] = &sd[4][3];
res[2] = &sd[5][3];
}
else
{
res[0] = &sd[6][3];
res[1] = &sd[7][3];
res[2] = &sd[8][3];
}
}
else
{
if (y < 3)
{
res[0] = &sd[0][6];
res[1] = &sd[1][6];
res[2] = &sd[2][6];
}
else if (y < 6)
{
res[0] = &sd[3][6];
res[1] = &sd[4][6];
res[2] = &sd[5][6];
}
else
{
res[0] = &sd[6][6];
res[1] = &sd[7][6];
res[2] = &sd[8][6];
}
}
return res;
}
char check(int x, int y, int num)
{
for (int i = 0; i < 9; i++)
{
if (sd[y][i] == num && (i != x))
return 0;
if (sd[i][x] == num && (i != y))
return 0;
}
char** fangkuai = pdfk(x, y);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (fangkuai[i][j]==num && (i != y%3 || j!= x%3))
return 0;
}
}
return 1;
}
char place[81][3] = { 0 };
int placecnt = 0;
char result[81][3] = { 0 };
char tianchong(int ceng)
{
if (ceng == placecnt)
{
//找到结果
for (int i = 0; i < placecnt; i++)
{
result[i][0] = place[i][0];
result[i][1] = place[i][1];
result[i][2] = place[i][2];
}
return 1;
}
for (int i = 1; i < 10; i++)
{
if (check(place[ceng][0], place[ceng][1], i))
{
sd[place[ceng][1]][place[ceng][0]] = i;
if (tianchong(ceng + 1))
return 1;
}
}
sd[place[ceng][1]][place[ceng][0]] = 0;
return 0 ;
}
int main()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
scanf("%d", &sd[i][j]);
if (sd[i][j] == 0)
{
place[placecnt][0] = j;
place[placecnt][1] = i;
placecnt++;
}
}
}
tianchong(0);
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
printf("%d ", sd[i][j]);
}
printf("\n");
}
} #include<stdio.h>
int mark[9][9]={0},input[9][9];
int flag=0;
int isvalue(int value,int i,int j)
{
int m,f,l,x,y;
for(m=0;m<9;m++)
{
if (input[i][m]==value)
return 0;
}
for (m=0;m<9;m++)
{
if (input[m][j]==value)
return 0;
}
x=3*(i/3);
y=3*(j/3);
for (f=0;f<3;f++)
{
for (l=0;l<3;l++)
{
if (input[x+f][y+l]==value)
{
return 0;
}
}
}
return 1;
}
void dfs(int i,int j)
{
if (i==9)
{
flag=1;
return;
}
if (input[i][j]==0)
{
for (int n=1;n<=9;n++)
{
if (isvalue(n,i,j))
{
input[i][j]=n;
dfs(j==8?i+1:i,j==8?0:j+1);
if (flag)
return;
input[i][j]=0;
}
}
}
else
{
dfs(j==8?i+1:i,j==8?0:j+1);
}
}
int main()
{
int i,j,n,m;
for (i=0;i<9;i++)
{
for (j=0;j<9;j++)
{
scanf("%d",&input[i][j]);
}
}
dfs(0,0);
for (i=0;i<9;i++)
{
for (j=0;j<9;j++)
printf("%d ",input[i][j]);
printf("\n");
}
}