[Usaco2015 OPEN] Palindromic Paths
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=4098
[算法]
显然 , 回文路径中第i个字母的位置(x , y)必然满足 : x + y - 1 = i
用f[i][j][k]表示现在在第i步 , 左上的横坐标为j , 右下的横坐标为k , 有多少种方案使得两边路径上的字母序列相同 , DP即可
时间复杂度 : O(N ^ 3)
滚动数组 , 将空间复杂度优化为O(N ^ 2)
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 510
const int P = 1e9 + ; int n;
int f[][MAXN][MAXN];
char mp[MAXN][MAXN]; inline void update(int &x , int y)
{
x += y;
x %= P;
}
inline bool valid(int x , int y)
{
return x >= && x <= n && y >= && y <= n;
} int main()
{ scanf("%d",&n);
for (int i = ; i <= n; i++) scanf("%s",mp[i] + );
if (mp[][] == mp[n][n])
{
f[][][n] = ;
} else
{
printf("0\n");
return ;
}
for (int i = ; i <= n; i++)
{
int now = i & , nxt = now ^ ;
memset(f[nxt] , , sizeof(f[nxt]));
for (int x = ; x <= n; x++)
{
for (int y = ; y <= n; y++)
{
int t1 = i + - x , t2 = * n - y + - i;
if (!valid(x , t1) || !valid(y , t2)) continue;
if (!f[now][x][y]) continue;
if (valid(x , t1 + ))
{
if (valid(y , t2 - ) && mp[x][t1 + ] == mp[y][t2 - ])
update(f[nxt][x][y] , f[now][x][y]);
if (valid(y - , t2) && mp[x][t1 + ] == mp[y - ][t2])
update(f[nxt][x][y - ] , f[now][x][y]);
}
if (valid(x + , t1))
{
if (valid(y , t2 - ) && mp[x + ][t1] == mp[y][t2 - ])
update(f[nxt][x + ][y] , f[now][x][y]);
if (valid(y - , t2) && mp[x + ][t1] == mp[y - ][t2])
update(f[nxt][x + ][y - ] , f[now][x][y]);
}
}
}
}
int ans = ;
for (int i = ; i <= n; i++) update(ans , f[n & ][i][i]);
printf("%d\n" , ans); return ; }
[Usaco2015 OPEN] Palindromic Paths的更多相关文章
- [bzoj4098] [Usaco2015 Open]Palindromic Paths
DP.. f[i][j][k]表示左上结束节点是第i条副对角线上的第j个点,右下结束节点是第n*2-i条副对角线上的第k个点,构成回文的方案数. i那维滚动一下.时间复杂度O(n^3)空间复杂度O(n ...
- [USACO15OPEN]回文的路径Palindromic Paths
[USACO15OPEN]回文的路径Palindromic Paths 题目描述 Farmer John's farm is in the shape of an N \times NN×N grid ...
- TOJ 5020: Palindromic Paths
5020: Palindromic Paths Time Limit(Common/Java):10000MS/30000MS Memory Limit:65536KByteTotal Su ...
- 题解 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 ...
- Codeforces 1205C Palindromic Paths (交互题、DP)
题目链接 http://codeforces.com/contest/1205/problem/C 题解 菜鸡永远做着变巨的梦 然而依然连div1BC题都不会做 要是那天去打cf怕是又要1题滚粗了.. ...
- CF1205C Palindromic Paths
题目链接 问题分析 首先可以想到,坐标和为奇数的位置可以被唯一确定.同样的,如果假定\((1,2)\)是\(0\),那么坐标和为偶数的位置也可以被唯一确定.这样总共使用了\(n^2-3\)次询问. 那 ...
随机推荐
- java常见问题集锦
Eclipse 编译错误 Access restriction:The type *** is not accessible due to restriction on... 解决方案 Eclipse ...
- step 1:begin to identify something in english(to becaome a baby again)
long long ago , i think if i want to improve my english especially computer english . i must do so m ...
- UVA10673 上下界问题
#include <iostream> #include<cstdio> using namespace std; #define LL long long LL a,b,m, ...
- Linux(4):文件属性
文件属性: # 重点: 磁盘空间不足 和 软链接与硬链接的区别 查看文件的属性: # ls lhi 文件 [root@NEO ~]# ls -lhi /etc/hosts 130078 -rw-r-- ...
- ORACLE备份、恢复、常用查询
--第一,启动服务,(如果数据库处于启动状态,那么略过这一步) 打开命令行执行以下语句 net start OracleServiceORCL net start OracleOraDb10g_ ...
- bitcms-比特内容管理系统 3.1版源码发布
bitcms比特内容管理系统,经过几个版本的更新和客户的使用已经基本上完善了.下面主要介绍下他的运行环境和功能. 一.运行环境:windows server+IIS bitcms采用Entity Fr ...
- Codevs 3556 科技庄园==洛谷 P2760
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Life是codevs的用户,他是一个道德极高的用户,他积极贯彻党的十八大精神, ...
- hdu 2438 Turn the corner [ 三分 ]
传送门 Turn the corner Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- MongoDB基本管理命令操作
1. 查看所有数据库: show dbs 或: show databases 注意: 该命令不会显示新创建的空数据库,若想显示需要向空数据库插入一些数据. MongoDB中默认的数据库为test,若果 ...
- POJ 1422【最小路覆盖数】
题意: 背景: 小镇有n个路口,空降兵可以在任意路口降落.有m条通往别的路口的单向边,但是不会出现循环. 问最少空降多少个士兵可以走完所有路口. 数据输入: 测试组数 t 每组有: 路口数 n 边数 ...