CodeForces - 348D: Turtles (LGV Theorem)

The meaning of problems: given N * M matrix '*' can be represented by the '#' can not be expressed, it is to find two paths from [1,1] to [N, M] to, in addition to starting and ending points such that, there is no intersection.

Ideas: no idea, it is bare title.  Lindström-Gessel-Viennot lemma

a to b, c to d, the intersection of the two paths there is no program number = w [a, b] * w [c, d] -w [a, d] * w [b, c];

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
const int maxn=3010;
const int Mod=1e9+7;
int mp[maxn][maxn],N,M,res; char c[maxn][maxn];
int solve(int sx,int sy,int tx,int ty)
{
    memset(mp,0,sizeof(mp));
    mp[sx][sy]=1;
    rep(i,1,N) rep(j,1,M) {
        if(c[i][j]=='#') continue;
        if(c[i-1][j]=='.') (mp[i][j]+=mp[i-1][j])%=Mod;
        if(c[i][j-1]=='.') (mp[i][j]+=mp[i][j-1])%=Mod;
    }
    return mp[tx][ty];
}
int main()
{
    scanf("%d%d",&N,&M);
    rep(i,1,N) scanf("%s",c[i]+1);
    res=1LL*solve(1,2,N-1,M)*solve(2,1,N,M-1)%Mod-1LL*solve(1,2,N,M-1)*solve(2,1,N-1,M)%Mod;
    if(res<0) res+=Mod;
    printf("%d\n",res);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hua-dong/p/11455470.html