TOJ 5020: Palindromic Paths
5020: Palindromic Paths 
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
一道不错的枚举+滚动数组,美滋滋,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的更多相关文章
- [USACO15OPEN]回文的路径Palindromic Paths
[USACO15OPEN]回文的路径Palindromic Paths 题目描述 Farmer John's farm is in the shape of an N \times NN×N grid ...
- 题解 P3126 【[USACO15OPEN]回文的路径Palindromic Paths】
P3126 [USACO15OPEN]回文的路径Palindromic Paths 看到这题题解不多,蒟蒻便想更加通俗易懂地分享一点自己的心得,欢迎大佬批评指正^_^ 像这种棋盘形的两边同时做的dp还 ...
- Educational Codeforces Round 89 (Rated for Div. 2) C Palindromic Paths
题目链接:Palindromic Paths 题意: 给你一个n行m列的矩阵,这个矩阵被0或者1所填充,你需要从点(1,1)走到点(n,m).这个时候会有很多路径,每一条路径对应一个01串,你可以改变 ...
- [USACO15OPEN]回文的路径Palindromic Paths 2.0版
题目描述 农夫FJ的农场是一个N*N的正方形矩阵(2\le N\le 5002≤N≤500),每一块用一个字母作标记.比如说: ABCD BXZX CDXB WCBA 某一天,FJ从农场的左上角走到右 ...
- Palindromic Paths(DP)
描述 Given an N×N grid of fields (1≤N≤500), each labeled with a letter in the alphabet. For example: A ...
- [bzoj4098] [Usaco2015 Open]Palindromic Paths
DP.. f[i][j][k]表示左上结束节点是第i条副对角线上的第j个点,右下结束节点是第n*2-i条副对角线上的第k个点,构成回文的方案数. i那维滚动一下.时间复杂度O(n^3)空间复杂度O(n ...
- [Usaco2015 OPEN] Palindromic Paths
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4098 [算法] 显然 , 回文路径中第i个字母的位置(x , y)必然满足 : x ...
- Codeforces 1205C Palindromic Paths (交互题、DP)
题目链接 http://codeforces.com/contest/1205/problem/C 题解 菜鸡永远做着变巨的梦 然而依然连div1BC题都不会做 要是那天去打cf怕是又要1题滚粗了.. ...
- CF1205C Palindromic Paths
题目链接 问题分析 首先可以想到,坐标和为奇数的位置可以被唯一确定.同样的,如果假定\((1,2)\)是\(0\),那么坐标和为偶数的位置也可以被唯一确定.这样总共使用了\(n^2-3\)次询问. 那 ...
随机推荐
- 学习typescript(二)
学习typescript(二) ts 与 js 交互 ts 调用 js module使用 分为两种情况: ts 调用自己写的 js ts 调用别人写的 js 也就通过 npm 安装的 第一种情况处理如 ...
- mongodb sort
sort() 方法 要在 MongoDB 中的文档进行排序,需要使用sort()方法. sort() 方法接受一个文档,其中包含的字段列表连同他们的排序顺序.要指定排序顺序1和-1. 1用于升序排列, ...
- 洛谷 P1048 采药
采药 01背包模板题. #include <iostream> #include <cstdio> using namespace std; //Mystery_Sky //一 ...
- js如何调用电脑的摄像头
闲来无事,用js写了一个调用摄像头的demo,并用canvas显示保存.这个功能很实用,比如上传用户的头像,即时拍照及时上传. Html: <video width="200px&qu ...
- OAuth2.0基本原理及应用
OAuth2.0基本原理及应用 一.OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 在详细讲解OAuth 2.0之前,需要了解几个专 ...
- SQL server 数据库基础语句 查询语句
这一章要学习查询语句 我看car这一数据 我们就开始打上 select *from car 条件修改 update 表名 set 列名1=值1 where 列名2=值2 //当列名2=值2时 ...
- 洛谷 P1009 阶乘之和
题目描述 用高精度计算出S=1!+2!+3!+…+n!(n≤50) 其中“!”表示阶乘,例如:5!=5*4*3*2*1. 输入输出格式 输入格式: 一个正整数N. 输出格式: 一个正整数S,表示计算结 ...
- Codeforces Gym 100650B Countdown (离线)
题目链接:http://codeforces.com/gym/100650 根据给出的树和d,求出一些结点,这些结点形成子树的第d层结点数应该尽量多,具体要求可以参考题目. dfs一个结点前保存询问深 ...
- js 上传图片
<div class="block-input" style="height: 90px"> <span><i class=&qu ...
- WebStorm换主题(护眼)
一.下载喜欢颜色的主题 http://www.phpstorm-themes.com/ 我用的豆沙绿护眼 <scheme name="Solarized Light My" ...