C语言数独求解

我没搞懂return的用法,而且怎么才能正确输出数独求解谢谢#include<stdio.h>voidresolve(inta[9][9]);intcheck(inta[... 我没搞懂return的用法,而且怎么才能正确输出数独求解谢谢
#include<stdio.h>

void resolve(int a[9][9]);
int check(int a[9][9], int num, int row, int col);
int main()
{
int a[9][9]={{0, 0, 0, 0, 0, 0, 0, 9, 0},
{1, 9, 0, 4, 7, 0, 6, 0, 8},
{0, 5, 2, 8, 1, 9, 4, 0, 7},
{2, 0, 0, 0, 4, 8, 0, 0, 0},
{0, 0, 9, 0, 0, 0, 5, 0, 0},
{0, 0, 0, 7, 5, 0, 0, 0, 9},
{9, 0, 7, 3, 6, 4, 1, 8, 0},
{5, 0, 6, 0, 8, 1, 0, 7, 4},
{0, 8, 0, 0, 0, 0, 0, 0, 0}};
int i,j;

resolve(a);
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
printf("%2d", a[i][j]);
}
printf("\n");
}

return 0;
}
void resolve(int a[9][9])
{
int i,j;
int num;
int row, col;
for(i = 0; i < 9; i++)
{
for(j = 0; j < 9; j++)
{
if(a[i][j] == 0)
{
row = i;
col = j;
for(num = 1; num < 10; num++)
{
a[i][j] = num;
if(check(a, num, row, col) == 1)
return resolve(a);
a[i][j] = 0;
}
}
}
}
}
int check(int a[9][9], int num, int row, int col)
{
int i;
int m,n;
m = (row / 3) * 3;
n = (col / 3) * 3;
for(i=0; i < 9; i++)
{
if (a[row][i] == num && i != col)
return 0;
if (a[i][col] == num && i != row)
return 0;
if (a[m + (i%3)][n + (i/3)] == num && (m + (i%3)) != row && (n + (i/3)) != col)
return 0;
}
return 1;
}
展开
 我来答
rw...b@163.com
2017-12-09 · 超过28用户采纳过TA的回答
知道答主
回答量:56
采纳率:16%
帮助的人:7.9万
展开全部
#include

/*数字0表示该位置为空,待填入数字
{0,0,4,6,0,2,0,9,1},
{2,1,0,9,8,4,0,5,6},
{9,0,0,0,7,1,4,2,0},
{1,2,5,0,6,0,3,4,7},
{4,7,6,0,0,0,9,8,5},
{8,3,9,0,4,0,1,6,2},
{0,9,1,2,5,0,0,0,4},
{5,8,0,4,1,6,0,3,9},
{6,4,0,3,0,7,5,0,0}};
*/

int data[9][9] = {
{0,7,0,2,6,0,9,0,0},
{3,0,0,0,0,8,0,0,7},
{0,9,0,0,5,7,0,0,0},
{5,0,0,0,0,0,0,7,0},
{0,4,7,3,1,2,8,5,0},
{0,8,0,0,0,0,0,0,1},
{0,0,0,8,2,0,0,4,0},
{7,0,0,6,0,0,0,0,2},
{0,0,4,0,3,9,0,8,0}};

void input()
{
int i,j;
for(i = 0;i < 9;i++)
for(j = 0;j < 9;j++)
scanf("%d",&data[i][j]);
}

void output()
{
int i,j;
for(i = 0;i < 9;i++)
{
for(j = 0;j < 9;j++)
printf("%d ",data[i][j]);
printf("\n");
}
printf("\n");
}

/*检查num是否可放置在3*3区域是否有冲突*/
int CheckSquare(int line,int col,int num)
{
int i = (line / 3) * 3;
int j = (col / 3) * 3;
int m,n;
for(m = i;m < i + 3;m++)
for(n = j;n < j + 3;n++)
if((data[m][n] == num) && !(m == line && n == col))
return 0;
return 1;
}

/*检查行冲突*/
int CheckLine(int line,int col,int num)
{
int i = 9;
while(i--)
if((data[line][i] == num) && (i != col))
return 0;
return 1;
}

/*检查列冲突*/
int CheckColumn(int line,int col,int num)
{
int i = 9;
while(i--)
if((data[i][col] == num) && (i != line))
return 0;
return 1;
}

/*检查i行j列是否可放置num*/
int Check(int i,int j,int num)
{
return CheckSquare(i,j,num) && CheckLine(i,j,num) && CheckColumn(i,j,num);
}

/*检查是否完成*/
int IsDone()
{
int i,j;
for(i = 0;i < 9;i++)
for(j = 0;j < 9;j++)
if(!Check(i,j,data[i][j]) || (data[i][j] == 0))
return 0;
return 1;
}

void Calc()
{
int i,j,x;
if(IsDone())
{
output();
return;
}
for(i = 0;i < 9;i++)
{
for(j = 0;j < 9;j++)
{
if(data[i][j] == 0)
{
for(x = 1; x <= 9;x++)
{
if(Check(i,j,x))
{
data[i][j] = x;
Calc();
}
}
if(x == 10)
{
data[i][j] = 0;
return ;
}
}
}
}
}

int main()
{
// input();
Calc();
output();

return 0;
}
秒懂百科精选
高粉答主

2018-12-24 · 每个回答都超有意思的
知道答主
回答量:60.8万
采纳率:14%
帮助的人:3.2亿
展开全部
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式