BOJ_19949_영재의 시험 (Java)
BOJ_19949_영재의 시험 (Java)
[Silver II] 영재의 시험 - 19949
성능 요약
메모리: 15620 KB, 시간: 196 ms
분류
백트래킹, 브루트포스 알고리즘
제출 일자
2025년 3월 5일 15:56:25
문제 설명
컴퓨터공학과 학생인 영재는 이번 학기에 알고리즘 수업을 수강한다.
평소에 자신의 실력을 맹신한 영재는 시험 전날까지 공부를 하지 않았다.
당연하게도 문제를 하나도 풀지 못하였지만 다행히도 문제가 5지 선다의 객관식 10문제였다.
찍기에도 자신 있던 영재는 3개의 연속된 문제의 답은 같지 않게 한다는 자신의 비법을 이용하여 모든 문제를 찍었다.
이때 영재의 점수가 5점 이상일 경우의 수를 구하여라.
문제의 점수는 1문제당 1점씩이다.
입력
시험의 정답이 첫 줄에 주어진다.
출력
영재의 점수가 5점 이상일 경우의 수를 출력하여라.
문제 풀이
코드
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
/**
* Author: nowalex322, Kim HyeonJae
*/
import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static StringTokenizer st;
static int arr[], select[], res;
public static void main(String[] args) throws Exception {
new Main().solution();
}
public void solution() throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("src/main/java/BOJ_19949_영재의시험/input.txt")));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
arr = new int[10];
select = new int[10];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < 10; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
dfs(0, 0);
System.out.println(res);
bw.flush();
bw.close();
br.close();
}
private void dfs(int idx, int cnt) {
if(idx == 10){
if(cnt >= 5) {
res++;
}
return;
}
for(int i=1; i<=5; i++){
if(idx >= 2 && select[idx-2] == i && select[idx-1] == i) continue;
select[idx] = i;
if(arr[idx] == i) dfs(idx+1, cnt+1);
else dfs(idx+1, cnt);
}
}
}
This post is licensed under
CC BY 4.0
by the author.