UVa #437 The Tower of Babylon (例题9-2)
来源:互联网 发布:python idle 清屏 编辑:程序博客网 时间:2024/06/10 06:31
记忆化搜索,前两天看mit算法导论公开课里面Charles说这个memoization不算是动态规划,递推的才是。。
有机会的话回过头来写一些递推的
细节上,Rujia再一次给了这样的提示:如果数据范围太大但比较稀疏,则考虑给他们编码。在第五章的例题中(UVa 12096 The SetStack Computer(例题5-5),UVa 1592 Database(例题5-9))都有使用这种方法,是很常用也很重要的优化方法
Run Time: 0.016s
#define UVa "LT9-2.437.cpp"//The Tower of Babylonchar fileIn[30] = UVa, fileOut[30] = UVa;#include<cstring>#include<cstdio>#include<vector>using namespace std;struct Tower { int d[3][3]; Tower(int a, int b, int c){ d[0][0] = min(b, c); d[0][1] = max(b, c); d[0][2] = a; d[1][0] = min(a, c); d[1][1] = max(a, c); d[1][2] = b; d[2][0] = min(a, b); d[2][1] = max(a, b); d[2][2] = c; }};//Global Variables. Reset upon Each Case!const int maxn = 30 + 10;int n;vector<Tower> v;int dp[maxn][3];/////int solve(int idx, int k) { if(dp[idx][k] >= 0) return dp[idx][k]; dp[idx][k] = v[idx].d[k][2]; for(int i = 0; i < n; i ++) { for(int j = 0; j < 3; j ++) { if(v[i].d[j][0] < v[idx].d[k][0] && v[i].d[j][1] < v[idx].d[k][1]) { dp[idx][k] = max(dp[idx][k], solve(i,j) + v[idx].d[k][2]); } } } return dp[idx][k];}int main() { int kase = 0; while(scanf("%d", &n) && n) { v.clear(); int a, b, c; for(int i = 0; i < n; i ++) { scanf("%d%d%d", &a, &b, &c); v.push_back(Tower(a, b, c)); } memset(dp, -1, sizeof(dp)); int ans = -1; for(int i = 0; i < n; i ++) for(int j = 0; j < 3; j ++) ans = max(ans, solve(i,j)); printf("Case %d: maximum height = %d\n", ++kase, ans); } return 0;}
0 0
- UVa #437 The Tower of Babylon (例题9-2)
- UVa-437 例题9-2 巴比伦塔(The Tower of Babylon)
- 例题9-2 巴比伦塔(The Tower of Babylon, UVa 437)
- 例题9-2 UVA - 437 The Tower of Babylon 巴比伦塔(DGA-DP)(已更新)
- uva 437 The Tower of Babylon
- UVa 437 - The Tower of Babylon
- UVA 437 The Tower of Babylon
- UVa 437 - The Tower of Babylon LIS
- uva 437 - The Tower of Babylon
- UVa 437 - The Tower of Babylon
- UVa 437 - The Tower of Babylon
- UVa 437 - The Tower of Babylon
- UVa 437 - The Tower of Babylon
- UVA 437 The Tower of Babylon
- UVa:437 The Tower of Babylon
- UVA - 437 The Tower of Babylon
- Uva-437 The Tower of Babylon DP
- uva 437 The Tower of Babylon
- PHP添加redis扩展
- 求JAVA工程师!!!
- Volley
- HighCharts使用心得
- Debug Assertion Failed! File:f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\wincore.cpp Line:896
- UVa #437 The Tower of Babylon (例题9-2)
- android 无线调试
- ireport的使用
- C++之"类模板与STL编程"
- Majority Number II——算法练习
- 学Python这本书不错!
- 升级版音乐播放器
- android 内存相关
- wince地址映射过程