A courier starts in the top-left cell of a rectangular grid and must reach the bottom-right cell. On every step the courier may move exactly one cell right or one cell down — never left, up, or diagonally.
Each cell is either open (0) or blocked (1). The courier may never stand on a blocked cell. Count how many distinct sequences of moves take the courier from the top-left cell to the bottom-right cell.
Because every step increases either the row or the column by exactly one, two paths are considered distinct if and only if they differ in at least one visited cell.
Line 1: two integers rows and cols.
The next rows lines: each contains cols integers (each 0 or 1), the grid in row-major order.
A single integer: the number of distinct right/down paths from the top-left cell to the bottom-right cell that avoid every blocked cell. Print 0 if no such path exists (including when the start or end cell is blocked).
0 (open) or 1 (blocked).Fully open 3x3
Input
3 3 0 0 0 0 0 0 0 0 0
Expected
6
Explanation
With no blocked cells there are 6 ways to interleave 2 right moves and 2 down moves (C(4,2) = 6).
Some blocked cells
Input
4 4 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0
Expected
6
Explanation
The blocked cells at (0,2), (2,0) and (3,2) remove several routes; exactly 6 valid right/down paths remain from the top-left to the bottom-right cell.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →