uva 103 - Stacking Boxes

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=39

 

题目读了好久总算看懂了,具有n维属性的箱子a和b,如果a fits b 仅当存在a的属性排列,使与b对应

的属性满足q(a)<q(b),算法中定义二维数组,标记a是否fits b,然后深搜,当然得先把每个箱子的属性排序。

 

  
    
1 #include < stdio.h >
2 #include < string .h >
3 #include < stdlib.h >
4
5   struct Box {
6 int mt[ 15 ];
7 }bx[ 40 ];
8   bool flg[ 40 ][ 40 ], f0[ 40 ];
9   int rst[ 40 ], tp[ 40 ];
10 int nb, mt;
11 int max;
12
13 int comp( const void * a, const void * b)
14 {
15 return * (( int * )a) - * (( int * )b);
16 }
17
18 bool fits( int i, int j)
19 {
20 for ( int k = 0 ; k < mt; k ++ )
21 if (bx[i].mt[k] >= bx[j].mt[k]) return false ;
22 return true ;
23 }
24
25 void DFS( int i, int t)
26 {
27 int j;
28 for (j = 1 ; j <= nb; j ++ ) {
29 if (flg[i][j]) {
30 tp[t + 1 ] = j;
31 DFS(j, t + 1 );
32 }
33 }
34 if (max < t) {
35 for (j = 1 ; j <= t; j ++ )
36 rst[j] = tp[j];
37 max = t;
38 }
39 }
40
41 int main()
42 {
43 int i, j;
44 while (scanf( " %d%d " , & nb, & mt) != EOF) {
45 for (i = 1 ; i <= nb; i ++ ) {
46 for (j = 0 ; j < mt; j ++ )
47 scanf( " %d " , & bx[i].mt[j]);
48 qsort(bx[i].mt, mt, sizeof (bx[i].mt[ 0 ]), comp);
49 }
50 memset(flg, 0 , sizeof (flg));
51 for (i = 1 ; i <= nb; i ++ ) {
52 for (j = 1 ; j <= nb; j ++ ) {
53 if (i == j) continue ;
54 if (fits(i, j)) {
55 flg[i][j] = 1 ;
56 }
57 }
58 }
59 max = 0 ;
60
61 for (i = 1 ; i <= nb; i ++ ) {
62 tp[ 1 ] = i;
63 memset(f0, 0 , sizeof (f0));
64 f0[i] = 1 ;
65 DFS(i, 1 );
66 }
67 // if a fits b , b fits c then a fits c
68 // 因此rst[]保存的就是期望的顺序
69 printf( " %d\n " , max);
70
71 for (i = 1 ; i <= max; i ++ ) {
72 printf( " %d " , rst[i]);
73 if (i != max) printf( " " );
74 }
75 printf( " \n " );
76 }
77 return 0 ;
78 }
79

 

你可能感兴趣的:(stack)