0576.cpp (794B)
1 class Solution { 2 static const int MOD = 1E9 + 7; 3 static int dp[51][51][51]; 4 5 public: 6 Solution() { memset(dp, 0xFF, sizeof(dp)); } 7 int findPaths(const int m, const int n, const int moves, const int row, const int col) const { 8 if (!moves) return 0; 9 if (dp[moves][row][col] != -1) return dp[moves][row][col]; 10 int res = 0; 11 12 static int offset[] = {-1, 0, 1, 0, -1}; 13 for (int k = 0; k < 4; k++) { 14 const int x = row + offset[k]; 15 const int y = col + offset[k + 1]; 16 if (x < 0 || y < 0 || x >= m || y >= n) 17 res++; 18 else 19 res = (res + findPaths(m, n, moves - 1, x, y)) % MOD; 20 } 21 22 return dp[moves][row][col] = res; 23 } 24 }; 25 26 int Solution::dp[51][51][51];