|
| 1 | +package ps_baekjoon; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.ArrayDeque; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Queue; |
| 9 | +import java.util.Stack; |
| 10 | +import java.util.StringTokenizer; |
| 11 | +import java.util.Vector; |
| 12 | + |
| 13 | +public class Main { |
| 14 | + |
| 15 | + static StringBuilder sb = new StringBuilder(); |
| 16 | + |
| 17 | + static Vector<Coor> zeros = new Vector<>(); |
| 18 | + static int[][] arr; |
| 19 | + |
| 20 | + static class Coor { |
| 21 | + int x; |
| 22 | + int y; |
| 23 | + |
| 24 | + Coor(int x, int y) { |
| 25 | + this.x = x; |
| 26 | + this.y = y; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + static void printArr() { |
| 31 | + for (int i = 0; i < 9; i++) { |
| 32 | + for (int j = 0; j < 9; j++) { |
| 33 | + sb.append(arr[i][j]).append(' '); |
| 34 | + } |
| 35 | + sb.append("\n"); |
| 36 | + } |
| 37 | + |
| 38 | + System.out.println(sb); |
| 39 | + } |
| 40 | + |
| 41 | + static boolean col(int num, int x, int y) { |
| 42 | + for (int i = 0; i < 9; i++) { |
| 43 | + if (arr[x][i] == num) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + static boolean row(int num, int x, int y) { |
| 52 | + for (int i = 0; i < 9; i++) { |
| 53 | + if (arr[i][y] == num) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + static boolean square(int num, int x, int y) { |
| 62 | + int sx = (x / 3) * 3; |
| 63 | + int sy = (y / 3) * 3; |
| 64 | + |
| 65 | + for (int i = sx; i < sx + 3; i++) { |
| 66 | + for (int j = sy; j < sy + 3; j++) { |
| 67 | + if (arr[i][j] == num) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + static boolean ok(int num, int x, int y) { |
| 77 | + return col(num, x, y) && row(num, x, y) && square(num, x, y); |
| 78 | + } |
| 79 | + |
| 80 | + static void dfs(int cnt) { |
| 81 | + if (cnt == zeros.size()) { |
| 82 | + printArr(); |
| 83 | + System.exit(0); |
| 84 | + } |
| 85 | + |
| 86 | + Coor cur = zeros.get(cnt); |
| 87 | + |
| 88 | + for (int i = 1; i <= 9; i++) { |
| 89 | + if (ok(i, cur.x, cur.y)) { |
| 90 | + arr[cur.x][cur.y] = i; |
| 91 | + |
| 92 | + dfs(cnt + 1); |
| 93 | + |
| 94 | + arr[cur.x][cur.y] = 0; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static void main(String[] args) throws IOException { |
| 100 | + |
| 101 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 102 | + |
| 103 | + arr = new int[9][9]; |
| 104 | + |
| 105 | + for (int i = 0; i < 9; i++) { |
| 106 | + String line = br.readLine(); |
| 107 | + StringTokenizer st = new StringTokenizer(line); |
| 108 | + for (int j = 0; j < 9; j++) { |
| 109 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 110 | + |
| 111 | + if (arr[i][j] == 0) { |
| 112 | + zeros.add(new Coor(i, j)); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + dfs(0); |
| 118 | + } |
| 119 | +} |
0 commit comments