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

์ƒ๊ฐ

์•ž์œผ๋กœ ๋ณด๋‚ด๋Š” dp๋ฌธ์ œ์ด๋‹ค.

์ •์˜

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

dp[y][x] = (y,x) ์œ„์น˜๊นŒ์ง€ ๋„์ฐฉํ•˜๋Š” ๊ฒฝ๋กœ์˜ ๊ฐœ์ˆ˜

์ ํ™”์‹

ํ•ด๋‹น ์œ„์น˜๊นŒ์ง€ ๋„์ฐฉํ•˜๋Š” ๊ฐœ์ˆ˜๋Š” ์ด์ „ ์œ„์น˜์—์„œ ์˜ฌ ์ˆ˜ ์žˆ๋Š๋ƒ๋ฅผ ํŒ๋‹จํ•ด์•ผ ํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ ๊ณผ๊ฑฐ๋กœ ๋ถ€ํ„ฐ ํ˜„์žฌ๋ฅผ ๊ฐ–๊ณ ์˜ค๋Š” ๊ฒƒ์ด ๋ฌด๋ฆฌ๊ฐ€ ์žˆ์œผ๋‹ˆ ์˜คํžˆ๋ ค ํ˜„์žฌ ์œ„์น˜์—์„œ ๋‹ค์Œ ์œ„์น˜๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ๊ฒƒ์ด ์˜ณ๋‹ค.

dp[y+d][x] += dp[y][x]

dp[y][x+d] += dp[y][x]

d๋Š” (y,x)์œ„์น˜์—์„œ ์ ํ”„ํ•  ์ˆ˜ ์žˆ๋Š” ํฌ๊ธฐ์ด๋‹ค. ์ด ๋•Œ dp[y][x]๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ๋Š” ์ด์œ ๋Š”, ํ•ด๋‹น (y, x) ์œ„์น˜์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๊ฐ€ 2๊ฐœ๋ผ๋ฉด, ์ด ์ง€์ ๊นŒ์ง€ ๋„๋‹ฌํ•œ ๊ฒฝ๋กœ ๋ชจ๋‘๋Š” ๋‹ค์Œ ์œ„์น˜๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ์ˆซ์ž๋กœ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์ด ๋งž๋‹ค.

4
2 3 2 1
1 2 1 3
2 2 1 1
3 1 1 0

=========(1,1 )=========
=========y ๋ฐฉํ–ฅ=========
1 0 0 0
0 0 0 0
1 0 0 0
0 0 0 0

=========(1,1 )=========
=========x ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 0 0
0 0 0 0

=========(1,3 )=========
=========y ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 1 0
0 0 0 0

=========(1,3 )=========
=========x ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 1 0
0 0 0 0

=========(3,1 )=========
=========y ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 1 0
0 0 0 0

=========(3,1 )=========
=========x ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 2 0
0 0 0 0

=========(3,3 )=========
=========y ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 2 0
0 0 2 0

=========(3,3 )=========
=========x ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 2 2
0 0 2 0

=========(3,4 )=========
=========y ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 2 2
0 0 2 2

=========(3,4 )=========
=========x ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 2 2
0 0 2 2

=========(4,3 )=========
=========y ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 2 2
0 0 2 2

=========(4,3 )=========
=========x ๋ฐฉํ–ฅ=========
1 0 1 0
0 0 0 0
1 0 2 2
0 0 2 4

4

Code

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
int N;
int map[101][101];
ll dp[101][101];
 
int main(){
    cin >> N;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= N; j++) {
            cin >> map[i][j];
        }
    }
    memset(dp, 0, sizeof(dp));
    dp[1][1] = 1;
    dp[N][N] = 0;
    for (int y = 1; y <= N; y++) {
        for (int x = 1; x <= N; x++) {
            if (dp[y][x] == 0 || (y == N && x == N)) continue;
            int d = map[y][x];
            if (y+d <= N) dp[y+d][x] += dp[y][x];
            if (x+d <= N) dp[y][x+d] += dp[y][x];
        }
    }
    cout << dp[N][N] << '\n';
}
 

Reference