A sorting-center floor is laid out as a grid with rows rows and cols columns. A pallet robot starts in the top-left cell (0, 0) and must reach the bottom-right cell (rows-1, cols-1). On every move the robot goes either one cell right or one cell down; it can never move left, up, or diagonally. Some cells are blocked by shelving, and the robot can never enter a blocked cell. For a compliance audit, the robot's route must also pass through one specific checkpoint cell at some point during the trip (the checkpoint may be the start cell, the end cell, or any cell in between).
Line 1: two integers rows and cols.
Each of the next rows lines: cols space-separated integers, each 0 (clear) or 1 (blocked), giving the floor row by row.
Last line: two integers cr and cc -- the 0-indexed row and column of the mandatory checkpoint cell.
Print a single integer: the number of distinct right/down routes from (0, 0) to (rows-1, cols-1) that never enter a blocked cell and that visit the checkpoint cell (cr, cc) at some point, taken modulo 1000000007. If no such route exists (including when the start, end, or checkpoint cell is itself blocked), print 0.
0 or 1Example 1
Input
3 3 0 0 0 0 0 0 0 0 0 1 1
Expected
4
Explanation
The floor is fully open. There are 2 ways from (0,0) to the checkpoint (1,1) and 2 ways from (1,1) to (2,2), giving 2 * 2 = 4 routes through the checkpoint.
Example 2
Input
2 2 0 1 1 0 0 0
Expected
0
Explanation
Both cells adjacent to the start are blocked ((0,1) and (1,0)), so no right/down route can ever leave the start cell; the answer is 0 regardless of the checkpoint.
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 →