Calculating the number of possible paths through grid

224 Views Asked by At

We are given with four integers $H$ , $W$ , $A$ , $B$ respectively

We have a large rectangular grid with $H$ rows and $W$ columns. Find the number of paths from top left to bottom right moving only right ($R$) or down ($D$) to adjacent cells .

However, we cannot enter the cells in the intersection of the bottom $A$ rows and the leftmost $B$ columns. (That is, there are $A \times B$ forbidden cells.) There is no restriction on entering the other cells.

Example 1 : $H = 2; W = 3; A = 1; B = 1$

Answer : $2$

Example 2 : $H = 10; W = 7; A = 3; B = 4$

Answer : $3570$

1

There are 1 best solutions below

2
On BEST ANSWER

Assume there is no forbidden rectangle i.e. $A = B = 0$. Combinatorics tells us that then there are $\binom{W + H}{H}$ possible paths.

Now we want to subtract all forbidden paths. Note that a path is forbidden if he enters the forbidden rectangle. Let's think of a path as a string over $\{R, D\}$ of length $W+H$ where $R$ denotes a move to the right and $D$ denotes a move down.

We can see that a path enters the forbidden rectangle if and only if more than $H - A$ of the first $B + (H - A)$ moves are $D$'s. So how many strings of length $H + W$ are there where the first $B + (H - A)$ letters contain more than $(H - A)$ times a $D$? We can count this by the following sum: $$S := \Sigma_{i = H - A + 1}^{B + (H - A)}\binom{B+(H - A)}{i} \binom{W - B+A}{H - i}$$

For a given $i$ the first binom corresponds to the amount of strings of length $B + (H - A)$ with $i$ times a $D$. The second binom corresponds to the amount of strings of length $W - B + A$ containing $H-i$ times a $D$.

So the answer would be: $$ \binom{W + H}{H} - S$$