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[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int n,m;
char s[][];
ll dp[][];
ll solve(int x,int y,int xx,int yy)
{
if(s[x][y]=='#')
return ;
memset(dp,,sizeof(dp));
dp[x][y]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(s[i][j]=='#')
continue;
dp[i][j]+=dp[i-][j]+dp[i][j-];
dp[i][j]%=mod;
}
}
return dp[xx][yy];
}
int main()
{
//test;
n=read(),m=read();
for(int i=;i<=n;i++)
scanf("%s",s[i]+);
ll tmp=solve(,,n-,m)*solve(,,n,m-)-solve(,,n,m-)*solve(,,n-,m);
printf("%d\n",(tmp%mod+mod)%mod);
}

Codeforces Round #202 (Div. 1) D. Turtles DP的更多相关文章

  1. Codeforces Round #202 (Div. 2)

    第一题水题但是wa了一发,排队记录下收到的25,50,100,看能不能找零,要注意100可以找25*3 复杂度O(n) 第二题贪心,先找出最小的花费,然后就能得出最长的位数,然后循环对每个位上的数看能 ...

  2. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

  3. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

  4. Codeforces Round #276 (Div. 1) D. Kindergarten dp

    D. Kindergarten Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/proble ...

  5. Codeforces Round #260 (Div. 1) A - Boredom DP

    A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...

  6. Codeforces Round #533 (Div. 2) C.思维dp D. 多源BFS

    题目链接:https://codeforces.com/contest/1105 C. Ayoub and Lost Array 题目大意:一个长度为n的数组,数组的元素都在[L,R]之间,并且数组全 ...

  7. Codeforces Round #539 (Div. 2) 异或 + dp

    https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...

  8. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  9. Codeforces Round #202 (Div. 1) A. Mafia 贪心

    A. Mafia Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...

随机推荐

  1. webpack中的静态资源处理

    你可能已经注意到,在我们的项目结构里,有两个静态文件的路径,分别是:src/assets 和 static/.那这两个到底有什么区别呢? Webpacked 资源 为了回答这个问题,我们首先需要理解w ...

  2. webgote的例子(5)Sql注入(Blog)

    SQL Injection - Stored (Blog) (本章内容):留言板的注入 看到这个页面先看以下这个页面是做什么的.进行正常的写入发现我每写一句话,其内容都会写到下面的entry里面 在尝 ...

  3. android的wake_lock介绍

    Wake Lock是一种锁的机制, 只要有人拿着这个锁,系统就无法进入休眠, 可以被用户态程序和内核获得. 这个锁可以是有超时的或者是没有超时的, 超时的锁会在时间过去以后自动解锁. 如果没有锁了或者 ...

  4. CTSC 2017 游记

    惨啊,弱菜选手只报上了CTSC,去不了APIO. day -1 晚上的时候,坐上了去帝都的卧铺. 由于第二天就是luogu月赛round1,还得在火车上赶工出题... 颓了好长时间,把题面写出来了,用 ...

  5. 数据库介绍及MySQL安装

    阅读目录 一.数据库是什么? 二.数据库特点 三. 什么是数据库管理系统(DataBase Management System 简称DBMS) 四.数据库服务器.数据管理系统.数据库.表与记录的关系( ...

  6. C#矩形框沿直线移动

    C#中用GDT+的一系列方式,可以绘制各种图形:点,直线,圆形,矩形...... C#中这些图形的绘制,一般教程的demo中给出的代码,是在Form1_Paint(object sender, Pai ...

  7. WMI技术介绍和应用——WMI概述

    https://blog.csdn.net/breaksoftware/article/details/8424317

  8. ubuntu安装Shutter截图工具以及设置系统快捷键

    一.安装截图工具 Shutter 1. 添加安装包软件源 sudo add-apt-repository ppa:shutter/ppa 2. 更新源并安装 shutter sudo apt-get ...

  9. Python全栈开发之9、面向对象、元类以及单例

    前面一系列博文讲解的都是面向过程的编程,如今是时候来一波面向对象的讲解了 一.简介 面向对象编程是一种编程方式,使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就是对 “类” 和 “对象” ...

  10. Socket 异步

    摘要: System.Net.Sockets.Sockte 类有一组增强功能,提供可供专用的高性能套接字应用程序使用的可选异步模式,SocketAsyncEventArgs 类就是这一组增强功能的一部 ...