์‹ค๋ฒ„1 : ๋™์  ๊ณ„ํš๋ฒ• ๋ฌธ์ œ์ด๋‹ค.

์ƒ๊ฐ

DFS๋‚˜ BFS๋กœ๋Š” depth๊ฐ€ ๊นŠ์–ด์ ธ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค. ์ด ๋ฌธ์ œ๋Š” ๋™์ ๊ณ„ํš๋ฒ•์œผ๋กœ ํ’€์–ด์•ผ ํ•œ๋‹ค. ๊ทธ์ € ์ตœ๋Œ€ ์‚ฌํƒ•์˜ ๊ฐœ์ˆ˜๋งŒ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ •์˜

์ด ๋ฌธ์ œ๋Š” ์œ„์น˜์— ๋”ฐ๋ผ ๊ฐœ์ˆ˜๊ฐ€ ๊ฒฐ์ •๋˜๋ฏ€๋กœ dp์˜ ์ธ์ž๋ฅผ ์ขŒํ‘œ์˜ ์œ„์น˜๋กœ ์žก๋Š” ๊ฒƒ์ด ์ˆ˜์›”ํ•˜๋‹ค.

dp[y][x] = (y,x)์œ„์น˜์—์„œ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์‚ฌํƒ• ๊ฐœ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’

์ ํ™”์‹

์ด ์ •์˜์— ๋ถ€ํ•ฉํ•˜๋Š” ์ ํ™”์‹์€ ์–ด๋–ค์‹์œผ๋กœ ์งค ์ˆ˜ ์žˆ์„๊นŒ? ์ ํ™”์‹์€ ์ž„์˜์˜ ์ขŒํ‘œ ์œ„์น˜์—์„œ ์ด ์ขŒํ‘œ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ธฐ ์œ„ํ•ด ์–ด๋–ค ์ขŒํ‘œ์™€์˜ ์—ฐ๊ด€์„ฑ์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค. ๋ฌธ์ œ์—์„œ ํŠน์ • ์œ„์น˜์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์€ ์šฐ, ์ขŒ, ๋Œ€๊ฐ์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ํŠน์ • ์œ„์น˜์—์„œ์˜ ์‚ฌํƒ•์˜ ์ตœ๋Œ“๊ฐ’์€ ์ขŒ, ์šฐ, 10์‹œ๋ฐฉํ–ฅ ๋Œ€๊ฐ์„  ์„ธ ๋ฐฉํ–ฅ์˜ ๊ฐ’์œผ๋กœ ๋ถ€ํ„ฐ ๋„์ถœ๋  ์ˆ˜ ์žˆ๋‹ค.

dp[y][x] = max(dp[y-1][x], dp[y][x-1], dp[y-1][x-1]) + input[y][x]

์ฃผ์˜ํ•  ์ 

(1, 1)์—์„œ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ (1, 1), (1ํ–‰, 1), (1, 1์—ด)์˜ ์œ„์น˜์—์„œ๋Š” 3๋ฐฉํ–ฅ์—์„œ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•  ์ˆ˜ ์—†๋‹ค. ์ด ์ ์„ ๊ณ ๋ คํ•˜์—ฌ ๋ถ„๊ธฐ๋ฅผ ๋งŒ๋“ค์–ด ์ฝ”๋“œ๋ฅผ ์งœ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int N, M;
int map[1001][1001];
int dp[1001][1001];
 
int main(){
   cin >> N >> M;
   for (int i = 1; i <= N; i++) {
       for (int j = 1; j <= M; j++) {
           cin >> map[i][j];
           if (i == 1 && j == 1) dp[i][j] = map[i][j];
       }
   }
 
   for (int y = 1; y <= N; y++) {
       for (int x = 1; x <= M; x++) {
           if (y == 1 && x == 1) {
               continue;
           } else if (y == 1){
               dp[y][x] = max(dp[y][x], dp[y][x-1]);
           } else if (x == 1){
               dp[y][x] = max(dp[y][x], dp[y-1][x]);
           } else {
               dp[y][x] = max(dp[y][x], dp[y-1][x]);
               dp[y][x] = max(dp[y][x], dp[y][x-1]);
               dp[y][x] = max(dp[y][x], dp[y-1][x-1]);
           }
           dp[y][x] += map[y][x];
       }
   }
   cout << dp[N][M] << '\n';
}

Reference