P1084 数字三角形4

题解:dp+dfs.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = ;
int dp[maxn][maxn];
int cnt[maxn][maxn];
int n;
int dfs(int x,int y)
{
if(x==&&y==) return cnt[][];
if(y==) return dfs(x-,y)+cnt[x][y];
if(x>)return max(dfs(x-,y),dfs(x-,y-))+cnt[x][y];
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&cnt[i][j]);
}
}
int x,y;
scanf("%d %d",&x,&y);
for(int i=;i<=n;i++) dp[n][i] = cnt[n][i];
for(int i=n-;i>=x;i--)
{
for(int j=;j<=i;j++)
{
dp[i][j] = max(dp[i+][j],dp[i+][j+])+cnt[i][j];
}
}
int sum = ;
sum = dfs(x,y);
sum = sum+dp[x][y]-cnt[x][y];
printf("%d\n",sum);
return ;
}
/*
4
7
1 2
3 4 5
7 3 8 9
3 2
*/

卷珠帘

P1004 滑雪

题解:dfs.咦,我本来是来炼dp的。。。不做水题了。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
const int maxn = ;
int cnt[maxn][maxn];
int vis[maxn][maxn];
int dp[maxn][maxn];
int dfs(int x,int y)
{
if(x<||y<) return ;
if(x>n||y>m) return ;
if(vis[x][y]) return dp[x][y];
vis[x][y] = ;
int a,b,c,d;
a = b = c = d = ;
if(cnt[x-][y]<cnt[x][y]) a = dfs(x-,y);
if(cnt[x+][y]<cnt[x][y]) b = dfs(x+,y);
if(cnt[x][y+]<cnt[x][y]) c = dfs(x,y+);
if(cnt[x][y-]<cnt[x][y]) d = dfs(x,y-);
dp[x][y] = max(max(a,b),max(c,d))+;
return dp[x][y];
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%d",&cnt[i][j]);
}
}
int maxx = ;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
maxx = max(dfs(i,j),maxx);
}
}
printf("%d\n",maxx);
}

卷珠帘

nyoj15 括号匹配(二)

题解:接触的第一道区间dp题。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = ;
char s[maxn];
int dp[maxn][maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s+);
int n = strlen(s+);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(i == j) dp[i][j] = ;
else dp[i][j] = ;
}
dp[i][i-] = ;
}
//i和j分别表示一段连续字符的起点和终点
for(int l=;l<=n-;l++)
{
for(int i=;i+l<=n;i++)
{
int j = i+l;
if(s[i]=='('&&s[j]==')'||(s[i]=='['&&s[j]==']')) dp[i][j] = min(dp[i][j],dp[i+][j-]);
for(int k=i;k<j;k++)
{
dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
}
printf("%d\n",dp[][n]);
}
} /*
31
()([)][])
*/

卷珠帘

修炼dp( 2 )的更多相关文章

  1. 修炼dp(1)

    从最简单的开始: POJ:The Triangle #include <cstdio> #include <algorithm> #include <cstring> ...

  2. hdoj 2059 :龟兔赛跑 (DP)[转]

      转的别人的找了很多就这个比较好理解.   Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下 ...

  3. 杭电2059(dp)

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. 【巧妙消维DP】【HDU2059】龟兔赛跑

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. hdu 2059 龟兔赛跑(dp)

    龟兔赛跑 Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成 ...

  6. 龟兔赛跑(DP)

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. 「NOI2013」小 Q 的修炼 解题报告

    「NOI2013」小 Q 的修炼 第一次完整的做出一个提答,花了半个晚上+一个上午+半个下午 总体来说太慢了 对于此题,我认为的难点是观察数据并猜测性质和读入操作 我隔一会就思考这个sb字符串读起来怎 ...

  8. HDU 2059 龟兔赛跑(超级经典的线性DP,找合适的j,使得每个i的状态都是最好的)

    龟兔赛跑 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  9. hdu 2059:龟兔赛跑(动态规划 DP)

    龟兔赛跑 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

随机推荐

  1. hdu_1115_Lifting the Stone(求多边形重心)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1115 题意:给你N个点围成的一个多边形,让你求这个多边形的重心. 题解: 将多边形划分为若干个三角形. ...

  2. OpenCV ——背景建模之CodeBook(1)

    1,CodeBook算法流程介绍 CodeBook算法的基本思想是得到每个像素的时间序列模型.这种模型能很好地处理时间起伏,缺点是需要消耗大量的内存.CodeBook算法为当前图像的每一个像素建立一个 ...

  3. Heartbeat+DRBD+MySQL高可用方案【转】

    转自Heartbeat+DRBD+MySQL高可用方案 - yayun - 博客园 http://www.cnblogs.com/gomysql/p/3674030.html 1.方案简介 本方案采用 ...

  4. kill -0

    http://unix.stackexchange.com/questions/169898/what-does-kill-0-do 检查有没有权限杀他

  5. arcconf

    arcconf create 1 logicaldrive max volume 0 31 noprompt 创建 Logical Drive, 这里 0 31 就是之前记录的 Channel, De ...

  6. T-shirts Distribution

    T-shirts Distribution time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. php取随机数 explode劫取字符串 时间定义随机数

    php取随机数 <?phpfunction randomkeys($length){ $pattern='1234567890'; for($i=0;$i<$length;$i++) {  ...

  8. php实现json

    <?PHP function __json_encode( $data ) { if( is_array($data) || is_object($data) ) { $islist = is_ ...

  9. C语言版的16进制与字符串互转函数

    http://www.cnblogs.com/nio-nio/p/3309367.html /* // C prototype : void StrToHex(BYTE *pbDest, BYTE * ...

  10. GetClientRect

    这个函数好像就是对应于视口的,获取视口的宽高 #include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPAR ...