https://school.programmers.co.kr/learn/courses/30/lessons/1832
import java.util.*;
class Solution {
public int MOD = 20170805;
public int M, N;
public int[][] map;
public int[][][] dp;
public int[] dx = {0, 1};
public int[] dy = {1, 0};
public int solve(int x, int y, int beforeD) {
//기저 조건
if (x == M-1 && y == N-1) return 1;
if (dp[x][y][beforeD] != -1) return dp[x][y][beforeD];
int result = 0;
for (int d = 0; d < 2; d++) {
int X = x + dx[d];
int Y = y + dy[d];
if (X >= M || Y >= N) continue;
if (map[X][Y] == 1) continue;
if (map[x][y] == 2 && beforeD != d) continue;
result = (result + solve(X, Y, d)) % MOD;
}
return dp[x][y][beforeD] = result;
}
public int solution(int m, int n, int[][] cityMap) {
//초기화
M = m;
N = n;
map = cityMap;
dp = new int[M][N][2];
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
Arrays.fill(dp[i][j], -1);
return solve(0, 0, 0);
}
}