Java | DFS | Clean code
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(m*n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(m*n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n  int[][] dp;\n  int[] dirs = new int[] {-1, 0, 1};\n  int m, n, max = 0;\n  \n  private int maxMoves(int[][] grid, int x, int y) {\n    if (y == n-1) return 0;\n    \n    if (dp[x][y] != -1) return dp[x][y];\n    \n    dp[x][y] = 0;\n    for (var dir : dirs) {\n      var x2 = x + dir;\n      if (x2 < 0 || x2 == m) continue;\n      \n      if (grid[x2][y+1] > grid[x][y])\n        dp[x][y] = Math.max(dp[x][y], 1 + maxMoves(grid, x2, y+1));\n    }\n    return dp[x][y];\n  }\n  \n  public int maxMoves(int[][] grid) {\n    m = grid.length;\n    n = grid[0].length;\n    \n    dp = new int[m][n];\n    for (var row : dp)\n      Arrays.fill(row, -1);\n    \n    for (var i=0; i<m; i++)\n      max = Math.max(max, maxMoves(grid, i, 0));\n  \n    return max;\n  }\n}\n```\nIf you like my solution, please upvote it!
['Depth-First Search', 'Memoization', 'Matrix', 'Java']