USACO 1.5 Number Triangles (numtri)

时间:2014-02-22 05:33:13   收藏:0   阅读:374
/*
ID: haolink1
PROG: numtri
LANG: C++
*/

//Note: the key point of this problem is you shoud add the vlaue of number
//triangle from bottom to top, then you can see the optimal substructure.
//So you can try DP(dynamic programming),and that‘s it!

//#include <iostream>
#include <fstream>

using namespace std;
const short max_row = 1000;
int numbers[max_row][max_row];

int Max(int first,int second){
    if(first >= second)
        return first;
    return second;
}

int main(){
    ifstream fin("numtri.in");
    short row_num = 0;
    fin >> row_num;
    for(short i = 0; i < row_num; i++){
        for(short j = 0; j <= i; j++){
            fin >> numbers[i][j];
        }  
    }
    for(int i = row_num-2; i >=0; i--){
        for(int j = 0; j <= i; j++){
            //optimal substucture
            numbers[i][j] = Max(numbers[i+1][j],numbers[i+1][j+1])+numbers[i][j]; 
        }
    }
    ofstream fout("numtri.out");
    fout<<numbers[0][0]<< endl;
    return 0;
}

原文:http://blog.csdn.net/damonhao/article/details/19616443

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!