当前位置: 代码迷 >> 综合 >> Super Jumping! Jumping! Jumping! 动态 规划
  详细解决方案

Super Jumping! Jumping! Jumping! 动态 规划

热度:75   发布时间:2023-12-12 14:29:13.0

Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.

The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.
Input
Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each case, print the maximum according to rules, and one line one case.

Sample Input

3 1 3 2
4 1 2 3 4
4 3 3 2 1
0

Sample Output

4
10
3

这个题呢 动态规划 的 过程就是 如何 求最优解 就是 求解 dp【i】 的最大值, dp【i】 表示 跳到 第 i 个 数 mapp【i】 时 的 最大的 值 刚开始 只有 一个数的时候 I= 1 ,此时 循环 求出 dp【1】的 值 ,后面 当 i 的 值 一次增大 之后 dp【i】 的 值 就要用到 之前 所的 得到 的 dp【k】(k < i) 来 求解 最优解 啦 ,这个 过程 就是 “动态”的 意思拉, 动态规划 就是 从 前一个 状态 来 得出 后一个 状态 ,应为 前一个 状态 与后一个 状态 都 是 处于 不断的 变化 中 应为 我们 要 编程序 解决问题 要的出 一个 确定的 答案 , 即 就是 从 有限 的数据 的 动态 变化中 得出一个 最优解 。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;int main()
{int n;while(scanf("%d", &n) && n){int m, maxx , flag;int dp[1010], mapp[1010];// dp【i】 记录 当前的 最大 解 , mapp【i】 储存 第i个数memset(dp, 0, sizeof(dp));memset(mapp, 0, sizeof(mapp));for(int i = 0; i < n; i++){maxx = -1;scanf("%d", &m);mapp[i] = m;flag = false;for(int j = 0; j < i; j++)// 此处就是 前 状态 => 后 一个 状态{if(mapp[j] < m){maxx = max(maxx, dp[j] + m);flag = true;}}if(flag)dp[i] = maxx;elsedp[i] = mapp[i];}int ans = -1;for(int i = 0; i < n; i++)ans = max(ans, dp[i]);printf("%d\n", ans);}return 0;
}
  相关解决方案