动态规划示例四
来源:互联网 发布:无损鉴别软件 编辑:程序博客网 时间:2024/06/04 18:10
捡苹果问题,具体的问题描述请见动态规划:从新手到专家中的中级部分。
在矩阵中,分布着一些苹果。在矩阵中,一个人只能向下或者向右,请问它所能捡到的最大个数的苹果是多少。以下矩阵初始化中,虽然是只能0,1个数,但其实其他任意整数也是可以的。
appleNumbers[rowIndex][colIndex] = appleMatrix[rowIndex][colIndex]+max(appleNumbers[rowIndex][colIndex-1] ,appleNumbers[rowIndex-1][colIndex]);以下是具体代码
#include <stdio.h>#define NumRow 4#define NumCol 5int max(int a,int b){return (a > b) ? a : b;}int PickApple(int appleMatrix[NumRow][NumCol]){int rowIndex, colIndex;int appleNumbers[NumRow][NumCol];//初始化矩阵appleNumbers的第0行和第0列appleNumbers[0][0] = appleMatrix[0][0];for (rowIndex=1;rowIndex<NumRow;rowIndex++){appleNumbers[rowIndex][0] = appleNumbers[rowIndex-1][0]+appleMatrix[rowIndex][0];}for (colIndex=1;colIndex<NumCol;colIndex++){appleNumbers[0][colIndex] = appleNumbers[0][colIndex-1]+appleMatrix[0][colIndex];}//开始捡苹果for (rowIndex=1;rowIndex<NumRow;rowIndex++){for (colIndex=1;colIndex<NumCol;colIndex++){appleNumbers[rowIndex][colIndex] = appleMatrix[rowIndex][colIndex]+max(appleNumbers[rowIndex][colIndex-1] ,appleNumbers[rowIndex-1][colIndex]);}}printf("In the matrix, At most pick %d apples. \n", appleNumbers[rowIndex-1][colIndex-1]);return 0;}int main(){int appleMatrix[NumRow][NumCol] = { {1,0,1,1,0}, {0,1,1,0,1}, {1,0,1,0,0}, {0,0,1,1,1}};PickApple(appleMatrix);getchar();return 0;}
0 0
- 动态规划示例四
- 动态规划示例
- 动态规划示例二
- 动态规划示例三
- 动态规划示例五
- 动态规划小示例
- (四)动态规划
- 动态规划之四
- 四、算法_动态规划
- 浅谈动态规划(四)
- 动态规划入门(四)
- [LeetCode63]Unique pathII动态规划解题示例
- 简单动态规划集(四)
- 选球博奕与动态规划(四)
- 笨蛋难题四(动态规划)
- tyvj 4541 四维动态规划
- 基础算法(四)---动态规划算法
- 第十四周 动态规划(四)
- 水处理过滤器:袋式过滤器在水处理领域中的技术特点体现
- js刷新页面方法大全
- 语音识别的一些概念
- zepto中animate的用法
- [水]ZOJ2812
- 动态规划示例四
- 如何清理监听日志
- java编程1
- oracle中所有关于时间日期的问题总结
- java6 原生webService注解应用
- Lua 基础
- linux下安装和运行wireshark
- Apache: No space left on device: Couldn’t create accept lock
- java编程2