https://www.acmicpc.net/problem/16938
16938번: 캠프 준비
난이도가 10, 30인 문제를 고르거나, 20, 30인 문제를 고르면 된다.
www.acmicpc.net
저는 해당 문제를 조합을 이용하여 각각의 시험을 사용하는 것과 안하는 것을 dfs로 구현하고 다 뽑았다면 조건들이 맞는지 확인하고 각각의 조건들이 모두 충족한다면 1을 반환하여 가능한 결과값의 개수를 찾았습니다.
package BOJ.ETC;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_16938 {
private static int comb(int[] input, int depth, int n, int l , int r, int x, boolean[] visited){
if(depth == n){
int sum = 0;
int min = Integer.MAX_VALUE;
int max = 0;
for(int i = 0 ; i < n ; i++){
if(visited[i]){
sum += input[i];
min = Math.min(min,input[i]);
max = Math.max(max,input[i]);
}
}
if(max - min < x){
return 0;
}
if(sum >= l && sum <= r){
return 1;
}
return 0;
}
int result = 0;
visited[depth] = true;
result += comb(input,depth+1,n,l,r,x,visited);
visited[depth] = false;
result += comb(input,depth+1,n,l,r,x,visited);
return result;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int l = stoi.apply(st.nextToken());
int r = stoi.apply(st.nextToken());
int x = stoi.apply(st.nextToken());
int[] input = new int[n];
st = new StringTokenizer(br.readLine()," ");
for(int i = 0 ; i < n ; i++){
input[i] = stoi.apply(st.nextToken());
}
boolean[] visited = new boolean[n];
System.out.println(comb(input,0,n,l,r,x,visited));
}
}
'알고리즘' 카테고리의 다른 글
백준 2352번 반도체 설계 (JAVA) (0) | 2022.02.11 |
---|---|
백준 13904번 과제 (JAVA) (0) | 2022.02.10 |
백준 11441번 합 구하기 (JAVA) (0) | 2022.02.08 |
백준 16933번 벽 부수고 이동하기3 (JAVA) (0) | 2022.02.07 |
백준 9375번 패션왕 신해빈 (JAVA) (0) | 2022.02.06 |