数字三角形-poj

题目要求:


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

  在上面的数字三角形中寻找在上面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大。路径上的每一步都只能往左下或右下走。只需要求出这个最大和即可,不必给出具体路径。 
  三角形的行数大于1小于等于100,数字为 0 - 99

输入格式: 
5 //三角形行数。下面是三角形 

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

解题思路:

  用二维数组存放数字三角形

  D[r][j] //表示第i行第j个元素的数值;

  MaxSum(i,j) //表示从根部到第i行最大路径的,所有数值的最大和;

用递归的思想,在D(i,j)位置,下一个能走的位置为D(i+1,j)和D(i+1,j+1),进行递归

  MaxSum(i,j)=max(MaxSum(i+1,j),MaxSum(i+1,j+1))+D[i][j];

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
using namespace std;
int N;
#define  Max 100
int number[Max][Max];

int Maxnum(int x,int y){return x>y?x:y;}
int  Max_road_sum(int i,int j)
{
    if(i==N)
        return number[i][j];
    else
    {
        int num1=Max_road_sum(i+1,j);
        int num2=Max_road_sum(i+1,j+1);
        return Maxnum(num1,num2)+number[i][j];
    }

}
int main()
{

    int i,j;
    cin>>N;
    for(i=1;i<=N;i++)
    {
        for(j=1;j<=i;j++)
            cin>>number[i][j];
    }
    cout<<Max_road_sum(1,1)<<endl;
    return 0;
}
时间: 2024-08-03 10:13:27

数字三角形-poj的相关文章

POJ 2760: 数字三角形

import java.util.Scanner; public class Main { public static void main(String[] args) { int[][] triNumbers = new int[101][101];//0-100内 int[][] triMaxs = new int[101][101]; Scanner in = new Scanner(System.in); int nrow = in.nextInt();//行数 for(int i=0;

POJ 3176-Cow Bowling(dp_数字三角形)

Cow Bowling Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a st

POJ_3176_Cow_Bowling(数字三角形)

描述 http://poj.org/problem?id=3176 给出一个三角形,每个点可以走到它下面两个点,将所有经过的点的值加起来,问最大的和是多少. 分析 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using std :: max; 5 6 const int maxn=355; 7 int a[maxn][maxn],f[maxn][maxn]; 8 int n; 9 10 i

数字三角形

数字三角形必须经过某一个点,使之走的路程和最大 输入格式: 第1行n,表示n行 (n<=25), 第2到n+1行为每个的权值,第n+2行为两个数x,y表示必须经过的点 输出格式: 输出最大值 样例1 输入: 2 1 1 1 1 1 输出: 2 //11 月 23 日 2015 #include <stdio.h> int num[26][26];//存储数字三角形的权值 int route[26][2];//记录临时最优路径 int n; int s1,s2;//以特殊点分为上半段和下半

蓝桥杯 算法训练 ALGO-124 数字三角形

算法训练 数字三角形 时间限制:1.0s   内存限制:256.0MB 问题描述 (图3.1-1)示出了一个数字三角形. 请编一个程序计算从顶至底的某处的一条路 径,使该路径所经过的数字的总和最大. ●每一步可沿左斜线向下或右斜线向下走: ●1<三角形行数≤100: ●三角形中的数字为整数0,1,-99: . (图3.1-1) 输入格式 文件中首先读到的是三角形的行数. 接下来描述整个三角形 输出格式 最大总和(整数) 样例输入 573 88 1 02 7 4 44 5 2 6 5 样例输出 3

4829 [DP]数字三角形升级版

4829 [DP]数字三角形升级版 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 从数字三角形的顶部(如图,第一行的5表示行数)到底部有很多条不同的路径.对于每条路径,把路径上面的数加起来可以得到一个和,且!!!!!!!!! ================================================================================== =================

各种数字三角形

数字三角形 经典例题,有记忆化搜索,正推,逆推三种方法 如果记录路径,可以开一个数组记录状态是由哪个子状态推出来的 #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> using namespace std; const int maxn = 200; int n,dp[maxn][maxn],value[maxn][

【递归】数字三角形 简单dp

[递归]数字三角形 题目描述 对于大多数人来说,“我们是这么的正常,因此也就这么的平庸.”而天才总是与众不同的,所以当邪狼问修罗王:“老大,你蹲在那儿一动不动看了有半个小时了,蚂蚁有那么好看吗?” 修罗王是这样回答的:“我在思索人生的意义,你看这蚂蚁的面前有无数的道路选择,但它不知道选择哪条路可以到达目标,也不知道哪条路上有更多的食物,更不知道现在选择的道路对它以后的影响……” 如图所示,有一个层数为n(n≤1000)的数字三角形.现有一只蚂蚁从顶层开始向下走,每走下一级时,可向左下方向或右下方

hihoCoder 1037 数字三角形 最详细的解题报告

题目来源:hihoCoder 1037 数字三角形 解题思路:请好好看看 提示一.提示二.提示三 具体算法(java版,可以直接AC) import java.util.Scanner; public class Main { public static int[][] rewards; public static int[][] best; public static void main(String[] args) { Scanner scanner = new Scanner(System