Skip to content

Commit 71eec24

Browse files
authored
Merge pull request #10 from AlgorithmWithGod/suyeun84
[20250203] BOJ / 골드4 / 다이어트 / 김수연
2 parents 85a66fd + 959438c commit 71eec24

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class boj19942
6+
{
7+
static int N;
8+
static Ingredient[] list;
9+
static ArrayList<ArrayList<Integer>> combinations;
10+
11+
public static void main(String[] args) throws Exception{
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
int[] target = new int[4];
15+
int answer = Integer.MAX_VALUE;
16+
List<String> answer2 = new ArrayList<>();
17+
N = Integer.parseInt(st.nextToken());
18+
st = new StringTokenizer(br.readLine());
19+
for (int i = 0; i < 4; i++) {
20+
target[i] = Integer.parseInt(st.nextToken());
21+
}
22+
23+
list = new Ingredient[N];
24+
for (int i = 0; i < N; i++) {
25+
st = new StringTokenizer(br.readLine());
26+
list[i] = new Ingredient(i, Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
27+
}
28+
29+
//combinations
30+
for (int i = 1; i <= N; i++) {
31+
//식재료 선택 갯수
32+
combinations = new ArrayList<>();
33+
dfs(new ArrayList<Integer>(), i, 0);
34+
for (List<Integer> ingredient : combinations) {
35+
int mp = 0;
36+
int mf = 0;
37+
int ms = 0;
38+
int mv = 0;
39+
int mc = 0;
40+
ArrayList<Integer> temp2 = new ArrayList<>();
41+
for (int idx : ingredient) {
42+
mp += list[idx].p;
43+
mf += list[idx].f;
44+
ms += list[idx].s;
45+
mv += list[idx].v;
46+
mc += list[idx].c;
47+
temp2.add(list[idx].idx+1);
48+
}
49+
if (mp >= target[0] && mf >= target[1] && ms >= target[2] && mv >= target[3]) {
50+
if (answer > mc) {
51+
answer = mc;
52+
answer2.clear();
53+
Collections.sort(temp2);
54+
String sb = "";
55+
for (int t : temp2) {
56+
sb = sb + t + " ";
57+
}
58+
answer2.add(sb);
59+
} else if (answer == mc) {
60+
Collections.sort(temp2);
61+
String sb = "";
62+
for (int t : temp2) {
63+
sb = sb + t + " ";
64+
}
65+
answer2.add(sb);
66+
}
67+
}
68+
}
69+
combinations.clear();
70+
}
71+
if (answer != Integer.MAX_VALUE) {
72+
System.out.println(answer);
73+
Collections.sort(answer2);
74+
System.out.println(answer2.get(0));
75+
} else {
76+
System.out.println(-1);
77+
}
78+
}
79+
80+
static void dfs(ArrayList<Integer> curr, int i, int idx) {
81+
if (curr.size() == i) {
82+
combinations.add(curr);
83+
return;
84+
}
85+
for (int k = idx; k < N; k++) {
86+
ArrayList<Integer> newList = new ArrayList<>();
87+
for (int c : curr) {
88+
newList.add(c);
89+
}
90+
newList.add(list[k].idx);
91+
dfs(newList, i, k+1);
92+
}
93+
}
94+
95+
static class Ingredient {
96+
int idx, p, f, s, v, c;
97+
98+
public Ingredient(int idx, int p, int f, int s, int v, int c) {
99+
this.idx = idx;
100+
this.p = p;
101+
this.f = f;
102+
this.s = s;
103+
this.v = v;
104+
this.c = c;
105+
}
106+
}
107+
}
108+
109+
```

0 commit comments

Comments
 (0)