Problem Statement
There is a grid with HH horizontal rows and WW vertical columns. Let (i, j)(i,j) denote the square at the ii-th row from the top and the jj-th column from the left.
For each ii and jj (1 \leq i \leq H1≤i≤H, 1 \leq j \leq W1≤j≤W), Square (i, j)(i,j) is described by a character a_{i, j}ai,j. If a_{i, j}ai,j is .
, Square (i, j)(i,j) is an empty square; if a_{i, j}ai,j is #
, Square (i, j)(i,j) is a wall square. It is guaranteed that Squares (1, 1)(1,1) and (H, W)(H,W) are empty squares.
Taro will start from Square (1, 1)(1,1) and reach (H, W)(H,W) by repeatedly moving right or down to an adjacent empty square.
Find the number of Taro's paths from Square (1, 1)(1,1) to (H, W)(H,W). As the answer can be extremely large, find the count modulo 10^9 + 7109+7.
Constraints
- HH and WW are integers.
- 2 \leq H, W \leq 10002≤H,W≤1000
- a_{i, j}ai,j is
.
or#
. - Squares (1, 1)(1,1) and (H, W)(H,W) are empty squares.
Input
Input is given from Standard Input in the following format:
HH WW a_{1, 1}a1,1\ldots…a_{1, W}a1,W :: a_{H, 1}aH,1\ldots…a_{H, W}aH,W
Output
Print the number of Taro's paths from Square (1, 1)(1,1) to (H, W)(H,W), modulo 10^9 + 7109+7.
Sample 1
Inputcopy | Outputcopy |
---|---|
3 4 ...# .#.. .... | 3 |
There are three paths as follows:
Sample 2
Inputcopy | Outputcopy |
---|---|
5 2 .. #. .. .# .. | 0 |
There may be no paths.
Sample 3
Inputcopy | Outputcopy |
---|---|
5 5 ..#.. ..... #...# ..... ..#.. | 24 |
Sample 4
Inputcopy | Outputcopy |
---|---|
20 20 .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... | 345263555 |
Be sure to print the count modulo 10^9 + 7109+7.
#include <iostream>
#include <cstring>
using namespace std;
const int mod=1e9+7;
int sp[1005][1005]={0};
long long ans[1005][1005],h,m;
int main()
{
scanf("%d%d",&m,&h);
ans[1][1]=0;
char c;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=h;j++)
{
cin>>c;
if(c=='#')
{
sp[i][j]=1;
ans[i][j]=0;
}
}
}
int f=0;
for(int i=2;i<=m;i++)
{
if(sp[i][1]==1)
f=1;
if(f==0)
ans[i][1]=1;
else
ans[i][1]=0;
}
f=0;
for(int i=2;i<=h;i++)
{
if(sp[1][i]==1)
f=1;
if(f==0)
ans[1][i]=1;
else
ans[1][i]=0;
}
for(int i=2;i<=m;i++)
{
for(int j=2;j<=h;j++)
{
if(sp[i][j]==0)
ans[i][j]=(ans[i][j-1]+ans[i-1][j])%mod;
}
}
/* for(int i=1;i<=m;i++)
{
for(int j=1;j<=h;j++)
{
cout<<ans[i][j]<<" ";
}
cout<<endl;
}*/
cout<<endl<<ans[m][h];
return 0;
}