Codeforces Round #202 (Div. 1) D. Turtles DP

D. Turtles

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/547/problem/B

Description

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

 

Sample Input

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

Sample Output

1

 

HINT

 

题意

给定一张有坏点的网格图,求左上角走到右下角的两条不相交路径的方案数

 

题解:

考虑如果只有一条路该怎么做
显然 DP 就行了
那么我们定义 Calc ( x 1 , y 1 , x 2 , y 2 ) 为从 ( x 1 , y 1 ) 走到 ( x 2 , y 2 ) 的方案数
如果不考虑相交,那么答案就是 Calc (2,1, n , m  1) * Calc (1, 2, n  1, m )
现在考虑相交后,对于一种相交的方案,我们选择最后一个相交的点,将两人从这个点往后的目标反
转一下,这样可以映射到一条从 (2,1) 走到 ( n  1, m ) 的路径和一条从 (1, 2) 走到 ( n , m  1) 的路径
这样我们就将原先每种不合法的方案和反转后的每种方案建立起了映射
故答案为 Calc (2,1, n , m  1) * Calc (1, 2, n  1, m )  Calc (2,1, n  1, m ) * Calc (1, 2, n , m  1)
时间复杂度  ( nm ) ,可以拿到 100 分

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)  
#define maxn 200001
#define mod 1000000007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

int n,m;
char s[3005][3005];
ll dp[3005][3005];
ll solve(int x,int y,int xx,int yy)
{
    if(s[x][y]=='#')
        return 0;
    memset(dp,0,sizeof(dp));
    dp[x][y]=1;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(s[i][j]=='#')
                continue;
            dp[i][j]+=dp[i-1][j]+dp[i][j-1];
            dp[i][j]%=mod;
        }
    }
    return dp[xx][yy];
}
int main()
{  
    //test;
    n=read(),m=read();
    for(int i=1;i<=n;i++)
        scanf("%s",s[i]+1);
    ll tmp=solve(1,2,n-1,m)*solve(2,1,n,m-1)-solve(1,2,n,m-1)*solve(2,1,n-1,m);
    printf("%d\n",(tmp%mod+mod)%mod);
}

 

posted @ 2015-05-27 19:37  qscqesze  阅读(346)  评论(0编辑  收藏  举报