Skip to content

Commit e712aa0

Browse files
authored
Merge pull request #768 from AlgorithmWithGod/zinnnn37
[20250829] BOJ / G1 / 그림 교환 / 김민진
2 parents 76f5a80 + 7afb102 commit e712aa0

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
```java
2+
import java.io.*;
3+
4+
public class Main {
5+
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
9+
private static int N;
10+
private static int size;
11+
private static int[][] arr;
12+
private static int[][] dp;
13+
14+
public static void main(String[] args) throws IOException {
15+
init();
16+
sol();
17+
}
18+
19+
private static void init() throws IOException {
20+
N = Integer.parseInt(br.readLine());
21+
arr = new int[N][N];
22+
23+
for (int i = 0; i < N; i++) {
24+
String s = br.readLine();
25+
for (int j = 0; j < N; j++) {
26+
arr[i][j] = s.charAt(j) - '0';
27+
}
28+
}
29+
30+
size = (int) Math.pow(2, N);
31+
dp = new int[size][N];
32+
}
33+
34+
private static void sol() throws IOException {
35+
int ans = dfs(0, 0, 0) + 1;
36+
bw.write(ans + "");
37+
bw.flush();
38+
bw.close();
39+
br.close();
40+
}
41+
42+
private static int dfs(int buyer, int owners, int price) {
43+
// 현재 구매자가 그림 구매
44+
owners |= (1 << buyer);
45+
46+
// 이미 구매 함
47+
if (dp[owners][buyer] != 0) {
48+
return dp[owners][buyer];
49+
}
50+
51+
// 다음 구매자 구하기
52+
for (int i = 0; i < N; i++) {
53+
// 다음 구매자가 구매 가능 && 같거나 비싼 가격으로 구매 가능
54+
if ((owners & (1 << i)) == 0 && arr[buyer][i] >= price) {
55+
dp[owners][buyer] = Math.max(dp[owners][buyer], dfs(i, owners, arr[buyer][i]) + 1);
56+
}
57+
}
58+
return dp[owners][buyer];
59+
}
60+
61+
}
62+
```

0 commit comments

Comments
 (0)