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. 提高VS2010运行速度的技巧+关闭拼写检查

    任务管理器,CPU和内存都不高,为何?原因就是VS2010不停地读硬盘导致的; 写代码2/3的时间都耗在卡上了,太难受了; 研究发现,VS2010如果你装了VC等语言,那么它就会自动装SQL Serv ...

  2. Power BI 连接到 Azure 账单,自动生成报表,可刷新

    开始研究Azure官网等,提供的链接都是错误的,躺了很大的一个坑,配置后根本无法获取账单信息,经过多次查询找到了方向,过来记录一下: 错误的地址(应该是适用于全球版,国内版无法这样获取): https ...

  3. Android上线check_list

    Android 上线 check_list 类型 序号 检查项 结果(pass/no) 安装 卸载 1 非Root环境下的安装.卸载 2 Root环境下的安装.卸载 3 安装文件检查,无泄漏用户信息的 ...

  4. 目后佐道IT教育的品牌故事

    关于目后佐道 目后佐道IT教育作为中国IT职业教育领导品牌,致力于HTML5.UI.PHP.Java+大数据.Python+人工智能.Linux.产品经理.测试.运维等课程培训.100%全程面授,平均 ...

  5. C语言二维数组作为函数参数

    设有整型二维数组a[3][4]如下:0   1   2   34   5   6   78   9  10  11 它的定义为:    int a[3][4]={{0,1,2,3},{4,5,6,7} ...

  6. 解决response在controller返回乱码的解决方式

    乱码的代码 @RequestMapping(value = "/readbook", method = RequestMethod.GET) 加入 produces = " ...

  7. 参考别人的代码写的aes加密,记录一下(AES,ECB模式,填充PKCS5Padding,数据块128位,偏移量无,以hex16进制输出)

    package org.jimmy.autosearch2019.test; import java.security.SecureRandom; import javax.crypto.Cipher ...

  8. selenium-介绍和安装

    前戏 相信大家对web自动化selenium都不陌生,是一个web自动化框架,我在第一家公司的时候,产品是两个星期一个版本,每一次发布测试都要进行回归测试,也就是大家说的点点点,后来我就想,能不能做成 ...

  9. 在tomcat中配置连接池

    在tomcat的conf/Catalina/localhost目录下配置项目路径,tomcat启动是会直接根据配置去加载项目. 虽然配置就一句话,但经常忘,今天记下来. 如果你的项目成名是:mypro ...

  10. 响应式Web设计- 图片

    使用width属性:如果width属性设置为100%,图片会根据上下范围实现响应式的功能. <!DOCTYPE html><html><head><meta ...