E. Rock Is Push

You are at the top left cell (1,1) of an n×m labyrinth. Your goal is to get to the bottom right cell (n,m). You can only move right or down, one cell per step. Moving right from a cell (x,y) takes you to the cell (x,y+1), while moving down takes you to the cell (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+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,m — dimensions of the labyrinth (1≤n,m≤2000).

Next n lines describe the labyrinth. Each of these lines contains m characters. The j-th character of the i-th of these lines is equal to "R" if the cell (i,j) contains a rock, or "." if the cell (i,j) is empty.

It is guaranteed that the starting cell (1,1) is empty.

Output

Print a single integer — the number of different legal paths from (1,1) to (n,m) modulo 109+7.

Examples

input

1 1

.

output

1

input

2 3

...

..R

output

0

input

4 4

...R

.RR.

.RR.

R...

output

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).

In the second sample case the goal is blocked and is unreachable.

Illustrations for the third sample case can be found here: https://subdomain.codeforc.es/menci/assets/rounds/1225/index.html

题意

一个n*m的矩阵,里面有一堆箱子,你可以推箱子,连续的箱子你也能推动。

问你从(1,1)到(n,m)有多少种不同路径的方案个数。

题解

定义:

dp[i][j][0]表示从(i,j)往下走到达终点的方案数。

dp[i][j][1]表示从(i,j)往右走到达终点的方案数。

比较显然的

dp[i][j][0]=dp[i+1][j][1]+dp[i+2][j][1]+....+dp[i+x][j][1],直到(i+x+1,j)是一个箱子推到底了,不能再推箱子了。

同理dp[i][j][1]也是如此。

显然后面这坨可以用前缀和优化一下,然后就可以变成n^2的dp转移了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2005;
int n,m;
const int mod = 1e9+7;
char a[maxn][maxn];
// 0 for down;1 for right
int num[maxn][maxn][2],dp[maxn][maxn][2],sum[maxn][maxn][2];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",a[i]+1);
}
if(n==1&&m==1&&a[1][1]=='.'){
cout<<"1"<<endl;
return 0;
}
if(a[1][1]=='R'||a[n][m]=='R'){
cout<<"0"<<endl;
return 0;
}
for(int i=n;i>=1;i--){
for(int j=m;j>=1;j--){
if(a[i][j]=='R'){
num[i][j][0]+=1;
num[i][j][1]+=1;
}
num[i][j][0]+=num[i+1][j][0];
num[i][j][1]+=num[i][j+1][1];
}
}
dp[n][m][0]=1;dp[n][m][1]=1;sum[n][m][0]=1;sum[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]=(sum[i+1][j][0]-sum[n-num[i+1][j][0]+1][j][0])%mod;
dp[i][j][1]=(sum[i][j+1][1]-sum[i][m-num[i][j+1][1]+1][1])%mod;
sum[i][j][0]=(sum[i+1][j][0]+dp[i][j][1])%mod;
sum[i][j][1]=(sum[i][j+1][1]+dp[i][j][0])%mod;
}
}
cout<<(dp[1][1][0]+dp[1][1][1]+2ll*mod)%mod<<endl;
}

Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) E. Rock Is Push dp的更多相关文章

  1. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2)

    A - Forgetting Things 题意:给 \(a,b\) 两个数字的开头数字(1~9),求使得等式 \(a=b-1\) 成立的一组 \(a,b\) ,无解输出-1. 题解:很显然只有 \( ...

  2. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products

    链接: https://codeforces.com/contest/1247/problem/D 题意: You are given n positive integers a1,-,an, and ...

  3. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) C. p-binary

    链接: https://codeforces.com/contest/1247/problem/C 题意: Vasya will fancy any number as long as it is a ...

  4. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B2. TV Subscriptions (Hard Version)

    链接: https://codeforces.com/contest/1247/problem/B2 题意: The only difference between easy and hard ver ...

  5. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things

    链接: https://codeforces.com/contest/1247/problem/A 题意: Kolya is very absent-minded. Today his math te ...

  6. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) F. Tree Factory 构造题

    F. Tree Factory Bytelandian Tree Factory produces trees for all kinds of industrial applications. Yo ...

  7. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) B. TV Subscriptions 尺取法

    B2. TV Subscriptions (Hard Version) The only difference between easy and hard versions is constraint ...

  8. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things 水题

    A. Forgetting Things Kolya is very absent-minded. Today his math teacher asked him to solve a simple ...

  9. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products 数学 暴力

    D. Power Products You are given n positive integers a1,-,an, and an integer k≥2. Count the number of ...

随机推荐

  1. nginx--代理和负载均衡

    nginx代理 nginx的代理分为正向代理和反向代理 正向代理指的是,一个位于客户端和原始服务器之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原 ...

  2. python有哪些优点跟缺点

    显著的优点 1. 语言简洁优美,Java能实现的python都能实现(除安卓开发),python能实现的Java不一定能实现如(自动化运维,爬虫) 2. 跨平台,window, linux,mac通用 ...

  3. 为什么不允许使用 Java 静态构造函数?

    不允许使用 Java 静态构造函数,但是为什么呢?在深入探讨不允许使用静态构造函数的原因之前,让我们看看如果要使 构造函数静态化 会发生什么. Java 静态构造函数 假设我们有一个定义为的类: pu ...

  4. webpack入门配置步骤详解

    1.初始化 1.npm install webpack webpack-cli webpack-dev-server --g 全局安装必要的第三方插件 2.mkdir config dist src ...

  5. Java泛型类型擦除与运行时类型获取

    Java的泛型大家都知道是类型擦除的方式实现的,“编译器会进行泛型擦除”是一个常识了(实际擦除的是参数和自变量的类型).“类型擦除” 并非像许多开发者认为的那样,在 <..> 符号内的东西 ...

  6. 深入理解java中的byte类型

    作者 | 进击的石头--GO! 来源 | https://www.cnblogs.com/zl181015/p/9435035.html#4432849 Java也提供了一个byte数据类型,并且是基 ...

  7. .net post请求wcf

    class Program { static void Main(string[] args) { }); var r = HttpHelper.PostRequest("http://lo ...

  8. Java日期时间API系列1-----Jdk7及以前的日期时间类

    先看一个简单的图: 主要的类有: Date类负责时间的表示,在计算机中,时间的表示是一个较大的概念,现有的系统基本都是利用从1970.1.1 00:00:00 到当前时间的毫秒数进行计时,这个时间称为 ...

  9. LooseVersion()使用及.__version__版本号的获取

    我简单看了distutils库,但发现目前还用不到,感觉有些复杂.因此我简单复制了别人的介绍,如下: Distutils可以用来在Python环境中构建和安装额外的模块.新的模块可以是纯Python的 ...

  10. iOS滑动手势UIPanGestureRecognizer 注意事项

    今天在做侧滑页面时,发现页面随着滑动手势而滑动,到临界点时,如果再滑动会出现抖动现象.找到解决办法是进入方法后先判断一次,再判断是在滑动范围内让页面滑动.遂将滑动手势(UIPanGestureReco ...