• 문제 링크
https://www.acmicpc.net/problem/19675
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
public class Main {
static int[][] mat = new int[3][3];
static List<Pair> list = new ArrayList<>();
static int cnt;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if ((mat[i][j] = read()) == 0) list.add(new Pair(i, j));
recur(0);
bw.write(String.valueOf(cnt));
bw.flush();
}
private static void recur(int idx) {
if (idx == list.size()) {
cnt++;
return;
}
Pair p = list.get(idx);
boolean[] arr = mark(p);
for (int i = 1; i < 10; i++) {
if (arr[i]) continue;
mat[p.y][p.x] = i;
recur(idx + 1);
mat[p.y][p.x] = 0;
}
}
private static boolean[] mark(Pair p) {
boolean[] arr = new boolean[10];
for (int i = 0; i < 3; i++) {
arr[mat[p.y][i]] = true;
arr[mat[i][p.x]] = true;
}
return arr;
}
private static class Pair {
int y, x;
Pair(int y, int x) {
this.y = y;
this.x = x;
}
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 8959 Stuttering Strings - Backtracking / Java (0) | 2024.09.11 |
---|---|
[백준] 6977 Pattern Generator - Backtracking / Java (0) | 2024.09.10 |
[백준] 5351 Coin Row - Backtracking / Java (0) | 2024.09.08 |
[백준] 4391 Y2K Accounting Bug - Backtracking / Java (0) | 2024.09.07 |
[백준] 22970 문제 재탕 - Brute Force / Java (0) | 2024.09.06 |
댓글