Luogu UVA437 巴比伦塔 The Tower of Babylon
记忆化搜索,注意需要将每个 cube 扩展成 3 个,这样才能考虑到所有情况(有环形 dp 或者分层图那味了)
还有就是重载运算符减少码量的小技巧 get
#include<bits/stdc++.h> using namespace std; struct cube { int x, y, z; }; bool operator < (cube a, cube b) { return ((a.x < b.x && a.y < b.y) || (a.y < b.x && a.x < b.y)); } cube c[105]; int n, tot, cnt, f[105]; int dp(int x) { int &ans = f[x]; if(ans) { return ans; } ans = c[x].z; for(int i = 1; i <= tot; i++) { if(c[i] < c[x]) { ans = max(ans, dp(i) + c[x].z); } } return ans; } int main() { ios::sync_with_stdio(false); while(cin >> n && n) { int x, y, z; cnt++; tot = 0; memset(f, 0, sizeof(f)); for(int i = 1; i <= n; i++) { cin >> x >> y >> z; c[++tot] = (cube){x, y, z}; c[++tot] = (cube){y, z, x}; c[++tot] = (cube){x, z, y}; } int ret = 0; for(int i = 1; i <= tot; i++) { ret = max(ret, dp(i)); } cout << "Case " << cnt << ": maximum height = " << ret << endl; } return 0; }