Su Doku
Problem 96
Su Doku (Japanese meaning number place) is the name given to a popular puzzle concept. Its origin is unclear, but credit must be attributed to Leonhard Euler who invented a similar, and much more difficult, puzzle idea called Latin Squares. The objective of Su Doku puzzles, however, is to replace the blanks (or zeros) in a 9 by 9 grid in such that each row, column, and 3 by 3 box contains each of the digits 1 to 9. Below is an example of a typical starting puzzle grid and its solution grid.
| ![]() |
|
A well constructed Su Doku puzzle has a unique solution and can be solved by logic, although it may be necessary to employ "guess and test" methods in order to eliminate options (there is much contested opinion over this). The complexity of the search determines the difficulty of the puzzle; the example above is considered easy because it can be solved by straight forward direct deduction.
The 6K text file, sudoku.txt (right click and 'Save Link/Target As...'), contains fifty different Su Doku puzzles ranging in difficulty, but all with unique solutions (the first puzzle in the file is the example above).
By solving all fifty puzzles find the sum of the 3-digit numbers found in the top left corner of each solution grid; for example, 483 is the 3-digit number found in the top left corner of the solution grid above.
大意就是解数独了。这道题前前后后做了大概也有几天(因为学业繁忙,专门用来思考的时间倒是不多)。一开始我的想法就是,像人类解数独那样,教计算机用逻辑去分析,但这样要枚举的情况实在太多,而且还不能用一个通式写出来,编写程序十分麻烦。
后来才想到应该用计算的思维去解决这个问题。因为计算机擅长计算,而不是去分析哪个地方最好当突破口等等。于是我们可以考虑,每个空白格都有可能填进1~9的某个数字,按照一定的顺序去填满这些空格子,就构成了一棵巨大无比的树。(如果这个数独有20个已经被填好的数,那么这个树的大小是9^61)
虽然这棵树非常庞大,不过很好的一点是,如果我们否定了一棵树的一个节点,那么它的子树也将被否定,这样这个问题就变成了一个“砍树”的问题。于是思路就呼之欲出:
指定一个填数的序列,从这个序列开始移动,相当于树的先序遍历。遇到不符合数独规则的情况,就将这个子树给砍掉,写成代码是这个样子:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static FILE *fp;
void duru(char sudoku[][9]) {
char str[13]={0};
for (int i=0;i<10;i++) {
fgets(str,13,fp);
if (i!=0) {
for (int j=0;j<9;j++) {
sudoku[i-1][j]=str[j];
}
}
memset(str,0,13);
}
} //测试正确
//return 1 if the sudoku is right
int judge(char sudoku[][9]) {
int ret=1,hash[10]={0};
for (int i=0;i<9;i++) {
memset(hash,0,10*sizeof(int));
for (int j=0;j<9;j++) {
hash[sudoku[i][j]-'0']++;
}
for (int k=1;k<10;k++) {
if (hash[k]>1) {
// ret = 0;
// break;
return 0;
}
}
} //check row
for (int i=0;i<9;i++) {
memset(hash,0,10*sizeof(int));
for (int j=0;j<9;j++) {
hash[sudoku[j][i]-'0']++;
}
for (int k=1;k<10;k++) {
if (hash[k]>1) {
// ret = 0;
// break;
return 0;
}
}
} //check column
for (int i=0;i<9;i+=3) {
for (int j=0;j<9;j+=3) {
memset(hash,0,10*sizeof(int));
for (int i1=i;i1<i+3;i1++) {
for (int j1=j;j1<j+3;j1++) {
hash[sudoku[i1][j1]-'0']++;
}
}
for (int k=1;k<10;k++) {
if (hash[k]>1) {
return 0;
}
}
}
}
return 1;
}
// return 1 stands for end
int forward(int *pi,int *pj,char map[][9]) {
int ret=0;
(*pj)++;
if (*pj>=9) {
(*pj)-=9;
(*pi)++;
}
while (map[*pi][*pj]==1) {
(*pj)++;
if (*pj>=9) {
(*pj)-=9;
(*pi)++;
}
if (*pi==8&&*pj==8) {
ret=1;
break;
}
}
return ret;
}
//0 stands for error
int back(int *pi,int *pj,char map[][9]) {
int ret=1;
(*pj)--;
if (*pi<0) {
(*pj)+=9;
(*pi)--;
}
while (map[*pi][*pj]==1) {
(*pj)--;
if (*pi<0) {
(*pj) += 9;
(*pi)--;
}
if (*pi==-1&&(*pj)!=8) {
ret = 0;
break;
}
}
return ret;
}
void findend(int *pi,int *pj,char map[][9]) {
for (int i=0;i<9;i++) {
for (int j=0;j<9;j++) {
if (!map[i][j]) {
*pi = i;
*pj = j;
}
}
}
}
void solve(char sudoku[][9]) {
int indexi=-1,indexj=8,isback=0,endi,endj;
char map[9][9]={0};
for (int i=0;i<9;i++) {
for (int j=0;j<9;j++) {
if (sudoku[i][j]!='0') map[i][j]=1;
}
} // 注明已知条件
findend(&endi,&endj,map);
do {
if (judge(sudoku)&&(indexi!=endi||indexj!=endj)) {
forward(&indexi,&indexj,map);
sudoku[indexi][indexj] = '1';
}
if (judge(sudoku)==0) {
while (sudoku[indexi][indexj]=='9') {
sudoku[indexi][indexj] = '0';
back(&indexi,&indexj,map);
}
sudoku[indexi][indexj]++;
}
if (judge(sudoku)&&(indexi==endi)&&indexj==endj) break;
} while (1);
}
int main() {
char sudoku[9][9]={0};
int ans = 0;
fp=fopen("2.txt","r");
for (int lp=1;lp<=50;lp++) {
duru(sudoku);
solve(sudoku);
for (int i=0;i<9;i++) {
for (int j=0;j<9;j++) {
printf("%c",sudoku[i][j]);
}
printf("\n");
}
ans += (sudoku[0][0]-'0')*100 + (sudoku[0][1]-'0')*10 +(sudoku[0][2]-'0');
for (int i=0;i<9;i++) {
for (int j=0;j<9;j++) {
sudoku[i][j]=0;
}
} // initialized the sudoku
printf("\n");
}
printf("%d\n",ans);
}