Repository navigation
Expand file tree
/
Copy pathcodeUp_3530.java
More file actions
68 lines (64 loc) · 1.48 KB
/
Copy pathcodeUp_3530.java
File metadata and controls
68 lines (64 loc) · 1.48 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.*;
// CodeUp, 3530, 스도쿠
public class Main {
static int a[][];
static boolean check1[][], check2[][], check3[][];
static int x[], y[], zeroNum;
static void check() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (a[i][j] == 0) {
System.out.println("Not Possible");
return;
}
}
}
}
static void func(int dep) {
if (dep == zeroNum) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
return;
}
for (int i = 1; i <= 9; i++) {
int xx = x[dep], yy = y[dep];
if (check1[xx][i] || check2[yy][i] || check3[xx / 3 * 3 + yy / 3][i]) {
continue;
}
check1[xx][i] = check2[yy][i] = check3[xx / 3 * 3 + yy / 3][i] = true;
a[xx][yy] = i;
func(dep + 1);
check1[xx][i] = check2[yy][i] = check3[xx / 3 * 3 + yy / 3][i] = false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
a = new int[10][10];
check1 = new boolean[10][10];
check2 = new boolean[10][10];
check3 = new boolean[10][10];
x = new int[100];
y = new int[100];
zeroNum = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
a[i][j] = sc.nextInt();
if (a[i][j] == 0) {
x[zeroNum] = i;
y[zeroNum] = j;
zeroNum++;
} else {
check1[i][a[i][j]] = true;
check2[j][a[i][j]] = true;
check3[i / 3 * 3 + j / 3][a[i][j]] = true;
}
}
}
func(0);
check();
}
}