传送门
文章目录
题意:思路题意:
给你一个n×mn×mn×m的方格,你初始在(1,1)(1,1)(1,1)点,有些位置有箱子,你可以走到某个位置向你的方向推动这个箱子,箱子不能出界,问你走到(n,m)(n,m)(n,m)有多少种方案。
1≤n,m≤20001\le n,m\le 20001≤n,m≤2000
思路
没有箱子的时候,就是一个裸的过河卒的问题了,现在可以推动箱子,而且是有方向的,所以我们给原来的状态加一维代表上一次是从上还是左来的,即f[i][j][op]f[i][j][op]f[i][j][op],表示从(i,j)(i,j)(i,j)到(n,m)(n,m)(n,m),op=1op=1op=1代表从上面来,op=2op=2op=2代表从左面来,这样路径的方案数,考虑转移。
考虑某个位置(i,j)(i,j)(i,j),假设他上面没有箱子,右边有xxx个箱子,下面有yyy个箱子,此时f[i][j][1]f[i][j][1]f[i][j][1]可以从∑a=i,b=j+1a=i,b=m−xf[i][j][2]\sum_{a=i,b=j+1}^{a=i,b=m-x}f[i][j][2]∑a=i,b=j+1a=i,b=m−xf[i][j][2],注意这里需要用222来更新111,不然会产生后效性。
可以发现,向下也是这么转移的,预处理一下x,yx,yx,y即可,让后转移的话不能直接枚举,所以搞一个后缀和即可。
复杂度O(nm)O(nm)O(nm)
// Problem: E. Rock Is Push// Contest: Codeforces - Technocup - Elimination Round 2// URL: /contest/1225/problem/E// Memory Limit: 512 MB// Time Limit: 2000 ms// // Powered by CP Editor ()//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")//#pragma GCC optimize(2)#include<cstdio>#include<iostream>#include<string>#include<cstring>#include<map>#include<cmath>#include<cctype>#include<vector>#include<set>#include<queue>#include<algorithm>#include<sstream>#include<ctime>#include<cstdlib>#include<random>#include<cassert>#define X first#define Y second#define L (u<<1)#define R (u<<1|1)#define pb push_back#define mk make_pair#define Mid ((tr[u].l+tr[u].r)>>1)#define Len(u) (tr[u].r-tr[u].l+1)#define random(a,b) ((a)+rand()%((b)-(a)+1))#define db puts("---")using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> PII;const int N=,mod=1e9+7,INF=0x3f3f3f3f;const double eps=1e-6;int n,m;LL f[N][N][2],sum[N][N][2];int l[N][N],r[N][N];char s[N][N];int main(){//ios::sync_with_stdio(false);//cin.tie(0);scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) scanf("%s",s[i]+1);if(n==1&&m==1) {cout<<(s[1][1]!='R')<<endl;return 0;}for(int i=n;i>=1;i--) {for(int j=m;j>=1;j--) {l[i][j]=r[i][j]=s[i][j]=='R';l[i][j]+=l[i][j+1];r[i][j]+=r[i+1][j];}}f[n][m][0]=s[n][m]!='R';f[n][m][1]=s[n][m]!='R';// sum[n][m][0]=s[n][m]!='R';// sum[n][m][1]=s[n][m]!='R';for(int i=n;i>=1;i--) {for(int j=m;j>=1;j--) {// if(i==n&&j==m) continue;int x=l[i][j+1],y=r[i+1][j];f[i][j][0]+=sum[i+1][j][1]-sum[n-y+1][j][1];f[i][j][1]+=sum[i][j+1][0]-sum[i][m-x+1][0];f[i][j][0]%=mod; f[i][j][1]%=mod;f[i][j][0]+=mod; f[i][j][1]+=mod;f[i][j][0]%=mod; f[i][j][1]%=mod;sum[i][j][0]+=sum[i][j+1][0]+f[i][j][0];sum[i][j][1]+=sum[i+1][j][1]+f[i][j][1];sum[i][j][0]%=mod; sum[i][j][1]%=mod;sum[i][j][0]+=mod; sum[i][j][1]+=mod;sum[i][j][0]%=mod; sum[i][j][1]%=mod;}}cout<<(f[1][1][0]+f[1][1][1])%mod<<endl;return 0;}/**/