6581 Number Triangle

时间限制:500MS  内存限制:1000K
提交次数:57 通过次数:47

题型: 编程题   语言: G++;GCC

Description

        7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
(Figure 1)

Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.

输入格式

Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle.
The following N lines describe the data of the triangle. The number of rows in the triangle is &gt; 1 but <= 100.
The numbers in the triangle, all integers, are between 0 and 99.

输出格式

Your program is to write to standard output. The highest sum is written as an integer.

输入样例

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

输出样例

30

作者

admin

  最原始的数塔问题,,经典的动态规划问题。状态转移方程:dp[i][j]=max(dp[i-1][j-1],dp[i-1][j])+a[i][j];

其他细节见代码注释:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; int a[][];//a[i][i]为数塔上点[i][i]处输入的值
int dp[][];//dp[i][j]为走到点[i][j]所能得的最大值
int main()
{
//freopen("input.txt","r",stdin);
memset(a,,sizeof(a)); //初始化数组
memset(dp,,sizeof(dp));
int n;
scanf("%d",&n); //输入数据
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&a[i][j]);
}
}
//
dp[][]=a[][];
if(n==) //n为1就直接输入塔顶数字
{
printf("%d\n",dp[][]);
return ;
}
//状态转移方程
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
//走到第a[i][j]位置能得到的最大值dp[i][j]就是等于选择从走到a[i-1][j-1]和走到
//a[i-1][j]中值较大的那种走法里再加上a[i][j];
dp[i][j]=max(dp[i-][j-],dp[i-][j])+a[i][j];
}
}
//
int Max=-;
for(int i=;i<=n;i++)//扫描塔的最下层,找出最大值
if(dp[n][i]>Max)
Max=dp[n][i];
printf("%d\n",Max);
return ;
}

6581 Number Triangle的更多相关文章

  1. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  2. The Triangle

    针对如下形式的ACM试题,大多出自南阳理工学院的在线ACM试题(网址: 南阳理工在线评测系统),在此非常感谢,同时也非常感谢作者的分享! 时间限制:1000 ms  |  内存限制:65535 KB ...

  3. POJ 1163:The Triangle

    Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a progr ...

  4. poj 1163 The Triangle

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43809   Accepted: 26430 De ...

  5. 【动态规划】The Triangle

    问题 E: [动态规划]The Triangle 时间限制: 1 Sec  内存限制: 128 MB提交: 24  解决: 24[提交][状态][讨论版] 题目描述 73 88 1 02 7 4 44 ...

  6. 【LeetCode】120 - Triangle

    原题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...

  7. Poj 1163 The Triangle 之解题报告

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42232   Accepted: 25527 Description 7 3 ...

  8. OpenJudge/Poj 1163 The Triangle

    1.链接地址: http://bailian.openjudge.cn/practice/1163 http://poj.org/problem?id=1163 2.题目: 总时间限制: 1000ms ...

  9. POJ1163——The Triangle

    Description 73 88 1 02 7 4 44 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program t ...

随机推荐

  1. SQL之LEFT JOIN,EIGHT JOIN,INSERT JOIN的区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  2. BZOJ 2431 逆序对数列 DP

    2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec Memory Limit: 128 MB Description 对于一个数列{ai},如果有i< j且ai> ...

  3. 6.10---mybatis的实体---接口---接口映射---主配置文件

  4. unicode、UTF-8、UTF-16的历史

    1:中国人民通过对 ASCII 编码的中文扩充改造,产生了 GB2312 编码,可以表示6000多个常用汉字. 2:汉字实在是太多了,包括繁体和各种字符,于是产生了 GBK 编码,它包括了 GB231 ...

  5. Retrofit 传递json 和 复杂参数类型List<T>

    1 首先你要定义一个接口 @POST Call<String> post(@Url String url, @Body String info); 2 创建一个service public ...

  6. centos6 或者 centos7 重置遗忘的root密码

    centos7 1.重启服务器. 2.在出现如下界面时,按“e”键,进入内核边界页面,如下图: 3.进入后,在“linux16”这行参数最后添加“rd.break”参数,输入完后,同时安装“ctrl” ...

  7. java JDBC连接 Sqlserver 非默认的实例名问题

    一般我们在连接数据库的时候都是用的默认实例名,今天遇到了用非默认是实例名:连接代码如下(Java): <property name="url" value="jdb ...

  8. 【转载】关于DBUtils中QueryRunner的一些解读

    前面已经有文章说了DBUtils的一些特性, 这里再来详细说下QueryRunner的一些内部实现, 写的有错误的地方还恳请大家指出. QueryRunner类 QueryRunner中提供对sql语 ...

  9. centos7安装mwget下载资源,提升下载速度

    1.安装mwget wget http://jaist.dl.sourceforge.net/project/kmphpfm/mwget/0.1/mwget_0.1.0.orig.tar.bz2 ta ...

  10. php 后端实现JWT认证方法

    JWT是什么 JWT是json web token缩写.它将用户信息加密到token里,服务器不保存任何用户信息.服务器通过使用保存的密钥验证token的正确性,只要正确即通过验证.基于token的身 ...