🔺 문제
11049번: 행렬 곱셈 순서
첫째 줄에 입력으로 주어진 행렬을 곱하는데 필요한 곱셈 연산의 최솟값을 출력한다. 정답은 231-1 보다 작거나 같은 자연수이다. 또한, 최악의 순서로 연산해도 연산 횟수가 231-1보다 작거나 같
www.acmicpc.net
🔺 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import java.util.*;
import java.io.*;
public class Main {
static int N; // 행렬 개수
static Matrix[] M; // 행렬 저장 클래스 배열
static int[][] D; // i~j번째 행렬까지 최소 연산 횟수 저장
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
N = Integer.parseInt(st.nextToken());
M = new Matrix[N + 1];
D = new int[N + 1][N + 1];
// 테이블 초기화
for(int i = 0 ; i < D.length ; i++) {
for(int j = 0 ; j < D[i].length ; j++) {
D[i][j] = -1;
}
}
// 행렬 입력받기
for(int i = 1 ; i <= N ; i++) {
st = new StringTokenizer(br.readLine()," ");
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
M[i] = new Matrix(r, c);
}
System.out.println(execute(1, N));
}
// Top-Down 방식 점화식 구현
static int execute(int s, int e) {
int result = Integer.MAX_VALUE;
// 계산했던 부분은 다시 계산 X (메모이제이션)
if(D[s][e] != -1)
return D[s][e];
// 행렬 1개일 때
if(s == e)
return 0;
// 행렬 2개일 때 (앞 행렬 row * 뒤 행렬 row * 뒤 행렬 col)
if(s + 1 == e)
return M[s].r * M[s].c * M[e].c;
// 행렬이 3개 이상일 때, 재귀 형태 (Top-Down)
for(int i = s ; i < e ; i++)
result = Math.min(result, execute(s, i) + execute(i + 1, e) + M[s].r * M[i].c * M[e].c);
return D[s][e] = result;
}
// 행렬 정보 저장용 클래스
static class Matrix {
private int r;
private int c;
Matrix(int r, int c) {
this.r = r;
this.c = c;
}
}
}
|
cs |
✅ 해결 아이디어
✔ DP (Top-Down 방식)
- D[ i ][ j ] : i ~ j번째 행렬까지 최소 연산 횟수 저장
행렬 정보 저장용 클래스를 따로 만들었다.
🔺 다른 풀이들
- 행렬 정보 저장용 클래스를 따로 만드는 대신 2차원 배열을 사용하셨다. (재귀, 반복문 코드 둘다 볼 수 있음)
백준 11049 행렬 곱셈 순서 Java
동적 프로그래밍에서 유명한 문제인 행렬 곱셈 순서 문제이다. 2018년도에 알고리즘 수업을 들으면서 한 번 봤던 문제인데 이번에 제대로 이해하려고 노력해보니까 정말 헷갈리고 어려웠다.. 행
dundung.tistory.com
- 2차원 배열 사용
https://www.acmicpc.net/source/26585402
로그인
www.acmicpc.net
- 재귀) 깔끔 풀이 그 잡채
[java] 백준 11049 (행렬 곱셈 순서) Gold 3
Problem : https://www.acmicpc.net/problem/11049 11049번: 행렬 곱셈 순서 첫째 줄에 입력으로 주어진 행렬을 곱하는데 필요한 곱셈 연산의 최솟값을 출력한다. 정답은 231-1 보다 작거나 같은 자연수이다. 또한
gre-eny.tistory.com
- 반복문) 깔끔 코드 그 잡채 (3중 for문) => 복습용!
[BaekJoon] 백준 11049번 행렬 곱셈 순서(Java)
https://www.acmicpc.net/problem/11049 11049번: 행렬 곱셈 순서 첫째 줄에 입력으로 주어진 행렬을 곱하는데 필요한 곱셈 연산의 최솟값을 출력한다. 정답은 231-1 보다 작거나 같은 자연수이다. 또한, 최악
chamggae.tistory.com
💬 느낀 점
아이디어 생각하기까지가 어렵당...
1회독 | 2회독 | 3회독 | 4회독 | 5회독 |
V | 7/10 |
(+7/10 2회독)
흐아아.... 투 포인터로 풀다가 시간 초과 떠서 우짜지..하고
이전 코드 보고 코드 작성했다..ㅠ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import java.util.*;
import java.io.*;
public class Main {
static int N;
static Matrix[] M;
static int[][] D;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N = Integer.parseInt(br.readLine());
M = new Matrix[N + 1];
// dp 배열 초기화
D = new int[N + 1][N + 1];
for (int i = 0; i < D.length ; i++) {
for(int j = 0 ; j < D[i].length ; j++) {
D[i][j] = -1;
}
}
for(int i = 1 ; i <= N ; i++) {
st = new StringTokenizer(br.readLine(), " ");
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
M[i] = new Matrix(r, c);
}
System.out.println(solve(1, N));
}
private static int solve(int s, int e) {
int result = Integer.MAX_VALUE;
if(D[s][e] != -1)
return D[s][e];
if(s == e)
return 0;
if(s + 1 == e)
return M[s].r * M[s].c * M[e].c;
for(int i = s ; i < e ; i++) {
result = Math.min(result, solve(s, i) + solve(i + 1, e) + M[s].r * M[i].c * M[e].c);
}
return D[s][e] = result;
}
static class Matrix {
int r, c;
Matrix(int r, int c) {
this.r = r;
this.c = c;
}
}
}
|
cs |
(참고)
✔ Do it 알고리즘 코딩테스트 자바편
'코테 > 백준' 카테고리의 다른 글
[백준/JAVA] 11758번: CCW (0) | 2023.05.18 |
---|---|
[백준/JAVA] 2098번: 외판원 순회 (0) | 2023.05.17 |
[백준/JAVA] 2342번: Dance Dance Revolution (1) | 2023.05.16 |
[백준/JAVA] 1328번: 고층 빌딩 (0) | 2023.05.16 |
[백준/JAVA] 1915번: 가장 큰 정사각형 (1) | 2023.05.16 |