Count paths from the top-left to the bottom-right of a grid, moving only right or down and avoiding obstacle cells, modulo 10^9 + 7.
Medium4Dynamic programmingMatrixNo attempts yetTime limit2sMemory limit512 MBEvery afternoon Jack runs from his house to John's house. Both houses sit in an open field of N rows and M columns. Jack wants to take a different route every day, but he does not know how many different routes exist.
The field is written as a grid of N rows and M columns.
....
..X.
....
Jack lives in the top-left cell and John lives in the bottom-right cell. Jack does not want to waste time, so he only walks down or right. Some cells of the field hold an obstacle such as a rock or a building, and Jack cannot pass through them. An obstacle is marked with an X.
The field above has 4 valid routes. The cells that a route passes through are marked with an asterisk.
**** *... *... **..
..X* *.X. **X. .*X.
...* **** .*** .***
Every valid route has the same length, N + M - 1.
The number of routes can be very large, so print it modulo 1000000007 (109+7).
The first line contains two integers N and M, the number of rows and the number of columns of the field.
Each of the next N lines contains M characters. A dot (.) means the cell is empty. An X means the cell holds an obstacle and Jack cannot use it.
The top-left cell and the bottom-right cell never hold an obstacle.
2≤N≤200
2≤M≤200
Print the number of routes from the top-left cell to the bottom-right cell, modulo 1000000007.
In most languages the modulus operator is %.