zoukankan      html  css  js  c++  java
  • UVA 103 Stacking Boxes

      终于完成了啊,这可是我自己独立做的第一道DP题!激动ing……这题在白书里是DAG上的DP,可是我看不懂,比如怎么建图我就不会,所以代码都是自己想的。图我不会建,只好动下脑子,刚开始是想用二维数组保存数据的,后来改成了结构体。整个程序我还算满意。

    View Code
     1 #include <iostream>
    2 #include <algorithm>
    3 using namespace std;
    4 const int MAXM = 32;
    5 const int MAXN = 12;
    6 int m,n,dp[MAXM];
    7 typedef struct Box
    8 {
    9 int num;
    10 int arr[MAXN];
    11 }Box;
    12 Box box[MAXM];
    13 bool cmp(Box a,Box b)
    14 {
    15 for(int i = 0; i < 9; i++)
    16 if(a.arr[i] != b.arr[i])
    17 return a.arr[i] < b.arr[i];
    18 return a.arr[9]<b.arr[9];
    19 }
    20 int check(Box a,Box b) //a能放到b里
    21 {
    22 int i,f = 1;
    23 for(i = 0; i < n; i++)
    24 if(a.arr[i] >= b.arr[i])
    25 {f = 0;break;}
    26 return f;
    27 }
    28 void print(int j)
    29 {
    30 if(dp[j] == 1)
    31 cout<<box[j].num<<endl;
    32 for(int i=0;i<m;i++)
    33 if(dp[i] == dp[j]-1 && check(box[j],box[i]))
    34 {
    35 cout<<box[j].num<<" ";
    36 print(i);
    37 break;
    38 }
    39 }
    40 int main()
    41 {
    42 int i,j,tmax,ans,f;
    43 while(cin>>m>>n)
    44 {
    45 for(i = 0; i < m; i++)
    46 {
    47 box[i].num = i + 1;
    48 for(j = 0; j < n; j++)
    49 cin>>box[i].arr[j];
    50 }
    51 for(i = 0; i < m; i++)
    52 sort(box[i].arr,box[i].arr + n);
    53 sort(box,box+m,cmp);
    54 for(i = 0; i < m; i++)
    55 dp[i] = 1;
    56 for(i = m-2; i >= 0; i--)
    57 {
    58 tmax = 1;
    59 for(j = m-1; j >= i+1; j--)
    60 if(check(box[i],box[j]))
    61 if(dp[j]+1 > tmax)
    62 {
    63 tmax = dp[j]+1;
    64 dp[i] = tmax;
    65 }
    66 }
    67 ans = dp[0]; f = 0;
    68 for(i = 1; i < m; i++)
    69 if(dp[i] > ans)
    70 {ans = dp[i]; f = i;}
    71 cout<<ans<<endl;
    72 print(f);
    73 }
    74 return 0;
    75 }
  • 相关阅读:
    Building a flexiable renderer
    Indirect Illumination in mental ray
    我的心情
    Cellular Automata
    Subsurface Scattering in mental ray
    Shader Types in mental ray
    BSP Traversal
    我的渲染器终于达到了MR的速度
    How to handle displacement and motion blur
    说明
  • 原文地址:https://www.cnblogs.com/lzxskjo/p/2424865.html
Copyright © 2011-2022 走看看