zoukankan
html css js c++ java
POJ 1163题 数字三角形问题(动态规划)
http://poj.org/problem?id=1163
1、普通递归
#include <iostream> #include <cstring> #include <cstdio> using namespace std; #define __max(a,b) (((a) > (b)) ? (a) : (b)) #define MAXNUM 101 int N; int aMax[MAXNUM][MAXNUM]; // aMax is memorandum int matrix[MAXNUM][MAXNUM]; int Max(int i, int j) { if (i == N) return matrix[i][j]; return __max( Max(i + 1, j), Max(i + 1, j + 1) ) + matrix[i][j]; } void Input(int _matrix[MAXNUM][MAXNUM]) // the 2nd dimension must be given! { for (int i = 1; i <= N; i++) { for (int j = 1; j <= i; j++) cin >> _matrix[i][j]; } } int main(void) { freopen("cin.txt", "r", stdin); cin >> N; memset(aMax, -1, sizeof(aMax)); // 0xff memset(matrix, -1, sizeof(matrix)); Input(matrix); cout << Max(1, 1) << endl; return 0; }
2、记忆式搜索 (动态规划)
int Max(int i, int j) { if (i == N) return matrix[i][j]; if (aMax[i + 1][j] == -1) aMax[i + 1][j] = Max(i + 1, j); if (aMax[i + 1][j + 1] == -1) aMax[i + 1][j + 1] = Max(i + 1, j + 1); return __max( aMax[i + 1][j], aMax[i + 1][j + 1] ) + matrix[i][j]; }
3、方法2的代码优化
int Max(int i, int j) { if (i == N) return matrix[i][j]; if (aMax[i][j] == -1) // 在普通递归的程序中加上 aMax[i][j] = __max( Max(i + 1, j), Max(i + 1, j + 1) ) + matrix[i][j]; // 改 return aMax[i][j]; // 加 }
查看全文
相关阅读:
easyui validatebox 验证集合
java.io.InvalidClassException: com.master.CurrentMessages; local class incompatible:
脏读 幻读 不可重复读
配置spring上下文
radio checked不起作用的原因
org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.
[JBPM3.2]TaskNode的signal属性详解
JBPM具体应用之decision节点的使用
ED/EP系列1《简单介绍》
利用内容观察者操作系统的联系人(查询,加入)
原文地址:https://www.cnblogs.com/jjtx/p/2533496.html
最新文章
mysql的查询缓存
python代码的书写要求
浅谈API设计
推荐十款来自极客标签的超棒前端特效[第十四期]
UVa 10491 Cows and Cars (概率&广义三门问题 )
python字符串操作总结
Android手机设置隐藏命令大全
DRP过后,感受知识间的通性
OK335xS I2C device registe hacking
Android App data write as file data with synchronous Demo
热门文章
Android shell command execute Demo
OK335xS UART device registe hacking
matrix-gui-browser-2.0 matrix-browser Qt QWebView hacking
Android Mokoid Open Source Project hacking
I.MX6 Linux Serial Baud Rate hacking
TI AM335x Linux MUX hacking
Texas Instruments matrix-gui-2.0 hacking -- execute_command.sh
Texas Instruments matrix-gui-2.0 hacking -- app_description.php
jQuery 清除div内容
动态设置easyui datagrid URL
Copyright © 2011-2022 走看看