/
2239.java
161 lines (138 loc) · 3.23 KB
/
2239.java
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int[][] map = new int[10][10];
static int currentRow, currentCol;
private void solve() {
// http://mygumi.tistory.com/208
for (int i = 1; i <= 9; i++) {
String[] s = sc.readLine().split("");
for (int j = 1; j <= 9; j++) {
map[i][j] = Integer.parseInt(s[j - 1]);
}
}
fill();
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
}
public static boolean fill() {
int r = 0;
int c = 0;
if (findEmpty()) {
return true;
}
r = currentRow;
c = currentCol;
for (int n = 1; n <= 9; n++) {
if (isSafe(n, r, c)) {
map[r][c] = n;
if (fill()) {
return true;
}
// failure
map[r][c] = 0;
}
}
// backtracking
return false;
}
public static boolean isSafe(int n, int r, int c) {
if (checkRow(r, n) && checkCol(c, n, 0) && checkBox(r, c, n)) {
return true;
}
return false;
}
public static boolean findEmpty() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
if (map[i][j] == 0) {
currentRow = i;
currentCol = j;
return false;
}
}
}
return true;
}
public static boolean checkRow(int r, int n) {
for (int i = 1; i <= 9; i++) {
if (map[r][i] == n) {
return false;
}
}
return true;
}
public static boolean checkCol(int c, int n, int test) {
for (int i = 1; i <= 9; i++) {
if (map[i][c] == n) {
return false;
}
}
return true;
}
public static boolean checkBox(int r, int c, int n) {
int row = ((int) Math.ceil(r / 3.0) - 1) * 3 + 1;
int col = ((int) Math.ceil(c / 3.0) - 1) * 3 + 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (map[row + i][col + j] == n) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
sc.init();
new Main().solve();
}
static class sc {
private static BufferedReader br;
private static StringTokenizer st;
static void init() {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
static String readLine() {
try {
return br.readLine();
} catch (IOException e) {
}
return null;
}
static String readLineReplace() {
try {
return br.readLine().replaceAll("\\s+", "");
} catch (IOException e) {
}
return null;
}
static String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
}
return st.nextToken();
}
static boolean hasNext() {
return st.hasMoreTokens();
}
static long nextLong() {
return Long.parseLong(next());
}
static int nextInt() {
return Integer.parseInt(next());
}
static double nextDouble() {
return Double.parseDouble(next());
}
}
}