1. class Solution {
    2. private:
    3. const int CEIL = 1000000007;
    4. int m, n;
    5. vector<vector<map<int, int> > > cache;
    6. int DFS(int x, int y, int step) {
    7. if (x < 0 || y < 0 || x >= m || y >= n)
    8. return 1;
    9. if (step == 0)
    10. return 0;
    11. if (cache[x][y].count(step))
    12. return cache[x][y][step];
    13. int count = 0;
    14. count = (count + DFS(x + 1, y, step - 1)) % CEIL;
    15. count = (count + DFS(x - 1, y, step - 1)) % CEIL;
    16. count = (count + DFS(x, y + 1, step - 1)) % CEIL;
    17. count = (count + DFS(x, y - 1, step - 1)) % CEIL;
    18. cache[x][y][step] = count;
    19. return count;
    20. }
    21. public:
    22. int findPaths(int m_, int n_, int maxMove, int startRow, int startColumn) {
    23. m = m_;
    24. n = n_;
    25. cache.resize(m, vector<map<int, int> >(n));
    26. return DFS(startRow, startColumn, maxMove);
    27. }
    28. };