谁能编一个解决九宫数独的程序(用C语言)

 我来答
匿名用户
2013-08-03
展开全部
#include <stdio.h>

typedef struct
{
int line;
int row;
int num;
}Node;

int main()
{
/*
int a[9][9]={
{4,0,3,6,0,0,0,0,0},
{0,0,0,0,0,1,0,2,4},
{0,1,0,0,4,0,5,0,0},
{0,0,0,9,0,4,0,6,0},
{3,0,2,0,0,0,4,0,9},
{0,7,4,1,0,3,0,0,0},
{0,0,1,0,9,0,0,4,0},
{2,4,0,3,0,0,0,0,0},
{0,0,0,4,0,8,2,0,7}};
*/
int a[9][9]={
{0,0,0,8,0,0,0,6,0},
{8,7,0,0,0,0,0,0,0},
{2,9,0,0,4,1,0,0,5},
{0,0,5,7,0,0,0,0,9},
{0,2,0,0,0,0,0,1,0},
{9,0,0,0,0,4,3,0,0},
{7,0,0,6,1,0,0,9,8},
{0,0,0,0,0,0,0,5,2},
{0,6,0,0,0,9,0,0,0}};
/*
int a[9][9]={
{0,2,0,0,6,0,0,0,0},
{0,9,0,4,0,5,1,3,0},
{0,0,8,7,0,0,0,0,5},
{6,0,0,3,0,0,4,0,0},
{0,0,0,9,0,6,0,0,0},
{0,0,7,0,0,1,0,0,3},
{4,0,0,0,0,7,3,0,0},
{0,8,5,2,0,4,0,7,0},
{0,0,0,0,9,0,0,1,0}};
*/
/*
int a[9][9]={
{0,0,3,0,2,0,0,0,6},
{0,0,2,0,9,0,0,0,4},
{7,0,0,8,0,0,2,0,3},
{0,8,0,0,7,0,5,0,0},
{0,7,0,1,0,6,0,3,0},
{0,0,0,2,0,0,0,9,0},
{4,0,6,0,0,8,0,0,5},
{6,0,0,0,4,0,3,0,0},
{9,0,0,0,1,0,7,0,0}};
*/
int i,j,n,en,flag,y,k=0,x,qu,p,q;
Node b[70];
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
if(!a[i][j])
{
b[k].line=i;
b[k].row=j;
b[k].num=0;
k+=1;
}
}
}
en=k;
/*从b[0]开始试,若b[k].num>9,则k-1,否则k+1*/
for(k=0;k<en;)
{
++b[k].num;
i=b[k].line;
j=b[k].row;
a[i][j]=b[k].num;
n=0;
while(n<9&&b[k].num<=9)
{
if(n==i)
{
for(y=0;y<9;y++)
{
if(y==j)
continue;
if(a[n][y]==a[i][j])
flag=1;
}
}
else if(n==j)
{
for(y=0;y<9;y++)
{
if(y==i)
continue;
if(a[y][n]==a[i][j])
flag=1;
}
}
/*判断同一块中有没有相同值*/
qu=3*(i/3)+j/3;
switch(qu)
{
case 0:x=0;
y=0;
break;
case 1:x=0;
y=3;
break;
case 2:x=0;
y=6;
break;
case 3:x=3;
y=0;
break;
case 4:x=3;
y=3;
break;
case 5:x=3;
y=6;
break;
case 6:x=6;
y=0;
break;
case 7:x=6;
y=3;
break;
default :x=6;
y=6;
break;
}
p=x;
q=y;
for(;x<p+3;x++)
{
for(;y<q+3;y++)
{
if(x==i&&y==j)
continue;
if(a[x][y]==a[i][j])
{
flag=1;
break;
}
}
if(flag==1)
break;
}
if(flag==1)
{
a[i][j]=++b[k].num;
flag=0;
n=0;
continue;
}
n++;
}
if(b[k].num>9)
{
a[i][j]=b[k].num=0;
k--;
if(k<0)
{
printf("error!\r\n");
return -1;
}
}
else
k++;
}
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
printf("%d",a[i][j]);
}
printf("\r\n");
}
return 1;
}
匿名用户
2013-08-03
展开全部
c++的,改成c 就行了

#include <iostream> #include <algorithm> using namespace std;

int main() { int num[9] = {1,2,3,4,5,6,7,8,9};

sort(num, num+9); do { int sum1 = num[0] + num[1] + num[2]; int sum2 = num[3] + num[4] + num[5]; if(sum1 == sum2) { int sum3 = num[6] + num[7] + num[8]; if(sum2 == sum3) { int sum4 = num[0] + num[3] + num[6]; if(sum3 == sum4) { int sum5 = num[1] + num[4] + num[7]; if(sum4 == sum5) { int sum6 = num[2] + num[5] + num[8]; if(sum5 == sum6) { int sum7 = num[2] + num[4] + num[6]; if(sum6 == sum7) { int sum8 = num[0] + num[4] + num[8]; if(sum7 == sum8) { cout<<num[0]<<num[1]<<num[2]<<endl; cout<<num[3]<<num[4]<<num[5]<<endl; cout<<num[6]<<num[7]<<num[8]<<endl; cout<<endl; } } } } } } } }while (next_permutation(num, num+9));

return 0; }
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-08-03
展开全部
额..不会上次想了很久都没做出来..这有个.. http://zhidao.baidu.com/question/52274405.html
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-08-03
展开全部
什么叫九宫数独
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式