-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon2580.java
109 lines (91 loc) · 3.12 KB
/
Baekjoon2580.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
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
/**
* https://www.acmicpc.net/problem/2580
* 백준 2580 스도쿠
*/
public class Baekjoon2580 {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static int[][] sudoku = new int[9][9];
private static List<Node> shouldDecide = new ArrayList<>();
private static boolean[] decided;
private static boolean isPrinted = false;
public static void main(String[] args) throws IOException {
for (int i = 0; i < 9; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < 9; j++) {
sudoku[i][j] = Integer.parseInt(st.nextToken());
if (sudoku[i][j] == 0) shouldDecide.add(new Node(i, j));
}
}
decided = new boolean[shouldDecide.size()];
for (int i = 1; i <= 9; i++) {
if (isAvailable(i, 0)) {
sudoku[shouldDecide.get(0).x][shouldDecide.get(0).y] = i;
decided[0] = true;
dfs(1);
decided[0] = false;
sudoku[shouldDecide.get(0).x][shouldDecide.get(0).y] = 0;
}
}
}
private static boolean isAvailable(int number, int indexOfShoudDecide) {
int x = shouldDecide.get(indexOfShoudDecide).x;
int y = shouldDecide.get(indexOfShoudDecide).y;
for (int i = 0; i < 9; i++) {
if (sudoku[x][i] == number || sudoku[i][y] == number) return false;
}
if (!checkSquare(x, y, number)) {
return false;
}
return true;
}
public static boolean checkSquare(int r, int c, int num) {
r = r / 3;
c = c / 3;
for (int rr = r * 3; rr < (r * 3) + 3; rr++) {
for (int cc = c * 3; cc < (c * 3) + 3; cc++) {
if (sudoku[rr][cc] == num) {
return false;
}
}
}
return true;
}
private static void dfs(int index) {
if (index == shouldDecide.size()) {
for (int i = 0; i < 9; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < 9; j++) {
sb.append(sudoku[i][j]).append(" ");
}
System.out.println(sb.toString().trim());
}
isPrinted = true;
return;
}
if (isPrinted) return;
for (int i = 1; i <= 9; i++) {
if (isAvailable(i, index)) {
sudoku[shouldDecide.get(index).x][shouldDecide.get(index).y] = i;
decided[index] = true;
dfs(index + 1);
decided[index] = false;
sudoku[shouldDecide.get(index).x][shouldDecide.get(index).y] = 0;
}
}
}
private static class Node {
int x, y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
}