• 【动态规划】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;
    }
    
  • 相关阅读:
    flex学习网站地址
    ASP.NET 开发人员应该知道的8个网站
    登入页面添加图片有重复页面出现怎么办
    一行代码是有两个??
    c# 枚举基础有[flags]和没有的的区别
    c#中[Flags] 枚举类型定义问题_百度知道
    写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的。
    Margin和Padding之间的区别
    对于top.ascx里面可以不可以放置css的文件进行一个讲解
    设计模式之--适配器模式(Adapter)
  • 原文地址:https://www.cnblogs.com/CSLaker/p/7398183.html
Copyright © 2020-2023  润新知