Score : 500 points
Problem Statement
We have a grid with H horizontal rows and W vertical columns of squares.
Square (i,j), which is at the i-th row from the top and j-th column from the left, is wall if Sij is #
and road if Sij is .
.
There is a queen, the chess piece, at Square (1,1). In one move, it can move any number of squares to the right, downwards, or diagonally to the lower right to a road square without jumping over wall squares.
In how many ways can the queen travel from Square (1,1) to Square (H,W)? Find the count modulo (109+7).
Here, two ways to travel are considered different if and only if there exists i such that the position of the queen after the i-th move is different in those two ways.
Constraints
- 2≤H,W≤2000
- Sij is
#
or .
.
- S11 and SHW are
.
.
Input is given from Standard Input in the following format:
H W
S11…S1W
⋮
SH1…SHW
Output
Print the number of ways, modulo (109+7), in which the queen can travel from Square (1,1) to Square (H,W).
3 3
...
.#.
...
10
There are 10 ways to travel, as follows:
- (1,1)→(1,2)→(1,3)→(2,3)→(3,3)
- (1,1)→(1,2)→(1,3)→(3,3)
- (1,1)→(1,2)→(2,3)→(3,3)
- (1,1)→(1,3)→(2,3)→(3,3)
- (1,1)→(1,3)→(3,3)
- (1,1)→(2,1)→(3,1)→(3,2)→(3,3)
- (1,1)→(2,1)→(3,1)→(3,3)
- (1,1)→(2,1)→(3,2)→(3,3)
- (1,1)→(3,1)→(3,2)→(3,3)
- (1,1)→(3,1)→(3,3)
4 4
...#
....
..#.
....
84
From (1,1), the queen can move to (1,2), (1,3), (2,1), (2,2), (3,1), or (4,1).
One possible path to (4,4) is (1,1)→(3,1)→(3,2)→(4,3)→(4,4).
8 10
..........
..........
..........
..........
..........
..........
..........
..........
13701937
Find the count modulo (109+7).