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. phpmyadmin消除无法保存最近表的提示

    运行 sudo dpkg-reconfigure phpmyadmin 重新配置phpmyadmin ip选择127.0.0.1,端口3306,"MySQL username for php ...

  2. datagrid数据网格获取所有选中行的索引,插入某个列值为其他列的运算值

    获取所有选中行的索引,存入数组ary中: var data=$("#dg").datagrid("getSelections"); var ary=[]; fo ...

  3. C语言中的fprintf函数详解

    fprintf 功能 传送格式化输出到一个文件中 用法 #include   stdio.h int fprintf( FILE *stream, const char *format,...); f ...

  4. Azure 项目构建 – 部署 Jenkins 服务器以实现持续集成(CI)

    通过完整流程详细介绍了如何通过 Azure 虚拟机.虚拟网络等服务在 Azure 平台上快速搭建 Jenkins 服务器. 此系列的全部课程 https://school.azure.cn/curri ...

  5. Ubuntu 14.04 配置confluence破解

    1. 配置java环境,请参展我的另一篇博客 http://www.cnblogs.com/youran-he/p/8607155.html 2. 下载文件 https://pan.baidu.com ...

  6. java 核心技术卷一笔记 6 .2接口 lambda 表达式 内部类

    6.2 接口实例 6.2.1 接口与回调 在java.swing包中有一个Timer类,可以使用它在到达给定的时间间隔时发出通告,假如程序中有一个时钟,就可以请求每秒钟获得一个通告,以便更新时钟的表盘 ...

  7. php接受axios数据

    var params = { username: 'admin', password: '123456' } axios.post('test.php', params).then(res => ...

  8. POJ2402 Palindrome Numbers第K个回文数——找规律

    问题 给一个数k,给出第k个回文数  链接 题解 打表找规律,详见https://www.cnblogs.com/lfri/p/10459982.html,差别仅在于这里从1数起. AC代码 #inc ...

  9. scss引入的问题

    导入.sass或.scss文件 css有一个不太常用的特性,即@import 导入功能,它允许在一个css文件中导入其他css文件.然而,结果是只有执行到@import 规则时,浏览器才会去下载其他c ...

  10. NASM 之 helloworld1

    SECTION .data msg: db "Hello World!", 0x0a len: equ $-msg SECTION .text global _main kerne ...