Roadworks on the Grid
A bike courier rides a grid of streets from the depot at the top-left intersection to a customer at the bottom-right. She only ever rides east (right) or south (down).
This week some intersections are fenced off for roadworks. She likes to vary her ride: how many different routes does she have?
Your task
Write countRoutes(grid). grid[r][c] is 0 for an open intersection and 1 for a closed one. Return the number of routes from grid[0][0] to the bottom-right cell that move one cell right or down at a time and never enter a closed cell.
If the depot or the customer's intersection is closed, return 0. A 1 × 1 open grid has exactly 1 route (she is already there).
countRoutes(grid: int[][]) → int
Examples
grid = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
2
An open 3 × 3 grid has 6 routes. The closed middle kills every route through it, leaving two: all the way right then down, or all the way down then right.
grid = [[0, 0, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]]
3
Count the routes that reach each intersection, row by row. The first row is all 1s. The second row is 1, 2, 0 (closed) and 1. The third row is 0 (closed), 2, 2 and 3. So 3 routes reach the customer.
Constraints
- 1 ≤ number of rows, number of columns ≤ 20
- Every row has the same length.
- grid[r][c] is 0 or 1.
Start with brute force
Use recursion: from each cell, try moving right and moving down, and count the paths that arrive at the customer.
An open 20 × 20 grid has over 35 billion routes, and the recursion visits each one. Every route through the same middle cell recounts everything that comes after it.
Hints
0 of 4 shown- Hint 1
Pick an open intersection not on the top row or left edge. From which two intersections could she have arrived?
- Hint 2
She could have come from above or from the left, and those two groups of routes never overlap. So the number of routes to this cell is the number of routes to the cell above plus the number of routes to the cell on the left.
- Hint 3
A closed intersection has 0 routes into it. What about the top row: does every cell there have 1 route, even with a closed cell further left?
- Hint 4
Fill a table row by row, left to right. The start cell is 1 if it is open. Every other cell is 0 if it is closed, and otherwise it is the value above plus the value to the left, where a neighbour outside the grid counts as 0. A single row, updated in place, is enough.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Every route into a cell comes either from above or from the left, and these are two separate groups. So routes(r, c) = routes(r - 1, c) + routes(r, c - 1), and a closed cell has 0 routes.
Trace the second example, row by row:
- Row 0 is all open, so it is
1, 1, 1, 1, because each cell can only be reached from its left. - In row 1, column 0 gets 1 from above and column 1 gets 1 + 1 = 2. Column 2 is closed, so it is 0. Column 3 gets 1 from above and 0 from the left, which is 1. The row is
1, 2, 0, 1. - In row 2, column 0 is closed, so it is 0. Column 1 gets 2 + 0 = 2, column 2 gets 0 + 2 = 2, and column 3 gets 1 + 2 = 3.
The answer is 3. The code keeps only one row. Before ways[c] is updated it still holds the value from the cell above, and ways[c - 1] has already been updated to the cell on the left, so adding them gives above plus left.
function countRoutes(grid) {
const cols = grid[0].length;
const ways = new Array(cols).fill(0); // ways[c] = routes into column c of the current row
ways[0] = grid[0][0] === 0 ? 1 : 0;
for (const row of grid) {
for (let c = 0; c < cols; c++) {
if (row[c] === 1) ways[c] = 0; // closed: no routes through here
else if (c > 0) ways[c] += ways[c - 1]; // from above (old value) + from the left
}
}
return ways[cols - 1];
}def count_routes(grid):
cols = len(grid[0])
ways = [0] * cols # ways[c] = routes into column c of the current row
ways[0] = 1 if grid[0][0] == 0 else 0
for row in grid:
for c in range(cols):
if row[c] == 1:
ways[c] = 0 # closed: no routes through here
elif c > 0:
ways[c] += ways[c - 1] # from above (old value) + from the left
return ways[cols - 1]Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.