Algorithm Atlas · worked example
See the algorithm, step by step
A cell combines ways or cost from the predecessor directions allowed by the movement rule.
This call is starting. Local values appear as they are initialized.
Data & variables
Changed Source indexExample input
rows=3 cols=4Follow the code. Data appears here when it enters scope.
Call stack · 1 frame
maincall 1
Rust source
Line 15fn grid_paths(rows: usize, cols: usize) -> u64 {
if rows == 0 || cols == 0 {
return 0;
}
let mut dp = vec![1; cols];
for _ in 1..rows {
for c in 1..cols {
dp[c] += dp[c - 1];
}
}
dp[cols - 1]
}
fn main() {
let result = grid_paths(3, 4).to_string();
println!("{result}");
}
Try your own version
Compare the visible input `rows=3 cols=4` with `rows=1 cols=5`. Predict why their results are `10` and `1`. Then change one legal value and explain whether the same invariant, the old cell is the contribution from above and the new left cell is the contribution from the left, still applies.
Open personal copy in Playground ↗Start with this exact source and input, change a value, then use Run or Debug locally. This replay stays attached to the original example.
Recordings use a native Rust target. Playground runs on wasm32-wasip1; target-sized types such as usize can have different limits.

Understand it
In a 3×4 empty grid, the initial row is [1,1,1,1]. Subsequent rows become [1,2,3,4] and [1,3,6,10]. The bottom-right count is 10; a 1×1 grid has the single empty-move path.
The worked program is a complete executable for this pattern. Its core function uses typed input so the state transitions remain visible. The challenge input contract is: Parse rows and cols. Count paths through that empty grid using right/down moves from the top-left cell to the bottom-right. A 1x1 grid has one path. The visible fixture is rows=3 cols=4 and its example result is 10. The implementation and additional semantic fixtures were checked against an independent reference calculation. Those bounded tests establish these examples; they are not a proof for all inputs or a measurement on an iPhone.
Return a canonical String from solve: lowercase true/false for booleans; decimal integers without extra signs, leading zeros or whitespace; comma-separated sequence values and semicolon-separated groups. Whole-number medians have no decimal suffix; half medians use .5. Do not append a newline to the returned value. Preserve the explicitly named textual sentinels such as none or #. Input fixtures are bounded so the demonstrated arithmetic fits its named Rust types; satisfy the stated input preconditions before applying the algorithm.
String and character-grid fixtures use ASCII unless the lesson explicitly specifies Unicode scalar values.
A problem is a good match for Grid Path DP when it asks for robot paths, matrix costs, obstacle grids. Here the concrete task is: Parse rows and cols. Count paths through that empty grid using right/down moves from the top-left cell to the bottom-right. A 1x1 grid has one path. The visible input rows=3 cols=4 is an example, not a definition of the whole problem. Ask what operations the solution must support and what the input guarantees. Write the dependency arrows before choosing loop order; a recurrence that reads the future cannot be filled left to right without adjustment. The right choice follows from those guarantees, not from a familiar noun in the prompt.
The decisive invariant is: A cell combines ways or cost from the predecessor directions allowed by the movement rule. Try to say why it applies to the task without quoting the implementation. Which fact becomes known after one update, and which possible answers remain? A multidimensional or structured DP state records the information needed to identify a subproblem: two prefixes, an interval, a bitmask, a tree node, or a resource limit. If that claim cannot be maintained on a legal input, the pattern is wrong or one of its preconditions is missing. A counterexample is more useful than a vague warning: change one property of the sample, trace the first two steps, and identify the exact claim that fails.
Compare costs before committing. The advertised bound is O(rows*cols) time · O(cols) space possible; identify the operation responsible for it and the extra memory it needs. Then compare a straightforward alternative: a direct scan, full sort, or repeated recomputation may be easier but can pay for the same work many times. Do not choose the more elaborate method just because it has a famous name. For the sample, predict 10 and explain which part of the state lets the method reach that result.
When the prompt is ambiguous, ask about input size, ordering, duplicates, and required output format. Test base states, boundary cells or subsets, ties, and whether a compressed representation overwrites a value still needed later. Index tables consistently where the state is tabular. Compress dimensions only after the full recurrence and its dependency order are clear. The Rust sketch can show a useful primitive without proving that the whole task is solved; connect each primitive to the requested output. You should be able to name a fitting case, a near miss, and the assumption separating them before writing the first line.
The pattern
A cell combines ways or cost from the predecessor directions allowed by the movement rule.
When to use it: robot paths, matrix costs, obstacle grids
Complexity: O(rows*cols) time · O(cols) space possible
Input: rows=3 cols=4
Read the code
fn grid_paths(rows: usize, cols: usize) -> u64 {
if rows == 0 || cols == 0 {
return 0;
}
let mut dp = vec![1; cols];
for _ in 1..rows {
for c in 1..cols {
dp[c] += dp[c - 1];
}
}
dp[cols - 1]
}
fn main() {
let result = grid_paths(3, 4).to_string();
println!("{result}");
}
Complete std-only grid-path-dp program for its typed teaching fixture. Parsing and output construction are separate from the core algorithm.
Try it
Compare the visible and independent fixtures. Explain the input assumption that makes this algorithm applicable, then identify a change that would invalidate it.
What success looks like: You can justify the choice from constraints rather than from a remembered problem title.
Check your understanding
Trace Grid Path DP on input rows=1 cols=1. Which result follows from the documented contract?
Reveal the answer
210
For this input the result is 1. The old cell is the contribution from above and the new left cell is the contribution from the left. Compare this independent fixture with the worked trace: In a 3×4 empty grid, the initial row is [1,1,1,1]. Subsequent rows become [1,2,3,4] and [1,3,6,10]. The bottom-right count is 10; a 1×1 grid has the single empty-move path.
Go deeper
Common misconception: “Both dependencies must come from the next row”
The old cell is the contribution from above and the new left cell is the contribution from the left. In a 3×4 empty grid, the initial row is [1,1,1,1]. Subsequent rows become [1,2,3,4] and [1,3,6,10]. The bottom-right count is 10; a 1×1 grid has the single empty-move path.
- Concrete transition: In a 3×4 empty grid, the initial row is [1,1,1,1]. Subsequent rows become [1,2,3,4] and [1,3,6,10]. The bottom-right count is 10; a 1×1 grid has the single empty-move path.
- Independent second fixture: Input
rows=1 cols=1produces1. Execute the named function in the worked program with these typed arguments, then compare its actual result with this independently computed expectation. - Boundary and contract: Input
rows=4 cols=4produces20. The old cell is the contribution from above and the new left cell is the contribution from the left. The stated work bound is O(rows*cols) time · O(cols) space possible. Count actual state changes; a successful output test alone does not prove that bound.
Transfer challenge: Compare the visible input rows=3 cols=4 with rows=1 cols=5. Predict why their results are 10 and 1. Then change one legal value and explain whether the same invariant, the old cell is the contribution from above and the new left cell is the contribution from the left, still applies.
Connections
- Grid Path DP: Mental Model: A cell combines ways or cost from the predecessor directions allowed by the movement rule.
- Challenge: Grid Path DP: Parse rows and cols. Count paths through that empty grid using right/down moves from the top-left cell to the bottom-right. A 1x1 grid has one path.
Reveal the Algorithm Atlas answer
10
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Grid Path DP: When to Use It (courseId "algorithms", lessonId "algorithm.grid-path-dp.recognize") Link: https://crabrix.com/learn/algorithms/algorithm.grid-path-dp.recognize Goal: The old cell is the contribution from above and the new left cell is the contribution from the left. State the input preconditions before applying this invariant. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=algorithms/algorithm.grid-path-dp.recognize) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.