#include <stdio.h>
#include <string.h>
#define MAX_COL_ROW_NUM 9
static int g_array_data[MAX_COL_ROW_NUM][MAX_COL_ROW_NUM] =
{
/*
{0,8,5,0,0,0,2,1,0},
{0,9,4,0,1,2,0,0,3},
{0,0,0,3,0,0,7,0,4},
{5,0,3,4,0,9,0,0,0},
{0,4,0,2,0,6,0,3,0},
{0,0,0,1,0,3,9,0,7},
{6,0,8,0,0,5,0,0,0},
{1,0,0,8,4,0,3,6,0},
{0,2,7,0,0,0,8,9,0}
*/
/*
{0,0,6,0,0,1,0,8,0},
{1,7,0,0,0,9,0,6,0},
{0,0,0,4,6,7,0,0,0},
{6,1,0,0,4,0,8,0,0},
{0,0,2,0,0,0,3,0,0},
{0,0,5,0,7,0,0,9,6},
{0,0,0,2,1,5,0,0,0},
{0,3,0,6,0,0,0,2,8},
{0,2,0,7,0,0,6,0,5}
*/
{0,0,0,7,0,5,0,0,8},
{0,0,0,0,2,0,5,9,0},
{0,4,0,3,0,0,0,2,0},
{0,0,3,9,6,0,0,7,2},
{0,0,6,2,0,1,9,0,0},
{2,9,0,0,3,7,8,0,0},
{0,1,0,0,0,3,0,5,0},
{0,6,7,0,1,0,0,0,0},
{3,0,0,8,0,9,0,0,0}
};
typedef struct tag_needfind
{
int x;
int y;
}V_needfind;
static V_needfind g_needfind[81]={0};
static int g_needfind_num=0;
static int g_fix[MAX_COL_ROW_NUM][MAX_COL_ROW_NUM]={0};
typedef struct tag_small
{
int x1;
int x2;
int y1;
int y2;
}V_small;
static V_small g_small[MAX_COL_ROW_NUM]={0};
int findsmall(int x, int y)
{
int i;
for(i=0;i<MAX_COL_ROW_NUM;i++)
{
if(x>=g_small[i].x1 && x<=g_small[i].x2)
{
if(y>=g_small[i].y1 && y<=g_small[i].y2)
{
return i;
}
}
}
return -1;
}
int IsFix(int val,int x, int y)
{
int i,j,k,m,n;
i=j=k=m=n=0;
//row
for(i=0;i<MAX_COL_ROW_NUM;i++)
{
if(g_array_data[x][i] > 0)
{
if(g_array_data[x][i] == val)
return 0;
}
}
//col
for(i=0;i<MAX_COL_ROW_NUM;i++)
{
if(g_array_data[i][y] > 0)
{
if(g_array_data[i][y] == val)
return 0;
}
}
//small 9
m = findsmall(x,y);
if(m>=0)
{
for(i=g_small[m].x1;i<=g_small[m].x2;i++)
{
for(j=g_small[m].y1;j<=g_small[m].y2;j++)
{
if(g_array_data[i][j] == val)
{
return 0;
}
}
}
}
else
{
printf("err in parse \n");
return 0;
}
return 1;
}
int find(int Index)
{
int i,j,k,m,n;
int x=g_needfind[Index].x;
int y=g_needfind[Index].y;
int flag = 0;
i=j=k=m=n=0;
for(i=0;i<9;i++)
{
//i+1满足行、列、小9的要求
if(1 == IsFix(i+1,x,y))
{
g_array_data[x][y] = i+1;
g_fix[x][y] = i+1;
if(Index < g_needfind_num-1)
{
if(-1 == find(Index+1))
{
flag = 0;
continue;
}
}
else
{
printf("find ok\n");
}
flag = 1;
}
}
if(0==flag)
{
g_array_data[x][y] = 0;
g_fix[x][y] = 0;
return -1;
}
else
return 0;
}
void Init()
{
int i=0,j=0,k=0;
for(i=0;i<3;i++)
{
g_small[k].x1 = 0;
g_small[k].x2 = MAX_COL_ROW_NUM/3-1;
g_small[k].y1 = 0+i*3;
g_small[k].y2 = MAX_COL_ROW_NUM/3-1+i*3;
++k;
}
for(i=0;i<3;i++)
{
g_small[k].x1 = MAX_COL_ROW_NUM/3;
g_small[k].x2 = MAX_COL_ROW_NUM/3*2-1;
g_small[k].y1 = 0+i*3;
g_small[k].y2 = MAX_COL_ROW_NUM/3-1+i*3;
++k;
}
for(i=0;i<3;i++)
{
g_small[k].x1 = MAX_COL_ROW_NUM/3*2;
g_small[k].x2 = MAX_COL_ROW_NUM/3*3-1;
g_small[k].y1 = 0+i*3;
g_small[k].y2 = MAX_COL_ROW_NUM/3-1+i*3;
++k;
}
}
void main()
{
int i,j,k=0;
Init();
for(i=0;i<MAX_COL_ROW_NUM;i++)
{
for(j=0;j<MAX_COL_ROW_NUM;j++)
{
if(0 == g_array_data[i][j])
{
g_needfind[k].x = i;
g_needfind[k].y = j;
++k;
}
}
}
g_needfind_num = k;
if(k>0)
{
find(0);
}
for(i=0;i<MAX_COL_ROW_NUM;i++)
{
for(j=0;j<MAX_COL_ROW_NUM;j++)
{
printf("%d ", g_array_data[i][j]);
}
printf("\n");
}
}
#include <string.h>
#define MAX_COL_ROW_NUM 9
static int g_array_data[MAX_COL_ROW_NUM][MAX_COL_ROW_NUM] =
{
/*
{0,8,5,0,0,0,2,1,0},
{0,9,4,0,1,2,0,0,3},
{0,0,0,3,0,0,7,0,4},
{5,0,3,4,0,9,0,0,0},
{0,4,0,2,0,6,0,3,0},
{0,0,0,1,0,3,9,0,7},
{6,0,8,0,0,5,0,0,0},
{1,0,0,8,4,0,3,6,0},
{0,2,7,0,0,0,8,9,0}
*/
/*
{0,0,6,0,0,1,0,8,0},
{1,7,0,0,0,9,0,6,0},
{0,0,0,4,6,7,0,0,0},
{6,1,0,0,4,0,8,0,0},
{0,0,2,0,0,0,3,0,0},
{0,0,5,0,7,0,0,9,6},
{0,0,0,2,1,5,0,0,0},
{0,3,0,6,0,0,0,2,8},
{0,2,0,7,0,0,6,0,5}
*/
{0,0,0,7,0,5,0,0,8},
{0,0,0,0,2,0,5,9,0},
{0,4,0,3,0,0,0,2,0},
{0,0,3,9,6,0,0,7,2},
{0,0,6,2,0,1,9,0,0},
{2,9,0,0,3,7,8,0,0},
{0,1,0,0,0,3,0,5,0},
{0,6,7,0,1,0,0,0,0},
{3,0,0,8,0,9,0,0,0}
};
typedef struct tag_needfind
{
int x;
int y;
}V_needfind;
static V_needfind g_needfind[81]={0};
static int g_needfind_num=0;
static int g_fix[MAX_COL_ROW_NUM][MAX_COL_ROW_NUM]={0};
typedef struct tag_small
{
int x1;
int x2;
int y1;
int y2;
}V_small;
static V_small g_small[MAX_COL_ROW_NUM]={0};
int findsmall(int x, int y)
{
int i;
for(i=0;i<MAX_COL_ROW_NUM;i++)
{
if(x>=g_small[i].x1 && x<=g_small[i].x2)
{
if(y>=g_small[i].y1 && y<=g_small[i].y2)
{
return i;
}
}
}
return -1;
}
int IsFix(int val,int x, int y)
{
int i,j,k,m,n;
i=j=k=m=n=0;
//row
for(i=0;i<MAX_COL_ROW_NUM;i++)
{
if(g_array_data[x][i] > 0)
{
if(g_array_data[x][i] == val)
return 0;
}
}
//col
for(i=0;i<MAX_COL_ROW_NUM;i++)
{
if(g_array_data[i][y] > 0)
{
if(g_array_data[i][y] == val)
return 0;
}
}
//small 9
m = findsmall(x,y);
if(m>=0)
{
for(i=g_small[m].x1;i<=g_small[m].x2;i++)
{
for(j=g_small[m].y1;j<=g_small[m].y2;j++)
{
if(g_array_data[i][j] == val)
{
return 0;
}
}
}
}
else
{
printf("err in parse \n");
return 0;
}
return 1;
}
int find(int Index)
{
int i,j,k,m,n;
int x=g_needfind[Index].x;
int y=g_needfind[Index].y;
int flag = 0;
i=j=k=m=n=0;
for(i=0;i<9;i++)
{
//i+1满足行、列、小9的要求
if(1 == IsFix(i+1,x,y))
{
g_array_data[x][y] = i+1;
g_fix[x][y] = i+1;
if(Index < g_needfind_num-1)
{
if(-1 == find(Index+1))
{
flag = 0;
continue;
}
}
else
{
printf("find ok\n");
}
flag = 1;
}
}
if(0==flag)
{
g_array_data[x][y] = 0;
g_fix[x][y] = 0;
return -1;
}
else
return 0;
}
void Init()
{
int i=0,j=0,k=0;
for(i=0;i<3;i++)
{
g_small[k].x1 = 0;
g_small[k].x2 = MAX_COL_ROW_NUM/3-1;
g_small[k].y1 = 0+i*3;
g_small[k].y2 = MAX_COL_ROW_NUM/3-1+i*3;
++k;
}
for(i=0;i<3;i++)
{
g_small[k].x1 = MAX_COL_ROW_NUM/3;
g_small[k].x2 = MAX_COL_ROW_NUM/3*2-1;
g_small[k].y1 = 0+i*3;
g_small[k].y2 = MAX_COL_ROW_NUM/3-1+i*3;
++k;
}
for(i=0;i<3;i++)
{
g_small[k].x1 = MAX_COL_ROW_NUM/3*2;
g_small[k].x2 = MAX_COL_ROW_NUM/3*3-1;
g_small[k].y1 = 0+i*3;
g_small[k].y2 = MAX_COL_ROW_NUM/3-1+i*3;
++k;
}
}
void main()
{
int i,j,k=0;
Init();
for(i=0;i<MAX_COL_ROW_NUM;i++)
{
for(j=0;j<MAX_COL_ROW_NUM;j++)
{
if(0 == g_array_data[i][j])
{
g_needfind[k].x = i;
g_needfind[k].y = j;
++k;
}
}
}
g_needfind_num = k;
if(k>0)
{
find(0);
}
for(i=0;i<MAX_COL_ROW_NUM;i++)
{
for(j=0;j<MAX_COL_ROW_NUM;j++)
{
printf("%d ", g_array_data[i][j]);
}
printf("\n");
}
}