[Java] BOJ 11049 - ํ๋ ฌ ๊ณฑ์
์์Problem Solving/Baekjoon Online Judge (BOJ)2025. 5. 1. 14:28
Table of Contents

01. ๐ ๋ฌธ์ ํ์
N: ํ๋ ฌ์ ๊ฐ์ $(1 \leq N \leq 500)$
r, c: ํ๋ ฌ์ ํฌ๊ธฐ $(1 \leq r, c \leq 500)$
- ํ๋ ฌ์ ๊ณฑ์ ์ํํ์ ๋, ์์์ ๋ฐ๋ผ ์ฐ์ฐ ํ์๊ฐ ๋ฌ๋ผ์ง๊ฒ ๋๋๋ฐ, ์ฐ์ฐ ํ์๊ฐ ๊ฐ์ฅ ๋ฎ์์ง๋ ์์๋ก ๊ณ์ฐํ์์ ๋ ๋์ค๋ ์ต์ ์ฐ์ฐ ํ์๋ฅผ ์ถ๋ ฅ
01-1. ์๊ณ ๋ฆฌ์ฆ ์ ํ & ์๊ฐ ๋ณต์ก๋
- ์ด์ ํ๋ ฌ ๊ณฑ์ ์ฐ์ฐ ํ์๋ฅผ ๋ค์ ์ฐ์ฐ ํ์์ ์ด์ฉํด ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก, DP๋ก ํด๊ฒฐํ ์ ์์
- dp๋ฐฐ์ด์ 2์ฐจ์ ๋ฐฐ์ด๋ก ๋ํ๋ผ ์ ์์ผ๋ฉฐ, i์ j๋ i๋ฒ์งธ ํ๋ ฌ๋ถํฐ j๋ฒ์งธ ํ๋ ฌ๊น์ง ํ๋ ฌ ๊ณฑ์ ์งํํ์๋, ์ต์ ์ฐ์ฐ ํ์๋ฅผ ๋ด์
- i๋ถํฐ j๊น์ง ๋ฒ์์ ํ๋ ฌ ๊ณฑ ์ต์ ์ฐ์ฐ ํ์ dp[i][j]๋ ์๋์ ๊ฐ์ ์ ํ์์ผ๋ก ๋ํ๋ผ ์ ์์
$dp[i][j] = min_{k=i}^{j-1}(dp[i][k] + dp[k+1][j] + cost)$
$cost = row[i] * col[i] * col[j]$
02. ๐ ์ฝ๋ ์ค๊ณํ๊ธฐ
- ํ๋ ฌ์ ๊ฐ์ N๊ณผ ํ๋ ฌ์ ์ ๋ณด r, c๋ฅผ ์์ฐจ์ ์ผ๋ก ์
๋ ฅ
- ์ ๋ ฅ ์, dp[i][i]๋ ์์ง ์ฐ์ฐ์ด ์๋ ์ํ์ด๋ฏ๋ก ์ฐ์ฐ ํ์๋ 0์ด๋ฉฐ, r๊ณผ c๋ฅผ ์ ์ฅ
- dp[i][j]์ ๋ํ์ฌ ์์ฐจ์ ์ผ๋ก ํ์ ์์
- k(1~j-1)๋ฅผ ์์ฐจ์ ์ผ๋ก ํ์ํ๋ฉฐ, dp[i][k] + dp[k+1][j] + cost๋ฅผ ๊ณ์ฐํ์ฌ ๋์จ ์ต์ ๊ฐ์ dp[i][j]์ ์ต์๊ฐ์ผ๋ก ํจ
(์ด ๋, cost๋ ๋ ํ๋ ฌ dp[i][k]๊ณผ dp[k+1][j]์ ํ๋ ฌ ๊ณฑํ ์ฐ์ฐ ํ์๋ฅผ ๋ํ๋)
- k(1~j-1)๋ฅผ ์์ฐจ์ ์ผ๋ก ํ์ํ๋ฉฐ, dp[i][k] + dp[k+1][j] + cost๋ฅผ ๊ณ์ฐํ์ฌ ๋์จ ์ต์ ๊ฐ์ dp[i][j]์ ์ต์๊ฐ์ผ๋ก ํจ
- dp[1][N]๋ 1~N๊น์ง ํ๋ ฌ์ ํ๋ ฌ ๊ณฑํ ์ต์ ์ฐ์ฐ ํ์๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฏ๋ก, ์ถ๋ ฅ
03. ๐ ๏ธ ์๋๋ณ ์์
1ํ์ฐจ
04. ๐๏ธ ์ ๋ต ์ฝ๋
import java.io.*;
import java.util.*;
public class boj11049 {
public static class Matrix{
private int r, c;
private long value;
Matrix(int r, int c, long val){
this.r = r;
this.c = c;
this.value = val;
}
void setMatrix(int r, int c, long val){
this.r = r;
this.c = c;
this.value = val;
}
long getValue(){
return this.value;
}
int getRow(){
return this.r;
}
int getCol(){
return this.c;
}
}
public static long mult(Matrix a, Matrix b){
return a.getRow() * a.getCol() * b.getCol() + a.getValue() + b.getValue();
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int N = Integer.valueOf(br.readLine());
int r, c;
Matrix[][] dp = new Matrix[N+1][N+1];
for(int i = 1; i <= N; i++){
st = new StringTokenizer(br.readLine());
r = Integer.valueOf(st.nextToken());
c = Integer.valueOf(st.nextToken());
dp[i][i] = new Matrix(r, c, 0);
}
for(int i = 1; i < N; i++){
for(int j = 1; j <= N-i; j++){
long curValue = mult(dp[j][j], dp[j+1][j+i]);
int curRow = dp[j][j].getRow(), curCol = dp[j+1][j+i].getCol();
dp[j][j+i] = new Matrix(curRow, curCol, curValue);
for(int k = 1; k <= i-1; k++){
curValue = mult(dp[j][j+k], dp[j+k+1][j+i]);
curRow = dp[j][j+k].getRow(); curCol = dp[j+k+1][j+i].getCol();
if(dp[j][j+i].getValue() > curValue)
dp[j][j+i].setMatrix(curRow, curCol, curValue);
}
}
}
bw.write(dp[1][N].getValue() + "\n");
bw.flush();
bw.close();
br.close();
}
}
'Problem Solving > Baekjoon Online Judge (BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Java] BOJ 1202 - ๋ณด์ ๋๋ (0) | 2025.05.06 |
|---|---|
| [Java] BOJ 2263 - ํธ๋ฆฌ์ ์ํ (0) | 2025.05.03 |
| [Java] BOJ 1005 - ACM Craft (0) | 2025.04.29 |
| [Java] BOJ 2842 - ์ง๋ฐฐ์ ํ์๋ (0) | 2025.04.27 |
| [Java] BOJ 12014 - ์ฃผ์ (0) | 2025.04.26 |
@ONE_ :: ์ ํธ์
์๋ชป๋ ์ ๋ณด๊ฐ ์๋ค๋ฉด ๋ง์ํด์ฃผ์ธ์!