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. 开源日志系统比较:scribe、chukwa、kafka、flume

    1. 背景介绍 许多公司的平台每天会产生大量的日志(一般为流式数据,如,搜索引擎的pv,查询等),处理这些日志需要特定的日志系统,一般而言,这些系统需要具有以下特征: (1) 构建应用系统和分析系统的 ...

  2. LeetCode OJ 27. Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. D ...

  3. OC画笔CGContextRef

    1.画线 CGContextRef context = UIGraphicsGetCurrentContext();//context相当于画布 CGContextSetStrokeColorWith ...

  4. nginx 报错 upstream timed out (110: Connection timed out)解决方案【转】

    转自 nginx 报错 upstream timed out (110: Connection timed out)解决方案 - 为程序员服务http://outofmemory.cn/code-sn ...

  5. asp.net 基础

    前台HTML,javascript,后台C# 代码能不在后台写,就不在后台写 WebSite和WebApplication的区别 1)当改变后台代码时,WebApplication需重启浏览器或者重新 ...

  6. git 提高下载速度

    1.  直接下载分支,就不用下载不需要的源码了. git clone --depth 1 git://github.com/TI-OpenLink/wl18xx.git  --branch ol_r8 ...

  7. css 相关

    background-size: auto就会变成就是不会让图像变形的,会自动调整,一般是会设置多少箱像素的, background-size: XXpx XXpx;百分百那就铺满整个区域了 back ...

  8. Linux环境下Android JNI程序的编译

    尊重原创作者,转载请注明出处: http://blog.csdn.net/gemmem/article/details/8993493 在android开发中,有时候需要编写一些C/C++代码,这时候 ...

  9. android cts 命令的说明

    Host help showthis message 帮助文档 exit exitcts command line 退出CTS ls 全部用l替代,--plan直接用p替代,也即 l p .其他类似 ...

  10. 一个完整的项目中,需要的基本gulp

    一个完整的项目需要使用gulp的多种功能,包括—— (1)加载各种需要的插件 var concat=require('gulp'); var clean=require(''gulp); 等等.需要的 ...