标签:lse lists temp int 区域 turn \n 多个 family
关键代码展示:
//判断该数字在当前数独是否符合要求
int judge(int num, int ple){
int x = ple / 9; //x表示数字的纵坐标
int y = ple % 9; //y表示数字的横坐标
int qulx = x / 3;
int quey = y / 3; //que表示9宫格的区域
for (int i = 0; i < 9; i++){
if (map[x][i] == num){
return 0;
}
}
for (int i = 0; i < 9; i++){
if (map[i][y] == num){
return 0;
}
}
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (map[qulx * 3 + i][quey * 3 + j] == num)
return 0;
}
}
return 1;
}
void getans(int next){
if (temp == 1){ //目的是只生成一个解
return;
}
if (next == 81){
char lis[600];
temp = 1; //temp=1就可以使递归跳出,避免求得多个解
int bi = 0;
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
if (j == 0)lis[bi++] = map[i][j] + ‘0‘;
else {
lis[bi++] = ‘ ‘;
lis[bi++] = map[i][j] + ‘0‘;
}
}
lis[bi++] =‘\n‘;
}
lis[bi++] = ‘\n‘;
lis[bi] = ‘\0‘;
fputs(lis, fp);
return;
}
if (map[next / 9][next % 9] != 0){ //如果点有数字,则调到下一个网格
getans(next + 1);
return;
}
for (int i = 1; i <= 9; i++){
if (judge(i, next)){
map[next / 9][next % 9] = i;
getans(next + 1);
map[next / 9][next % 9] = 0; //回溯
}
}
}
//根据上文给的算法进行数独的生成
void makesudo(int list[9]){
int lists[9];
char lis[600];
int bi = 0;
for (int i = 0; i < 9; i++){
for (int j = 0; j < mov[i]; j++){
lists[mov[i] - 1 - j] = list[8 - j];
}
for (int j = 0; j < 9 - mov[i]; j++){
lists[j + mov[i]] = list[j];
}
for (int j = 0; j < 9; j++){
if (j == 0) lis[bi++] = lists[j] + ‘0‘;
else{
lis[bi++] = ‘ ‘;
lis[bi++] = lists[j] + ‘0‘;
}
}
lis[bi++] = ‘\n‘;
}
lis[bi++] = ‘\n‘;
lis[bi] = ‘\0‘;
nown++;
fputs(lis, fp);
if (nown == n){
return;
}
}
标签:lse lists temp int 区域 turn \n 多个 family
原文地址:https://www.cnblogs.com/P814264306/p/8810868.html