728x90
๐๋ฌธ์ ๋งํฌ
๐ป์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
int[][] dp = new int[n][m];
dp[0][0] = map[0][0];
//dp[0][0~m]์ ๊ตฌํ๋ ๊ณผ์
for (int i = 1; i < m; i++) {
dp[0][i] = dp[0][i - 1] + map[0][i];
}
//dp[1~n][0~m]์ ๊ตฌํ๋ ๊ณผ์
for (int i = 1; i < n; i++) {
int[] left = new int[m];
int[] right = new int[m];
//์ผ์ชฝ ๋ถํฐ ํ์(์ค๋ฅธ์ชฝ ๋ฐฉํฅ์ผ๋ก ํ์)
left[0] = dp[i - 1][0] + map[i][0];
for (int j = 1; j < m; j++) {
left[j] = Math.max(dp[i - 1][j], left[j - 1]) + map[i][j];
}
//์ค๋ฅธ์ชฝ ๋ถํฐ ํ์(์ผ์ชฝ ๋ฐฉํฅ์ผ๋ก ํ์)
right[m - 1] = dp[i - 1][m - 1] + map[i][m - 1];
for (int j = m - 2; j > -1; j--) {
right[j] = Math.max(dp[i - 1][j], right[j + 1]) + map[i][j];
}
for (int j = 0; j < m; j++) {
dp[i][j] = Math.max(left[j], right[j]);
}
}
System.out.println(dp[n - 1][m - 1]);
}
}
โณํ๊ณ
- ์ค๋ฅธ์ชฝ ๋ฐฉํฅ์ผ๋ก๋ง ์ด๋ํ๋ฉด ์ฌ์ด ๋ฌธ์ ๋ผ๊ณ ์๊ฐํ๋๋ฐ, ์ผ์ชฝ ์ค๋ฅธ์ชฝ ๋ชจ๋ ์์ง์ผ ์ ์์ด์ ์ด๋ ค์ ๋ค.
- ๋จ์ํ๊ฒ ์ผ์ชฝ, ์ค๋ฅธ์ชฝ ๋ฐฉํฅ ๋ชจ๋ ๊ณ์ฐํ ํ ์ต๋๊ฐ์ ๊ตฌํ๋ฉด ๋๋ค.
728x90
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm/Baekjoon] ๋๋ก์ ์ ํธ๋ฑ - 2980 (S4/JAVA) (0) | 2023.06.27 |
---|---|
[Algorithm/Baekjoon] ์ค์ธ์ฐ๊ธฐ - 10431 (S5/JAVA) (0) | 2023.06.26 |
[Algorithm/Baekjoon] ํํฐ - 1238 (G3/JAVA) (0) | 2023.06.22 |
[Algorithm/Baekjoon] ๊ฒน์น๋ ๊ฑด ์ซ์ด - 20922 (S1/JAVA) (0) | 2023.06.21 |
[Algorithm/Baekjoon] ๋ ๊ฒ์ - 9655 (S5/JAVA) (2) | 2023.06.19 |