zoukankan      html  css  js  c++  java
  • 【动态规划】POJ-3176

    一、题目

    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 standard bowling-pin-like triangle like this:

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

    Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame.

    Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

    Input

    Line 1: A single integer, N

    Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

    Output

    Line 1: The largest sum achievable using the traversal rules

    Sample Input

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

    Sample Output

    30
    Hint

    Explanation of the sample:

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

    The highest score is achievable by traversing the cows as shown above.

    二、思路&心得

    ​简单的动态规划问题,从底向上依次扫描就行了,可以用直接用DP,也可以用记忆化搜索。

    三、代码

    1.DP:

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int MAX_N = 355;
    
    int N;
    int dp[MAX_N][MAX_N];
    
    void solve() {
    	for (int i = 0; i < N; i ++) {
    		for (int j = 0; j <= i; j ++) {
    			scanf("%d", &dp[i][j]);			
    		}
    	}
    	for (int i = N - 2; i >=0; i --) {
    		for (int j = 0; j <= i; j ++) {
    			dp[i][j] += max(dp[i + 1][j], dp[i + 1][j + 1]);
    		}
    	}
    	printf("%d
    ", dp[0][0]);
    }
    
    int main() {
    	while (~scanf("%d", &N)) {
    		solve();
    	}
    	return 0;
    }
    

    2.记忆化搜索:

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int MAX_N = 355;
    
    int N;
    int dp[MAX_N][MAX_N];
    int visit[MAX_N][MAX_N];
    
    int score(int x, int y) {
    	if (visit[x][y] == 1) return dp[x][y];
    	visit[x][y] = 1;
    	if (x == N - 1) return dp[x][y];
    	return dp[x][y] += max(score(x + 1, y), score(x + 1, y + 1));
    }
    
    void solve() {
    	for (int i = 0; i < N; i ++) {
    		for (int j = 0; j <= i; j ++) {
    			scanf("%d", &dp[i][j]);			
    		}
    	}
    	printf("%d
    ", score(0, 0));
    }
    
    int main() {
    	while (~scanf("%d", &N)) {
    		solve();
    	}
    	return 0;
    }
    
  • 相关阅读:
    谈谈对 ”框架“ 这个概念的理解,以及它和库的区别
    npm 安装或更新模块失败的解决办法
    vs 2017/2015/2013 如何定位C++内存泄漏
    django 在python 3中提示 无法找到 MySQLDB
    合并表中数据
    pymysql
    mysql-错误备查
    tensorflow-gpu 使用的常见错误
    Ubuntu 安装 tensorflow-gpu + keras
    mysql 查看表结构方法
  • 原文地址:https://www.cnblogs.com/CSLaker/p/7398183.html
Copyright © 2011-2022 走看看