poj 1163:The Triangle

这道题是动态规划的基础题目,解题思路参考《算法基础与在线实践》一书

总时间限制:
1000ms
内存限制:
65536kB
poj 1163:The Triangle_第1张图片
描述

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 > 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.

这串英文的大意是,找到一个路径,从最上层到最下层的数字的总和最大,求出这个最大值。这个三角形的行是1到100的整数

这道题可以用递归做

某个节点(i行,j列)的递归的公式是:maxlength(i,j) = max(maxlength(i+1,j),maxlength(i+1,j+1))+dp[i][j];

递归结束条件是:i==n-1,则:return dp[i][j];

意思就是说,每个点的最大值由下面两个点中比较大的那个加上它本身得到。。。。递归的代码就不贴了,毫无疑问,是超时的!!!原因很简单,太多的重复计算了,时间复杂度达到O(2^N)

下面,用一个数组存储这个中间的结果,就是动态规划的第一种方法:

c++版:

#include 
#include
using namespace std;
vector<vector<int> > n_v;
vector<vector<int> > result_v;
int n;

int rec(int i, int j){
    if (i==n-1){
        return n_v[i][j];
    }
    if(result_v[i+1][j]==-1){
        //没有录入这个
        result_v[i+1][j] = rec(i+1, j);
    }
    if(result_v[i+1][j+1]==-1){
        result_v[i+1][j+1] = rec(i+1, j+1);
    }
    int result =  result_v[i+1][j+1]>result_v[i+1][j]? result_v[i+1][j+1]:result_v[i+1][j];
    return result+n_v[i][j];
}
int main(){
    cin>>n;
    for (int i = 1; i <= n; ++i) {
        vector<int> e_v;
        vector<int> e_v1;
        int t;
        for (int j = 0; j < i; ++j) {
            cin>>t;
            e_v.push_back(t);
            e_v1.push_back(-1);
        }
        n_v.push_back(e_v);
        result_v.push_back(e_v1);
    }
    cout<0,0);
    return 0;
}

这里写图片描述

java版

import java.util.*;

public class Main {
    public static void show(ArrayList n_v, int n){
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n_v.get(i).size(); j++) {
                System.out.print(n_v.get(i).get(j)+" ");
            }
            System.out.println();
        }
    }

    public static ArrayList n_v = new ArrayList();
    public static ArrayList rec_v = new ArrayList();

    public static int n;

    public static int rec(int i, int j){
        //如果已经到最后一行了,就直接返回最后一行的值
        if (i==n-1){
            return (int)n_v.get(i).get(j);
        }
        if ((int)rec_v.get(i+1).get(j)==-1){
            rec_v.get(i+1).set(j, rec(i+1,j));
        }
        if((int)rec_v.get(i+1).get(j+1)==-1){
            rec_v.get(i+1).set(j+1, rec(i+1, j+1));
        }
        int sum1 = (int)rec_v.get(i+1).get(j);
        int sum2 = (int)rec_v.get(i+1).get(j+1);
        int result = sum1>sum2?sum1:sum2;
        return result + (int)n_v.get(i).get(j);
    }

    public static void init(){
        for (int i = 1; i <= 100; i++) {
            ArrayList r_i = new ArrayList();
            for (int j = 0; j < i; j++) {
                r_i.add(-1);
            }
            rec_v.add(r_i);
        }
    }
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        for (int i = 1; i <= n; i++) {
            ArrayList n_r = new ArrayList();
            for (int j = 0; j < i; j++) {
                n_r.add(s.nextInt());
            }
            n_v.add(n_r);
        }
        init();
//        show(rec_v, n);
        System.out.println(rec(0,0));
//        show(rec_v, n);
    }
}

这里写图片描述

第二种方法,从底向上推。

c++版

#include 
#include
using namespace std;
vector<vector<int> > n_v;
vector<vector<int> > result_v;
int n;

int main(){
    cin>>n;
    for (int i = 1; i <= n; ++i) {
        vector<int> e_v;
        vector<int> e_v1;
        int t;
        for (int j = 0; j < i; ++j) {
            cin>>t;
            e_v.push_back(t);
            e_v1.push_back(-1);
        }
        n_v.push_back(e_v);
        result_v.push_back(e_v1);
    }
    for (int l = 0; l < n; ++l) {
        result_v[n-1][l] = n_v[n-1][l];
    }
    for (int k = n-2; k >= 0 ; --k) {
        for (int i = 0; i < n_v[k].size(); ++i) {

            result_v[k][i] = max(result_v[k+1][i], result_v[k+1][i+1])+n_v[k][i];
        }
    }
//    show(result_v);
    cout<0][0];
    return 0;
}

这里写图片描述

java版

import java.util.*;

public class Main {

    public static ArrayList n_v = new ArrayList();
    public static ArrayList rec_v = new ArrayList();

    public static int n;
    public static void init(){
        for (int i = 1; i <= 100; i++) {
            ArrayList r_i = new ArrayList();
            for (int j = 0; j < i; j++) {
                r_i.add(-1);
            }
            rec_v.add(r_i);
        }
    }
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        for (int i = 1; i <= n; i++) {
            ArrayList n_r = new ArrayList();
            for (int j = 0; j < i; j++) {
                n_r.add(s.nextInt());
            }
            n_v.add(n_r);
        }
        init();
        for (int i = 0; i < n; i++) {
            rec_v.get(n-1).set(i, n_v.get(n-1).get(i));
        }
        for (int i = n-2; i >= 0 ; --i) {
            for (int j = 0; j < rec_v.get(i).size(); j++) {
                int num = (int)rec_v.get(i+1).get(j)>(int)rec_v.get(i+1).get(j+1)?(int)rec_v.get(i+1).get(j):(int)rec_v.get(i+1).get(j+1);
                rec_v.get(i).set(j, num+(int)n_v.get(i).get(j));
            }
        }
        System.out.println(rec_v.get(0).get(0));
    }
}

这里写图片描述

第三种,用一个一维数组代替上面的二维数组,因为从上面可以看出,当下一行用完之后,那行就没有存在的价值了

c++版

#include 
#include
using namespace std;
vector<vector<int> > n_v;
vector<int> result_single_v;
int n;

int main(){
    cin>>n;
    for (int i = 1; i <= n; ++i) {
        vector<int> e_v;
        int t;
        for (int j = 0; j < i; ++j) {
            cin>>t;
            e_v.push_back(t);
        }
        n_v.push_back(e_v);
    }

    for (int l = 0; l < n; ++l) {
        result_single_v.push_back(n_v[n-1][l]);
    }

    for (int k = n-2; k >= 0 ; --k) {
        for (int i = 0; i < n_v[k].size(); ++i) {
            result_single_v[i] = max(result_single_v[i], result_single_v[i+1])+n_v[k][i];
        }
    }

    cout<0];
    return 0;
}

这里写图片描述

java版

import java.util.*;

public class Main {

    public static ArrayList n_v = new ArrayList();
    public static ArrayList result_s_v = new ArrayList();
    public static int n;
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        for (int i = 1; i <= n; i++) {
            ArrayList n_r = new ArrayList();
            for (int j = 0; j < i; j++) {
                n_r.add(s.nextInt());
            }
            n_v.add(n_r);
        }

        for (int i = 0; i < n; i++) {
            result_s_v.add(i, (int)n_v.get(n-1).get(i));
        }
        for (int i = n-2; i >= 0 ; --i) {
            for (int j = 0; j < n_v.get(i).size(); j++) {
                int num = (int)result_s_v.get(j)>(int)result_s_v.get(j+1)?(int)result_s_v.get(j):(int)result_s_v.get(j+1);
                result_s_v.set(j, num+(int)n_v.get(i).get(j));
            }
        }
        System.out.println(result_s_v.get(0));
    }
}

这里写图片描述

你可能感兴趣的:(c++算法入门题目,poj)