tioj 1025. 數獨問題

TIOJ-1025
重新寫一次這題,code 變短了 XD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

int tol=0;
bool (int x, int y, int a[][9], int num){
for(int i=0; i<9; i++){
if(a[x][i]==num) return 0;
if((a[i][y])==num) return 0;
if(a[(x/3)*3+i/3][(y/3)*3+i%3]==num) return 0;
}
return 1;
}
void dfs(int x, int y, int a[][9]){
while(a[x][y]!=0){
if(x==8&&y==8){
tol++;
for(int i=0; i<81; i++){
printf("%d", a[i/9][i%9]);
if((i+1)%9) printf(" ");
else printf("n");
}
printf("n");
}
if(y==8){
y=0;
x++;
}
else y++;
}
for(int i=1; i<=9; i++){
if(judge(x, y, a, i)){
a[x][y] = i;
dfs(x, y, a);
}
a[x][y] = 0;
}
}
int main(){
int a[9][9];
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
scanf("%d", &a[i][j]);
}
}
dfs(0, 0, a);
printf("there are a total of %d solution(s).n", tol);
}