제출 코드
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
package gold;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class G2239_sudoku {
static int len;
static boolean finish = false;
static List<Integer>[] row = new ArrayList[9];
static List<Integer>[] col = new ArrayList[9];
static List<Integer>[] box = new ArrayList[9];
static List<Node> zero = new ArrayList<>();
static int[][] arr = new int[9][9];
static class Node{
int i, j;
Node(int i, int j){
this.i = i;
this.j = j;
}
}
static void fill(int i, int j) {
row[i].add(arr[i][j]);
col[j].add(arr[i][j]);
box[i-i%3+j/3].add(arr[i][j]);
}
static void remove(int i, int j) {
row[i].remove((Object)arr[i][j]);
col[j].remove((Object)arr[i][j]);
box[i-i%3+j/3].remove((Object)arr[i][j]);
}
static void dfs(int num) {
if(num==len) {
finish = true;
return;
}
for(int n=1; n<=9; n++) {
Node now = zero.get(num);
if(!row[now.i].contains(n) && !col[now.j].contains(n) &&
!box[now.i-now.i%3+now.j/3].contains(n)) {
arr[now.i][now.j] = n;
fill(now.i, now.j);
dfs(num+1);
if(finish) return;
remove(now.i, now.j);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0; i<9; i++) {
row[i] = new ArrayList<Integer>();
col[i] = new ArrayList<Integer>();
box[i] = new ArrayList<Integer>();
}
for(int i=0; i<9; i++) {
String[] s = br.readLine().split("");
for(int j=0; j<9; j++) {
arr[i][j] = Integer.parseInt(s[j]);
if(arr[i][j] != 0) fill(i, j);
else zero.add(new Node(i, j));
}
}
len = zero.size();
dfs(0);
StringBuilder sb = new StringBuilder();
for(int i=0; i<9; i++) {
for(int j=0; j<9; j++) sb.append(arr[i][j]);
sb.append("\n");
}
System.out.println(sb);
}
}