← Home

576. Out of Boundary Paths

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
DFS + memoizationC++Markdown
576

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 576. Out of Boundary Paths, the solution in this repository is mainly a DFS + memoization solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: DFS + memoization, dynamic programming.

The notes already sitting in the source point us in the right direction:

  • dfs + memorization
  • without memorization: time: O(4^N), space: O(N)
  • time: O(mnN), space: O(mnN)

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are dfs, findPaths.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(mnN), space: O(mnN)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//dfs + memorization
02//Runtime: 12 ms, faster than 69.88% of C++ online submissions for Out of Boundary Paths.
03//Memory Usage: 9.5 MB, less than 40.39% of C++ online submissions for Out of Boundary Paths.
04//without memorization: time: O(4^N), space: O(N)
05//time: O(mnN), space: O(mnN)
06class Solution {
07public:
08    vector<vector<vector<int>>> dp;
09    int MOD = 1e9+7;
10    
11    int dfs(int m, int n, int N, int i, int j) {
12        if(i < 0 || i == m || j < 0 || j == n) return 1;
13        if(N == 0) return 0;
14        if(dp[N][i][j] >= 0) return dp[N][i][j];
15        
16        //dfs, not findPaths!!
17        int a = dfs(m, n, N-1, i, j-1);
18        int b = dfs(m, n, N-1, i, j+1);
19        int c = dfs(m, n, N-1, i-1, j);
20        int d = dfs(m, n, N-1, i+1, j);
21        
22        return dp[N][i][j] = (((a+b)%MOD + c)%MOD +d) % MOD;
23    }
24    
25    int findPaths(int m, int n, int N, int i, int j) {
26        if(i < 0 || i == m || j < 0 || j == n) return 1;
27        if(N == 0) return 0;
28        
29        dp = vector<vector<vector<int>>>(N+1, vector<vector<int>>(m, vector<int>(n, -1)));
30        
31        return dfs(m, n, N, i, j);
32    }
33};
34
35//dfs + memorization, change vector to array
36//Runtime: 4 ms, faster than 98.07% of C++ online submissions for Out of Boundary Paths.
37//Memory Usage: 6.5 MB, less than 86.03% of C++ online submissions for Out of Boundary Paths.
38//time: O(mnN), space: O(mnN)
39class Solution {
40public:
41    int dp[51][50][50];
42    int MOD = 1e9+7;
43    
44    int dfs(int m, int n, int N, int i, int j) {
45        if(i < 0 || i == m || j < 0 || j == n) return 1;
46        if(N == 0) return 0;
47        if(dp[N][i][j] >= 0) return dp[N][i][j];
48        
49        int a = dfs(m, n, N-1, i, j-1);
50        int b = dfs(m, n, N-1, i, j+1);
51        int c = dfs(m, n, N-1, i-1, j);
52        int d = dfs(m, n, N-1, i+1, j);
53        
54        return dp[N][i][j] = (((a+b)%MOD + c)%MOD +d) % MOD;
55    }
56    
57    int findPaths(int m, int n, int N, int i, int j) {
58        if(i < 0 || i == m || j < 0 || j == n) return 1;
59        if(N == 0) return 0;
60        
61        memset(dp, -1, sizeof(dp));
62        
63        return dfs(m, n, N, i, j);
64    }
65};
66
67//Approach 3: Dynamic Programming
68//bottom-up DP
69//Runtime: 12 ms, faster than 69.88% of C++ online submissions for Out of Boundary Paths.
70//Memory Usage: 9.3 MB, less than 54.36% of C++ online submissions for Out of Boundary Paths.
71//time: O(mnN), space: O(mn)
72class Solution {
73public:
74    int findPaths(int m, int n, int N, int i, int j) {
75        int MOD = 1e9+7;
76        //how many possible ways to go to (i, j)
77        vector<vector<int>> dp(m, vector<int>(n, 0));
78        //take 0 moves
79        dp[i][j] = 1;
80        
81        //how many possible ways to cross the boundary
82        int ans = 0;
83        
84        //take N moves
85        while(N-- > 0){
86            vector<vector<int>> tmp(m, vector<int>(n, 0));
87            
88            for(int i = 0; i < m; ++i){
89                for(int j = 0; j < n; ++j){
90                    //cross boundary, update ans
91                    if(i == 0)   ans = (ans+dp[i][j])%MOD;
92                    if(i == m-1) ans = (ans+dp[i][j])%MOD;
93                    if(j == 0)   ans = (ans+dp[i][j])%MOD;
94                    if(j == n-1) ans = (ans+dp[i][j])%MOD;
95                    
96                    //update (i,j)'s count, from 4 directions
97                    int a = (i>0) ? dp[i-1][j] : 0;
98                    int b = (i<m-1) ? dp[i+1][j] : 0;
99                    int c = (j>0) ? dp[i][j-1] : 0;
100                    int d = (j<n-1) ? dp[i][j+1] : 0;
101                    
102                    tmp[i][j] = (((a+b)%MOD + c)%MOD + d)%MOD;
103                }
104            }
105            
106            swap(dp, tmp);
107        }
108        
109        return ans;
110    }
111};

Cost

Complexity

Time
O(mnN), space: O(mnN)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.