NASA์์๋ ํ์ฑ ํ์ฌ๋ฅผ ์ํด ํ์ฑ์ ๋ฌด์ ์กฐ์ข ๋ก๋ด์ ๋ณด๋๋ค. ์ค์ ํ์ฑ์ ๋ชจ์ต์ ๊ต์ฅํ ๋ณต์กํ์ง๋ง, ๋ก๋ด์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ผ๋ง ์ ๋๊ธฐ ๋๋ฌธ์ ์งํ์ NรM ๋ฐฐ์ด๋ก ๋จ์ํ ํ์ฌ ์๊ฐํ๊ธฐ๋ก ํ๋ค.
์งํ์ ๊ณ ์ ์ฐจ์ ํน์ฑ์, ๋ก๋ด์ ์์ง์ผ ๋ ๋ฐฐ์ด์์ ์ผ์ชฝ, ์ค๋ฅธ์ชฝ, ์๋์ชฝ์ผ๋ก ์ด๋ํ ์ ์์ง๋ง, ์์ชฝ์ผ๋ก๋ ์ด๋ํ ์ ์๋ค. ๋ํ ํ ๋ฒ ํ์ฌํ ์ง์ญ(๋ฐฐ์ด์์ ํ๋์ ์นธ)์ ํ์ฌํ์ง ์๊ธฐ๋ก ํ๋ค.
๊ฐ๊ฐ์ ์ง์ญ์ ํ์ฌ ๊ฐ์น๊ฐ ์๋๋ฐ, ๋ก๋ด์ ๋ฐฐ์ด์ ์ผ์ชฝ ์ (1, 1)์์ ์ถ๋ฐ์์ผ ์ค๋ฅธ์ชฝ ์๋ (N, M)์ผ๋ก ๋ณด๋ด๋ ค๊ณ ํ๋ค. ์ด๋, ์์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด์, ํ์ฌํ ์ง์ญ๋ค์ ๊ฐ์น์ ํฉ์ด ์ต๋๊ฐ ๋๋๋ก ํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ N, M(1โคN, Mโค1,000)์ด ์ฃผ์ด์ง๋ค. ๋ค์ N๊ฐ์ ์ค์๋ M๊ฐ์ ์๋ก ๋ฐฐ์ด์ด ์ฃผ์ด์ง๋ค. ๋ฐฐ์ด์ ๊ฐ ์๋ ์ ๋๊ฐ์ด 100์ ๋์ง ์๋ ์ ์์ด๋ค. ์ด ๊ฐ์ ๊ทธ ์ง์ญ์ ๊ฐ์น๋ฅผ ๋ํ๋ธ๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ์ต๋ ๊ฐ์น์ ํฉ์ ์ถ๋ ฅํ๋ค.
2๊ฐ์ง ๊ฒฝ์ฐ ๋ชจ๋ ํ์
1) ์ผ์ชฝ โ ์ค๋ฅธ์ชฝ ๋ฐฉํฅ์ผ๋ก ์งํ (left)
2) ์ค๋ฅธ์ชฝ โ ์ผ์ชฝ ๋ฐฉํฅ์ผ๋ก ์งํ (right)
๋ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ค ๋ ํฐ ๊ฐ dp์ ์ ์ฅ
2๊ฐ์ง ๊ฒฝ์ฐ ๋ชจ๋ ํ์
1) ์ผ์ชฝ โ ์ค๋ฅธ์ชฝ ๋ฐฉํฅ์ผ๋ก ์งํ (left)
left[1] = dp[r-1][1] + map[r][1];
for(int c=2; c<m+1; c++)
left[c] = Math.max(left[c-1], dp[r-1][c]) + map[r][c];
2) ์ค๋ฅธ์ชฝ โ ์ผ์ชฝ ๋ฐฉํฅ์ผ๋ก ์งํ (right)
right[m] = dp[r-1][m] + map[r][m];
for(int c=m-1; c>=1; c--)
right[c] = Math.max(right[c+1], dp[r-1][c]) + map[r][c];
๋ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ค ๋ ํฐ ๊ฐ dp์ ์ ์ฅ
for(int c=1; c<m+1; c++)
dp[r][c] = Math.max(left[c], right[c]);
import java.io.*;
public class DP_9 {
static int map[][], dp[][], left[], right[];
static int n, m;
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
map = new int[n+1][m+1];
dp = new int[n+1][m+1];
left = new int[m+1];
right = new int[m+1];
for(int i=1; i<n+1; i++) {
s = br.readLine().split(" ");
for(int j=1; j<m+1; j++) {
map[i][j] = Integer.parseInt(s[j-1]);
}
}
for(int i=1; i<m+1; i++)
dp[1][i] = (dp[1][i-1] + map[1][i]);
System.out.println(control());
}
static int control() {
for(int r=2; r<n+1; r++) {
left[1] = dp[r-1][1] + map[r][1];
for(int c=2; c<m+1; c++)
left[c] = Math.max(left[c-1], dp[r-1][c]) + map[r][c];
right[m] = dp[r-1][m] + map[r][m];
for(int c=m-1; c>=1; c--)
right[c] = Math.max(right[c+1], dp[r-1][c]) + map[r][c];
for(int c=1; c<m+1; c++)
dp[r][c] = Math.max(left[c], right[c]);
}
return dp[n][m];
}
}
์ฑ๊ณต~