E. Rock Is Push
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
You are at the top left cell (1,1)(1,1) of an n×mn×m labyrinth. Your goal is to get to the bottom right cell (n,m)(n,m). You can only move right or down, one cell per step. Moving right from a cell (x,y)(x,y) takes you to the cell (x,y+1)(x,y+1), while moving down takes you to the cell (x+1,y)(x+1,y).
Some cells of the labyrinth contain rocks. When you move to a cell with rock, the rock is pushed to the next cell in the direction you're moving. If the next cell contains a rock, it gets pushed further, and so on.
The labyrinth is surrounded by impenetrable walls, thus any move that would put you or any rock outside of the labyrinth is illegal.
Count the number of different legal paths you can take from the start to the goal modulo 109+7109+7. Two paths are considered different if there is at least one cell that is visited in one path, but not visited in the other.
Input
The first line contains two integers n,mn,m — dimensions of the labyrinth (1≤n,m≤20001≤n,m≤2000).
Next nn lines describe the labyrinth. Each of these lines contains mm characters. The jj-th character of the ii-th of these lines is equal to "R" if the cell (i,j)(i,j) contains a rock, or "." if the cell (i,j)(i,j) is empty.
It is guaranteed that the starting cell (1,1)(1,1) is empty.
Output
Print a single integer — the number of different legal paths from (1,1)(1,1) to (n,m)(n,m) modulo 109+7109+7.
Examples
input
Copy
1 1
.
output
Copy
1
input
Copy
2 3
...
..R
output
Copy
0
input
Copy
4 4
...R
.RR.
.RR.
R...
output
Copy
4
Note
In the first sample case we can't (and don't have to) move, hence the only path consists of a single cell (1,1)(1,1).
In the second sample case the goal is blocked and is unreachable.
Illustrations for the third sample case can be found here: https://assets.codeforces.com/rounds/1225/index.html
分析:因为只能向右下方前进,所以当向右走,即从(i,j)走向(i,j+1)时,影响状态的岩石只有(i,j)(n,m)这个矩阵中的岩石和i这一行的岩石。向下走同理。
那么用dp[i][j][0]表示从(i,j)向右走的方案数,dp[i][j][1]表示从(i,j)向下走的方案数。
显然,dp[i][j][0]不能简单的由dp[i][j+1][0]+dp[i][j+1][1]得出,因为dp[i][j+1][0]中的方案数并没有计算从(i,j)推过来的岩石的影响。既然只能右下,那么我们可以枚举向右走了几步后向下走,预处理出(i,j)位置右边的岩石个数,然后求个区间合就可以了。区间合直接后缀和维护一下就可以了。向下同理。
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, m;
char s[2004][2004];
int rn[2004][2004];
int dn[2004][2004];
int dp[2004][2004][2];//0right 1down
int sufr[2004][2004];
int sufd[2004][2004];
inline int MOD(int x) {
if(x < 0)
return (x + mod);
if(x >= mod)return x-mod;
return x;
}
int main() {
cin >> n >> m;
memset(rn, 0, sizeof(rn));
memset(dn, 0, sizeof(dn));
memset(dp, 0, sizeof(dp));
memset(sufr, 0, sizeof(sufr));
memset(sufd, 0, sizeof(sufd));
for (int i = 1; i <= n; ++i) {
scanf("%s", s[i] + 1);
}
for (int i = 1; i <= n; ++i) {
for (int j = m; j >= 1; --j) {
rn[i][j] = rn[i][j + 1];
if (s[i][j+1] == 'R')rn[i][j]++;
}
}
for (int i = 1; i <= m; ++i) {
for (int j = n; j >= 1; --j) {
dn[j][i] = dn[j+1][i];
if (s[j+1][i] == 'R')dn[j][i]++;
}
}
dp[n][m][0] = dp[n][m][1] = 1;
for (int i = n; i >= 1; --i) {
for (int j = m; j >= 1; --j) {
//if(i == n && j == m)continue;
dp[i][j][0] += MOD(sufd[i][j + 1] - sufd[i][m - rn[i][j] + 1]);
dp[i][j][1] += MOD(sufr[i + 1][j] - sufr[n - dn[i][j] + 1][j]);
sufd[i][j] += dp[i][j][1];
sufr[i][j] += dp[i][j][0];
sufd[i][j] %= mod;
sufr[i][j] %= mod;
sufr[i][j] += sufr[i+1][j];
sufd[i][j] += sufd[i][j+1];
sufd[i][j] %= mod;
sufr[i][j] %= mod;
}
}
if(n == m&&n==1){
puts("1");
return 0;
}
cout<<MOD(dp[1][1][1] + dp[1][1][0])<<endl;
}