现在的位置: 首页 > 综合 > 正文

UVA 10051 – Tower of Cubes(dp + DAG最长路径) UVA 10051 – Tower of Cubes(dp + DAG最长路径)

2017年11月02日 ⁄ 综合 ⁄ 共 3258字 ⁄ 字号 评论关闭

UVA 10051 - Tower of Cubes(dp + DAG最长路径)

分类: dp 58人阅读 评论(0) 收藏 举报

目录(?)[+]


  Problem A: Tower of Cubes 


In this problem you are given N colorful cubes each having a distinct weight. Each face of a cube is colored with
one color. Your job is to build a tower using the cubes you have subject to the following restrictions:

  • Never put a heavier cube on a lighter one.
  • The bottom face of every cube (except the bottom cube, which is lying on the floor) must have the same color as the top face of the cube below it.
  • Construct the tallest tower possible.

Input 

The input may contain multiple test cases. The first line of each test case contains an integer N ( $1 \le N \le 500$)
indicating the number of cubes you are given. The 
i­th ( $1 \le i \leN$)
of the next 
N lines contains the description of the i­th
cube. A cube is described by giving the colors of its faces in the following order: front, back, left, right, top and bottom face. For your convenience colors are identified by integers in the range 1 to 100. You may assume that cubes are given in the increasing
order of their weights, that is, cube 1 is the lightest and cube 
N is the heaviest.

The input terminates with a value 0 for N.

Output 

For each test case in the input first print the test case number on a separate line as shown in the sample output. On the next line print the number of cubes in the tallest tower you have built. From the next line describe the cubes in your tower from top to
bottom with one description per line. Each description contains an integer (giving the serial number of this cube in the input) followed by a single white­space character and then the identification string (front, back, left, right, top or bottom) of the top
face of the cube in the tower. Note that there may be multiple solutions and any one of them is acceptable.

Print a blank line between two successive test cases.

Sample
Input
 

3
1 2 2 2 1 2
3 3 3 3 3 3
3 2 1 1 1 1
10
1 5 10 3 6 5
2 6 7 3 6 9
5 7 3 2 1 9
1 3 3 5 8 10
6 6 2 2 4 4
1 2 3 4 5 6
10 9 8 7 6 5
6 1 2 3 4 7
1 2 3 3 2 1
3 2 1 1 2 3
0

Sample
Output
 

Case #1
2
2 front
3 front
 
Case #2
8
1 bottom
2 back
3 right
4 left
6 top
8 front
9 front
10 top

题意:给定n个立方体,每个面有一个颜色,每个有质量,质量输入先后由轻到重,颜色由数字表示,现在叠放,每个立方体的底面要和下一个立方体的顶面一样颜色,并且下面一个要比上面一个重,求最多叠放几个。

思路:DAG最长路径问题,直接用记忆化搜索过,在搜索过程中由于后一个肯定比前一个重,所以从1到n搜索即可。

代码:

  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. const char name[6][20] = {"front""back""left""right""top""bottom"};  
  5. int n, ans, dp[505][6];  
  6. struct Box {  
  7.     int v[6];  
  8. } box[505];  
  9.   
  10. struct Out {  
  11.     int num;  
  12.     int name;  
  13. } out[505], save[505];  
  14. void dfs(int now, int top, int num) {  
  15.     for (int i = now + 1; i <= n; i ++) {  
  16.     for (int j = 0; j < 6; j ++) {  
  17.         if (now == 0 || (box[i].v[j] == top && dp[i][j] < num + 1)) {  
  18.         dp[i][j] = num + 1;  
  19.         if (j % 2) {  
  20.             save[num].num = i;  
  21.             save[num].name = j;  
  22.             dfs(i, box[i].v[j - 1], num + 1);  
  23.         }  
  24.         else {  
  25.             save[num].num = i;  
  26.             save[num].name = j;  
  27.             dfs(i, box[i].v[j + 1], num + 1);  
  28.         }  
  29.         }  
  30.     }  
  31.     }  
  32.     if (ans < num) {  
  33.     ans = num;  
  34.     for (int i = 0; i < num; i ++)  
  35.         out[i] = save[i];  
  36.     }  
  37. }  
  38. int main() {  
  39.     int bo = 0;  
  40.     while (~scanf("%d", &n) && n) {  
  41.     memset(dp, 0, sizeof(dp));  
  42.     ans = 0;  
  43.     for (int i = 1; i <= n; i ++) {  
  44.         for (int j = 0; j < 6; j ++) {  
  45.         scanf("%d", &box[i].v[j]);  
  46.         }  
  47.     }  
  48.     dfs(0, 0, 0);  
  49.     if (bo ++) printf("\n");  
  50.     printf("Case #%d\n", bo);  
  51.     printf("%d\n", ans);  
  52.     for (int i = 0; i < ans; i ++)  
  53.         printf("%d %s\n", out[i].num, name[out[i].name]);  
  54.     }  
  55.     return 0;  
  56. }  

抱歉!评论已关闭.