HDU 5617 Jam's maze 巧妙DP
题意:给你一个字符矩阵,从(1,1)到(n,n)有很多种走法,每一种走法形成一个字符串,问有多少种走法形成的字符串是回文的
分析:(粘贴BC题解)
的是回文串,有人会想到后缀数组自动机马拉车什么的,其实只要求方案数很多,所以我们应该想到动态规划,首先是状态的定义,我们可以想着从(1,1)(1,1)和(n,n)(n,n)开始走,然后走到相遇。所以我们定义状态可以定义为f[x_1][y_1][x_2][y_2]f[x1][y1][x2][y2]表示所对应的两个点(x_1,y_1)(x_2,y_2)(x1,y1)(x2,y2),这样的话考虑到数据的范围,显然会爆空间,然后我们想一想就是走多少步和知道x_1,x_2x1,x2的位置,就可以确定y_1,y_2y1,y2的位置。于是我们把状态定义为f[i][x_1][x_2]f[i][x1][x2]即可,状态转移很显然,如果相等的话把四个方向都扫一下即可。因为还是会爆空间,所以我们把第一维改成滚动数组就解决问题
注:其实这个题解的意思就是从1,1 和n,n 开始按距离 斜着更新,最后统计相遇的地方,相遇显然就是dp[n-1][i][i]
难点就是斜着更新不好想,还有就是滚动数组的空间优化
然后附上丑的代码;
#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <climits>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <list>
#include <bitset>
#include <vector>
#include <cassert>
using namespace std;
const int maxn=;
const int mod=;
int dp[][maxn][maxn];
char s[maxn][maxn];
int T,n;
void add(int &x,int y)
{
x+=y;
if(x>mod)x-=mod;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;++i)
scanf("%s",s[i]+);
if(s[][]!=s[n][n])
{
printf("0\n");
continue;
}
int cur=;
memset(dp[cur],,sizeof(dp[cur]));
dp[cur][][n]=;
for(int i=;i<n;++i)
{
cur^=;
memset(dp[cur],,sizeof(dp[cur]));
for(int x1=;x1<=i+;++x1)
{
for(int x2=n;x2>=n-i;--x2)
{
int y1=i+-x1;
int y2=n*-x2-i;
if(s[x1][y1]!=s[x2][y2])continue;
add(dp[cur][x1][x2],dp[cur^][x1][x2]);
add(dp[cur][x1][x2],dp[cur^][x1][x2+]);
add(dp[cur][x1][x2],dp[cur^][x1-][x2]);
add(dp[cur][x1][x2],dp[cur^][x1-][x2+]);
}
}
}
int ans=;
for(int i=;i<=n;++i)
add(ans,dp[cur][i][i]);
printf("%d\n",ans);
}
return ;
}
HDU 5617 Jam's maze 巧妙DP的更多相关文章
- HDU 5617 Jam's maze dp+滚动数组
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617 bc(中文):http://bestcoder.hdu.edu.cn/contest ...
- HDU 5616 Jam's balance 背包DP
Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell ...
- hdu 5616 Jam's balance(dp 正反01背包)
来自官方题解: AC代码: #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream ...
- cdq分治(hdu 5618 Jam's problem again[陌上花开]、CQOI 2011 动态逆序对、hdu 4742 Pinball Game、hdu 4456 Crowd、[HEOI2016/TJOI2016]序列、[NOI2007]货币兑换 )
hdu 5618 Jam's problem again #include <bits/stdc++.h> #define MAXN 100010 using namespace std; ...
- HDU 5616 Jam's balance(Jam的天平)
HDU 5616 Jam's balance(Jam的天平) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 1231 最大连续子序列 --- 入门DP
HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...
- hdu 4778 Gems Fight! 博弈+状态dp+搜索
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4102743.html 题目链接:hdu 4778 Gems Fight! 博弈+状态dp+搜 ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
随机推荐
- CLSID {91493441-5A91-11CF-8700-00AA0060263B}错误
最近遇到一个棘手的问题,在C#中处理PPT转HTML的过程中需要用到COM组件,按照往常的设置如下: 1. Start->Run->DCOMCNFG->Component Servi ...
- python学习笔记21(正则表达式)
正则表达式模式: 模式 描述 ^ 匹配的开始的 $ 匹配行尾 . 匹配除换行符的任何单个字符.使用-m选项允许其匹配换行符也是如此. [...] 匹配括号内任何单个字符 [^...] 匹配非单个字符集 ...
- iOS中的固定 高度
iOS键盘高度 英文 216(不带联想功能) 英文 252(带联想功能) 中文 252 系统自带表情键盘的高度是 253 在使用系统键盘的时候,如果遇到输入框被键盘挡住的情况 就要用通知中心 ...
- 浅淡Windows7 32位与64位/x86与x64的区别
看到有很多会员问到底是选Windows7 x86,还是选x64.这里简单的谈一下这这两种系统的区别. 简单的说x86代表32位操作系统 x64代表64位操作系统. 简单的判断电脑是否支持64位操作系 ...
- maven添加jar包依赖
maven的东西使用了一段时间,但是每次使用都多少有点含糊,所以总结一下. 目前主要使用是在jar包的控制上 原理: 在本地,指定一个文件夹,便是maven的仓库,maven会从远程的中央仓库中下载你 ...
- [转载]C#导入XLS数据到数据库
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- zoj 3725
题意: n个格子排成一条直线,可以选择涂成红色或蓝色,问最少 m 个连续为红色的方案数. 解题思路: 应该是这次 ZOJ 月赛最水的一题,可惜还是没想到... dp[i] 表示前 i 个最少 m 个连 ...
- 一个只需要点 「下一步」就完成监控 Windows
Cloud Insight 此前已然支持 Linux 操作系统,支持20多中数据库中间件等组件,多种操作,多种搭配,服务器监控玩的其乐无穷啊!但想想还有许多 Windows 的小伙伴没有体验过,所以在 ...
- Interface Serializable
public interface Serializable Serializability of a class is enabled by the class implementing the ja ...
- HDU1565+状态压缩dp
简单的压缩状态 dp /* 状态压缩dp 同hdu2167 利用滚动数组!! */ #include<stdio.h> #include<string.h> #include& ...