1. 程式人生 > 其它 >做題記錄 Luogu UVA437

做題記錄 Luogu UVA437

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;
}