Predict the output
What does this print?
grid = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
row = [0] * 3
row[0] = 1
for cells in grid:
for c, blocked in enumerate(cells):
if blocked:
row[c] = 0
elif c:
row[c] += row[c - 1]
print(row[-1])Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 2
Right: with the centre blocked, only the routes along the edges remain.
Zeroing a blocked cell stops any count from flowing through it, which is the only change from the open-grid version.
Why not the others?
- B 6. That is the count for an open 3 × 3 grid.
- C 0. The two edge routes avoid the block.
Steps come from running the Python solution.
Text version
3 × 4 grid, one block:
| Row | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 0 | 1 | 1 | 1 | 1 |
| 1 | 1 | ✕ 0 | 1 | 2 |
| 2 | 1 | 1 | 2 | 4 |
Answer: 4 in cell (2, 3), from (1, 3) and (2, 2).
2 × 3 grid, corner block:
| Row | 0 | 1 | 2 |
|---|---|---|---|
| 0 | 1 | 1 | ✕ 0 |
| 1 | 1 | 2 | 2 |
Answer: 2 in cell (1, 2), from (0, 2) and (1, 1).