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]; // 加 }
查看全文
相关阅读:
ORA-01565 ORA-15025 ORA-27041
11g r2 vip启动过程
控制文件多路径复用ORA-00205错误
sqlmap 整合Meterpreter
Mataasploit 常见问题
AWVS的安装、破解与使用
hydra常见场景
php 学习笔记
python学习笔记
计算机网络基础,网络协议,常见状态码
原文地址:https://www.cnblogs.com/jjtx/p/2533496.html
最新文章
Blue-whale writeup
POJ 3254
HDU1372 bfs
【GO】go 第三方开源包
【GO】go 模块化开发
【GO】golang 的new 和 make
【GO】go 基础命令
【重构】重新组织函数
【Linux】压缩命令
【IDE】idea Mac 快捷键
热门文章
【Linux】Linux JSON 格式化输出
【Linux】Linux 三剑客 grep | awk | sed
【资源网站】优秀编程教程网站收集
Oracle无法生成awr报告 ORA-20200错误
mysql参数详解
ERROR 1682 (HY000): Native table 'performance_schema'.'session_variables' has the wrong structure
mysql 5.6安装
大页未使用
MySQL 5.7 三种免密码登录
redhat7.6 搭建ftp yum服务器
Copyright © 2011-2022 走看看