Skip to content

Commit afc4f7e

Browse files
committed
[Gold IV] Title: 뮤탈리스크, Time: 116 ms, Memory: 16568 KB -BaekjoonHub
1 parent ab32472 commit afc4f7e

2 files changed

Lines changed: 39 additions & 55 deletions

File tree

백준/Gold/12869. 뮤탈리스크/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 15764 KB, 시간: 120 ms
7+
메모리: 16568 KB, 시간: 116 ms
88

99
### 분류
1010

1111
다이나믹 프로그래밍, 그래프 이론, 그래프 탐색, 너비 우선 탐색
1212

1313
### 제출 일자
1414

15-
2025년 11월 20일 15:40:10
15+
2025년 11월 26일 10:43:42
1616

1717
### 문제 설명
1818

백준/Gold/12869. 뮤탈리스크/뮤탈리스크.java

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,64 @@ class Main {
66
static int N;
77
static int[] health;
88
static int ans;
9-
static boolean[][][] visited;
10-
static Queue<int[]> Q;
9+
static int[][][] dp;
1110
static List<int[]> perms = new ArrayList<>();
1211
static int[] value = new int[]{9, 3, 1};
1312

1413
public static void main(String[] args) throws Exception {
1514
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
1615
N = Integer.parseInt(br.readLine());
1716

18-
visited = new boolean[61][61][61];
17+
dp = new int[61][61][61];
18+
for (int i = 0; i < 61; i++)
19+
for (int j = 0; j < 61; j++)
20+
Arrays.fill(dp[i][j], -1);
21+
dp[0][0][0] = 0;
1922

2023
health = new int[4];
2124
StringTokenizer st = new StringTokenizer(br.readLine());
2225
for (int i = 0; i < N; i++) {
2326
health[i] = Integer.parseInt(st.nextToken());
2427
}
2528
health[3] = 0;
26-
2729
getPerms();
28-
// for(int[] a : perms) {t
29-
// System.out.println(Arrays.toString(a));
30-
// }
31-
32-
Q = new ArrayDeque<>();
33-
Q.offer(health);
34-
visited[health[0]][health[1]][health[2]] = true;
35-
while (!Q.isEmpty()) {
36-
// 종료 조건
37-
int[] curH = Q.poll();
38-
// System.out.println(Arrays.toString(curH));
39-
if (allDead(curH)) {
40-
ans = curH[3];
41-
break;
42-
}
4330

44-
for (int[] p : perms) {
45-
int[] nextH = Arrays.copyOf(curH, 4);
46-
47-
//p = 1 2 0 => 각 인덱스의 피격 순서
48-
//pos = 2 0 1 => 순서에 해당하는 숫자의 위치
49-
// int[] pos = new int[4];
50-
// for (int i = 0; i < N; i++) {
51-
// pos[p[i]] = i;
52-
// }
53-
// if (N >= 2) {
54-
// if (health[pos[0]] == 0)
55-
// continue;
56-
// //첫번째는 0이 아니어야 함.
57-
// }
58-
// if (N >= 3) {
59-
// if (health[pos[2]] != 0 && health[pos[1]] == 0)
60-
// continue;
61-
// //3번째가 0이 아니면 2번째도 아니어야 함.
62-
// }
63-
64-
for (int i = 0; i < N; i++) {
65-
nextH[i] = Math.max(0, nextH[i] - value[p[i]]);
66-
}
67-
68-
if(visited[nextH[0]][nextH[1]][nextH[2]]) {
69-
// System.out.println("visited");
70-
continue;
71-
}
72-
73-
visited[nextH[0]][nextH[1]][nextH[2]] = true;
74-
nextH[3]++;
75-
Q.offer(nextH);
31+
dfs(health[0], health[1], health[2]);
32+
33+
System.out.println(dp[health[0]][health[1]][health[2]]);
34+
}
7635

36+
private static int dfs(int h1, int h2, int h3) {
37+
if (dp[h1][h2][h3] != -1)
38+
return dp[h1][h2][h3];
39+
40+
int ret = Integer.MAX_VALUE;
41+
for (int[] p : perms) {
42+
// System.out.println(Arrays.toString(p));
43+
int v1 = 0;
44+
int v2 = 0;
45+
int v3 = 0;
46+
if (N == 1) {
47+
v1 = value[p[0]];
48+
v2 = 0;
49+
v3 = 0;
50+
} else if (N == 2) {
51+
v1 = value[p[0]];
52+
v2 = value[p[1]];
53+
v3 = 0;
54+
} else if (N == 3) {
55+
v1 = value[p[0]];
56+
v2 = value[p[1]];
57+
v3 = value[p[2]];
7758
}
59+
60+
ret = Math.min(ret, dfs(Math.max(0, h1 - v1), Math.max(0, h2 - v2), Math.max(0, h3 - v3)));
7861
}
7962

80-
System.out.println(ans);
63+
return dp[h1][h2][h3] = 1 + ret;
8164
}
8265

66+
8367
static boolean[] V;
8468
static int[] p;
8569

@@ -115,4 +99,4 @@ private static boolean allDead(int[] health) {
11599
}
116100

117101

118-
}
102+
}

0 commit comments

Comments
 (0)