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\)次询问. 那 ...
随机推荐
- 定时任务-Timer
Timer类的全限定名 java.util.Timer java.util.Timer类的构造函数 public Timer(); public Timer(boolean isDaemon); pu ...
- 【Linux】VirtualBox网络配置桥接模式
VirtualBox网络配置桥接模式 CentOS/RHEL (虚拟机)配置 # 基于桥接模式设置固定 ip cat >> /etc/sysconfig/network-scripts/i ...
- hihocoder1831 80 Days
思路: 令p[i] = a[i] - b[i],p[i + n] = p[i](i = 1,2,...,n),则需要找出一段长度为n的连续序列使此序列的任一前缀和均大于-c.转化如下:首先求序列p的前 ...
- ExpandableListView 安卓二级菜单
ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListView).ExpandableListView允许有两个层次:一级列表中有二级列表.比如在 ...
- 【cpp】new delete
double *M = new double[2*num]; double *T = new double[2 * num]; double *activeM = new double[2 * num ...
- iOS:让UIView覆盖导航栏
当我们想做一个弹出式菜单时,想将导航栏也一起盖住不显示的话,可以用如下语句实现: UIView* myView = /* 你自定义的view */; UIWindow* currentWindow = ...
- 异步 BeginInvoke
委托的异步调用异步多线程的三大特点:1.同步方法卡界面,原因是主线程被占用:异步方法不卡界面,原因是计算交给了别的线程,主线程空闲2.同步方法慢,原因是只有一个线程计算:异步方法快,原因是多个线程同事 ...
- Android(java)学习笔记143:Android中View动画之 XML实现 和 代码实现
1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...
- WPF知识点全攻略05- XAML内容控件
此处简单列举出布局控件外,其他常用的控件: Window:WPF窗口 UserControl:用户控件 Page:页 Frame:用来浏览Page页 Border:嵌套控件,提供边框和背景. Butt ...
- 读懂 Deployment YAML【转】
既然要用 YAML 配置文件部署应用,现在就很有必要了解一下 Deployment 的配置格式,其他 Controller(比如 DaemonSet)非常类似. 还是以 nginx-deploymen ...