437-the Tower of Babylon
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_ problem&problem=378
Ideas:
For a (x,y,z) brick, it can be placed in 3 positions: (top two for ground, rear one for high)
X, Y, Z
X, Z, y
Y, Z, x
Record each posture and become a brick with a 3*n fixed posture.
Then build the map and find the longest road on the DAG.
Complete code:
/*0.015s*/#include <bits/stdc++.h> using namespace std;
int a[100][3], dp[100], N;
int DAG (int x) {if (dp[x]!=-1) return dp[x];
DP[X] = a[x][2];///high for (int y = 0; y < n; ++y) {if (y = = x) continue; if (A[y][0] < a[x][0] && a[y][1] < a[x][1] && Dp[x] < DAG (y) + a[x][2]) dp[x] = DAG
(y) + a[x][2];
return dp[x];
int main () {int cas = 0, maxh, I;
while (scanf ("%d", &n), n) {n + = n << 1;
Memset (DP,-1, sizeof (DP));
for (i = 0; i < n; i + + 3) {scanf ("%d%d%d", &a[i][0], &a[i][1], &a[i][2));
Sort (A[i], A[i] + 3);
A[i + 1][0] = a[i][0], a[i + 1][1] = a[i][2], a[i + 1][2] = a[i][1];
A[i + 2][0] = a[i][1], a[i + 2][1] = a[i][2], a[i + 2][2] = a[i][0];
} maxh = 0; for (i = 0; i < n;
++i) if (Maxh < DAG (i)) maxh = Dp[i];
printf ("Case%d:maximum height =%d\n", ++cas, Maxh);
return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/