Problem Solving/Baekjoon
[백준] 13908 비밀번호 - Brute Force / Java
graycode
2024. 1. 24. 11:52
• 문제 링크
13908번: 비밀번호
첫 번째 예제의 경우 가능한 비밀번호의 조합은 07, 17, 27, 37, 47, 57, 67, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 87, 97이다. 두 번째 예제의 경우 가능한 비밀번호의 조합은 34, 43이다.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
static int n, m, res;
static boolean[] visit = new boolean[10];
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
n = read();
m = read();
for (int i = 0; i < m; i++) visit[read()] = true;
recur(0, 0);
bw.write(String.valueOf(res));
bw.flush();
}
private static void recur(int depth, int cnt) {
if (depth == n) {
if (cnt == m) res++;
return;
}
for (int i = 0; i < 10; i++) {
if (visit[i]) {
visit[i] = false;
recur(depth + 1, cnt + 1);
visit[i] = true;
} else recur(depth + 1, cnt);
}
}
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;
}
}