codeforces 348d D. Turtles

D. Turtles

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You've got a table of size n × m. We'll consider the table rows numbered from top to bottom 1 through n, and the columns numbered from left to right 1 through m. Then we'll denote the cell in row x and column y as (x, y).

Initially cell (1, 1) contains two similar turtles. Both turtles want to get to cell (n, m). Some cells of the table have obstacles but it is guaranteed that there aren't any obstacles in the upper left and lower right corner. A turtle (one or the other) can go from cell (x, y) to one of two cells (x + 1, y) and (x, y + 1), as long as the required cell doesn't contain an obstacle. The turtles have had an argument so they don't want to have any chance of meeting each other along the way. Help them find the number of ways in which they can go from cell (1, 1) to cell (n, m).

More formally, find the number of pairs of non-intersecting ways from cell (1, 1) to cell (n, m) modulo 1000000007 (109 + 7). Two ways are called non-intersecting if they have exactly two common points — the starting point and the final point.

Input

The first line contains two integers n, m (2 ≤ n, m ≤ 3000). Each of the following n lines contains m characters describing the table. The empty cells are marked by characters ".", the cells with obstacles are marked by "#".

It is guaranteed that the upper left and the lower right cells are empty.

Output

In a single line print a single integer — the number of pairs of non-intersecting paths from cell (1, 1) to cell (n, m) modulo 1000000007(109 + 7).

Examples

input

Copy

4 5
.....
.###.
.###.
.....

output

Copy

1

input

Copy

2 3
...
...

output

Copy

1

思路: 找寻从1 1 到n,m 两条不想交的路径。 

定理: 用来求n个起点到n 个终点的不想交路径数。

(https://en.wikipedia.org/wiki/Lindstr%C3%B6m%E2%80%93Gessel%E2%80%93Viennot_lemma

行列式

(2,1)-> ( n-1, m )  ( 2,1 )-> ( n, m-1 )

(1,2) -> (n,m-1)   (1,2)-> (n-1,m)

那么其实每一条相交的路径都对应着一条 交换两个终点的相交路径,所以我们只要减去从21 到 n,m-1 和(1,2)到(n,m-1)相交路径数的积。

代码: 

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int N =3e3+5;
ll dp1[N][N];
ll dp2[N][N];

char mp[N][N];
int n,m;

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",mp[i]+1);
    }

    if(mp[1][2]=='#'||mp[2][1]=='#'||mp[n-1][m]=='#'||mp[n][m-1]=='#'||mp[1][1]=='#'||mp[n][m]=='#'){
        cout<<0<<endl;
        return 0;
    }

    dp1[1][2]=1;  // 找寻从12 到 n-1 ,m 的 方案数   和到 n ,m-1 的方案数
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(mp[i][j]=='#') continue;
            dp1[i][j]=(dp1[i][j]+dp1[i-1][j])%mod;
            dp1[i][j]=(dp1[i][j]+dp1[i][j-1])%mod;
        }
    }
    //cout<<" lu 1 "<<dp1[n-1][m]<<endl;

    dp2[2][1]=1;
    for(int i=1;i<=n;i++){  // 找寻从2 1 到 n ,m-1 的 方案数   和到 n-1 ,m 的方案数
        for(int j=1;j<=m;j++){
            if(mp[i][j]=='#') continue;
            dp2[i][j]=(dp2[i][j]+dp2[i-1][j])%mod;
            dp2[i][j]=(dp2[i][j]+dp2[i][j-1])%mod;
        }
    }

    //cout<<" lu 2 "<<dp2[n][m-1]<<endl;

    ll ans=((dp1[n-1][m]*dp2[n][m-1])%mod-(dp1[n][m-1]*dp2[n-1][m])%mod+mod)%mod;
    cout<<ans<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值