5020: Palindromic Paths 

Time Limit(Common/Java):10000MS/30000MS     Memory Limit:65536KByte
Total Submit: 8            Accepted:4

Description

Given an N×N grid of fields (1≤N≤500), each labeled with a letter in the alphabet. For example:

ABCD

BXZX
CDXB
WCBA

Each day, Susa walks from the upper-left field to the lower-right field, each step taking her either one field to the right or one field downward. Susa keeps track of the string that she generates during this process, built from the letters she walks across. She gets very disoriented, however, if this string is a palindrome (reading the same forward as backward), since she gets confused about which direction she had walked.

Please help Susa determine the number of distinct routes she can take that correspond to palindromes. Different ways of obtaining the same palindrome count multiple times. Please print your answer modulo 1,000,000,007.

Input

The first line of input contains N, and the remaining N lines contain the N rows of the grid of fields. Each row contains N characters that are in the range A...Z.

Output

Please output the number of distinct palindromic routes Susa can take, modulo 1,000,000,007.

Sample Input

4
ABCD
BXZX
CDXB
WCBA

Sample Output

12

Hint

Susa can make the following palindromes

1 x "ABCDCBA"

1 x "ABCWCBA"

6 x "ABXZXBA"

4 x "ABXDXBA"

Source

USACO 2015 US Open

一道不错的枚举+滚动数组,美滋滋,f[i][j][k]表示第一个点在第i行,第2个点在第j行都走了k步的方案数

#include <stdio.h>
#include <algorithm>
using namespace std;
typedef __int64 ll;
const int mod=1e9+;
char s[][];
ll dp[][][];
int main() {
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%s",s[i]+);
int now=,pre=;
if(s[][]!=s[n][n]) {
return ,printf("0\n");
}
dp[][n][pre]=;
for(int k=; k<=n; k++) {
for(int i=; i<=k; i++)
for(int j=n; j>=i&&j>=n-k+; j--) {
if(s[i][k-i+]==s[j][n-k+n-j+])
dp[i][j][now]=(dp[i-][j][pre]+dp[i][j][pre]+dp[i][j+][pre]+dp[i-][j+][pre])%mod;
else dp[i][j][now]=;
}
swap(now,pre);
}
ll ans=;
for(int i=; i<=n; i++) {
ans=(ans+dp[i][i][pre])%mod;
}
printf("%lld",ans);
return ;
}

TOJ 5020: Palindromic Paths的更多相关文章

  1. [USACO15OPEN]回文的路径Palindromic Paths

    [USACO15OPEN]回文的路径Palindromic Paths 题目描述 Farmer John's farm is in the shape of an N \times NN×N grid ...

  2. 题解 P3126 【[USACO15OPEN]回文的路径Palindromic Paths】

    P3126 [USACO15OPEN]回文的路径Palindromic Paths 看到这题题解不多,蒟蒻便想更加通俗易懂地分享一点自己的心得,欢迎大佬批评指正^_^ 像这种棋盘形的两边同时做的dp还 ...

  3. Educational Codeforces Round 89 (Rated for Div. 2) C Palindromic Paths

    题目链接:Palindromic Paths 题意: 给你一个n行m列的矩阵,这个矩阵被0或者1所填充,你需要从点(1,1)走到点(n,m).这个时候会有很多路径,每一条路径对应一个01串,你可以改变 ...

  4. [USACO15OPEN]回文的路径Palindromic Paths 2.0版

    题目描述 农夫FJ的农场是一个N*N的正方形矩阵(2\le N\le 5002≤N≤500),每一块用一个字母作标记.比如说: ABCD BXZX CDXB WCBA 某一天,FJ从农场的左上角走到右 ...

  5. Palindromic Paths(DP)

    描述 Given an N×N grid of fields (1≤N≤500), each labeled with a letter in the alphabet. For example: A ...

  6. [bzoj4098] [Usaco2015 Open]Palindromic Paths

    DP.. f[i][j][k]表示左上结束节点是第i条副对角线上的第j个点,右下结束节点是第n*2-i条副对角线上的第k个点,构成回文的方案数. i那维滚动一下.时间复杂度O(n^3)空间复杂度O(n ...

  7. [Usaco2015 OPEN] Palindromic Paths

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4098 [算法] 显然 , 回文路径中第i个字母的位置(x , y)必然满足 : x ...

  8. Codeforces 1205C Palindromic Paths (交互题、DP)

    题目链接 http://codeforces.com/contest/1205/problem/C 题解 菜鸡永远做着变巨的梦 然而依然连div1BC题都不会做 要是那天去打cf怕是又要1题滚粗了.. ...

  9. CF1205C Palindromic Paths

    题目链接 问题分析 首先可以想到,坐标和为奇数的位置可以被唯一确定.同样的,如果假定\((1,2)\)是\(0\),那么坐标和为偶数的位置也可以被唯一确定.这样总共使用了\(n^2-3\)次询问. 那 ...

随机推荐

  1. LaTeX入门简介

    原创链接 http://blog.csdn.net/perfumekristy/article/details/8515272 1.LaTeX软件的安装和使用 方法A(自助):在MikTeX的官网下载 ...

  2. tomcat+nginx 横向扩展

    1.分别在电脑上部署两个tomcat tomcat1  tomcat2 2.不是nginx 并启动 输入 localhost 并进入nginx欢迎界面,证明部署成功 3.修改nginx 配置 ngin ...

  3. Jquery 错误提示插件

    这是一个简单的输入框错误提示插件,可拓展! .jq-error{ font-size:12px; min-width:150px; width:auto; max-width:350px; line- ...

  4. (十二)mybatis之动态代理

    mybatis之动态代理的应用 在前文(https://www.cnblogs.com/NYfor2018/p/9093472.html)我们知道了,Mybatis的使用需要用到Mapper映射文件, ...

  5. 一个简单的例子教会您使用javap

    javap是JDK自带的工具: 这篇文章使用下面这段简单的Java代码作为例子进行讲解. class Outer { Nested nested; Nested getNested() { retur ...

  6. js 上传图片

    <div class="block-input" style="height: 90px"> <span><i class=&qu ...

  7. ucosii(2.89)mutex 应用要点

    mutex 的创建在于共享资源打交道是可以可以保证满足互斥条件:1,必须保证继承优先级要高于可能与相应共享资源打交道的任务中优先级最高的优先级.2,不要将占有Mutex的任务挂起,也不要让占有mute ...

  8. python 判断路径是否存在

    import os os.path.exists(文件绝对路径)

  9. rpn网络结构再分析

    这是rpn网络train阶段的网络结构图 rpn_conv1之前的网络是特征提取层,也是和fast rcnn共享的层.rpn_conv1是一层1*1的卷积,这一层是单独为rpn网络多提取一层特征,这一 ...

  10. ELK日志分析 学习笔记

    (贴一篇之前工作期间整理的elk学习笔记) ELK官网 https://www.elastic.co   ELK日志分析系统 学习笔记 概念:ELK = elasticsearch + logstas ...