Skip to content

Commit 0838670

Browse files
authored
[BOJ] 28447 마라탕 재료 고르기 (S2)
1 parent fab3ab2 commit 0838670

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

정건우/4주차/260123.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//https://www.acmicpc.net/problem/28447
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.StringTokenizer;
6+
7+
public class BOJ_S2_28447_마라탕재료고르기 {
8+
static int N, M, max;
9+
static boolean [] picked;
10+
static int [][] taste;
11+
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
StringTokenizer st = new StringTokenizer(br.readLine());
15+
16+
N = Integer.parseInt(st.nextToken());
17+
M = Integer.parseInt(st.nextToken());
18+
picked = new boolean[N];
19+
taste = new int[N][N];
20+
max = Integer.MIN_VALUE;
21+
22+
for (int i = 0; i < N; i++) {
23+
st = new StringTokenizer(br.readLine());
24+
for (int j = 0; j < N; j++) {
25+
taste[i][j] = Integer.parseInt(st.nextToken());
26+
}
27+
}
28+
29+
combi(0, 0, 0);
30+
31+
System.out.println(max);
32+
33+
}
34+
35+
private static void combi(int start, int depth, int sum) {
36+
if (depth == M) {
37+
max = Math.max(max, sum);
38+
return;
39+
}
40+
41+
for (int i = start; i < N; i++) {
42+
if(picked[i]) continue;
43+
44+
for (int j = 0; j < N; j++) {
45+
if(picked[j]) sum += taste[i][j];
46+
}
47+
48+
picked[i] = true;
49+
50+
combi(i+1, depth + 1, sum);
51+
52+
picked[i] = false;
53+
54+
for (int j = 0; j < N; j++) {
55+
if (picked[j]) sum -= taste[i][j];
56+
}
57+
58+
}
59+
60+
}
61+
}

0 commit comments

Comments
 (0)