λ°±μ€ 1890λ². κ·Έλ₯ DFS + DP λ¬Έμ λ‘ μλ μ½λλ‘λ 무λν λμκ°μ§λ§
#include <iostream>
using namespace std;
int N;
int tile[100][100];
long long memo[100][100];
long long sol(int i, int j) {
if (i == N - 1 && j == N - 1) {
return 1;
}
if (memo[i][j] != -1) {
return memo[i][j];
}
long long &ret = memo[i][j];
ret = 0;
if (i + tile[i][j] < N){
ret += sol(i + tile[i][j], j);
}
if (j + tile[i][j] < N) {
ret += sol(i, j + tile[i][j]);
}
return ret;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> tile[i][j];
memo[i][j] = -1;
}
}
cout << sol(0, 0);
return 0;
}#include <iostream>
using namespace std;
int N;
int tile[100][100];
long long memo[100][100];
long long sol(int i, int j) {
if (i == N - 1 && j == N - 1) {
return 1;
}
if (memo[i][j] != -1) {
return memo[i][j];
}
long long ret = 0;
if (i + tile[i][j] < N){
ret += sol(i + tile[i][j], j);
}
if (j + tile[i][j] < N) {
ret += sol(i, j + tile[i][j]);
}
memo[i][j] = ret;
return ret;
}
int main() {
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> tile[i][j];
memo[i][j] = -1;
}
}
cout << sol(0, 0);
return 0;
}
μ€κ°μ sol λΆλΆμμ λ³μλ₯Ό νλ μ μΈν΄μ μ°λ κ²μΌλ‘ λ°λλ©΄ νΈμΆ μ€νμμ λ©λͺ¨λ¦¬λ₯Ό λ¬΄μ§ λ§μ΄ μ¨μ λ°λ‘ λ©λͺ¨λ¦¬ μ΄κ³Όκ° λ¬λ€
long long sol(int i, int j) {
if (i == N - 1 && j == N - 1) {
return 1;
}
if (memo[i][j] != -1) {
return memo[i][j];
}
long long ret = 0;
if (i + tile[i][j] < N){
ret += sol(i + tile[i][j], j);
}
if (j + tile[i][j] < N) {
ret += sol(i, j + tile[i][j]);
}
memo[i][j] = ret;
return ret;
}
DFSλ₯Ό μ¬κ·λ‘ 지 λλ λ³μ μ μΈμ μ μνμ.