-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2580.cpp
More file actions
50 lines (46 loc) · 907 Bytes
/
2580.cpp
File metadata and controls
50 lines (46 loc) · 907 Bytes
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
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#define SZ 9
int ans[SZ][SZ];
int pre[SZ][SZ];
void print(){
for(int i = 0; i < SZ; i++){
for(int j = 0; j < SZ; j++){
printf("%d ", ans[i][j]);
}
puts("");
}
exit(0);
}
bool valid(int r, int c, int num){
for(int i = 0; i < SZ; i++){
if(ans[r][i] == num) return false;
if(ans[i][c] == num) return false;
if(ans[(r/3)*3 + i/3][(c/3)*3 + i%3] == num) return false;
}
return true;
}
void sudoku(int r, int c){
if(c == SZ) r++, c = 0;
if(r == SZ){
print();
return;
}
if(pre[r][c]) return sudoku(r, c+1);
for(int i = 1; i <= SZ; i++){
if(valid(r, c, i)){
ans[r][c] = i;
sudoku(r, c+1);
ans[r][c] = 0;
}
}
}
int main(){
for(int i = 0; i < SZ; i++){
for(int j = 0; j < SZ; j++){
scanf("%d", &pre[i][j]);
if(pre[i][j] != 0) ans[i][j] = pre[i][j];
}
}
sudoku(0, 0);
}