前面的数据处理比较繁琐,一道跟LIS比较像的题目,但是只是类似,题意:给你一些立方体,按重量有小到达给出,每个立方体有六个面,并给出这六个面的颜色,现在让你堆立方体,每一个立方体必须比它下面的轻,而且两个接触的面必须颜色相同,
思路,每一个立方体其实是有六种状态的,只要记录每一个立方体的上下两个面的状态即可,还要记录此时的上表面下表面是原来的上下左右前后面的哪一面和 它是第几个立方体,统计好所有立方体的状态直接开始 寻找状态转移方程,跟LIS很像,所以很容易想到,最后记录路径 用递归输出即可,只需输出符合条件的其中一种就行了,一开始没看清楚,第二个案例 纠结了很久
#include<iostream> #include<cstdio> #include<list> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<stack> #include<map> #include<vector> #include<cmath> #include<memory.h> #include<set> //#define ll long long #define ll __int64 #define eps 1e-7 #define inf 0xfffffff //const ll INF = 1ll<<61; using namespace std; //vector<pair<int,int> > G; //typedef pair<int,int > P; //vector<pair<int,int> > ::iterator iter; // //map<ll,int >mp; //map<ll,int >::iterator p; //#define IN freopen("c:\\Users\\linzuojun\\desktop\\input.txt", "r", stdin) //#define OUT freopen("c:\\Users\\linzuojun\\desktop\\output.txt", "w", stdout) typedef struct Node { int top; int bottom; int id; int cnt; }; Node node[6000 + 5]; int dp[6000 + 5]; string color[12] = {"","front","back","left","right","top","bottom"}; int tmp[6]; int path[6000 + 5]; void clear() { memset(node,0,sizeof(node)); memset(dp,0,sizeof(dp)); memset(path,-1,sizeof(path)); } void Printf(int x) { if(x >= 0) { Printf(path[x]); cout<<node[x].id<<" "<<color[node[x].cnt]<<endl; } } int main() { int n; int Case = 0; while(cin>>n,n) { clear(); int tot = 0; for(int i=1;i<=n;i++) { for(int j=1;j<=6;j++) cin>>tmp[j]; for(int j=1;j<=6;j++) { node[tot].id = i; node[tot].cnt = j; if(j%2) { node[tot].top = tmp[j]; node[tot].bottom = tmp[j + 1]; } else { node[tot].top = tmp[j]; node[tot].bottom = tmp[j - 1]; } tot++; } } int ans = 0; int pos = 0; for(int i=0;i<tot;i++) { for(int j=i+1;j<tot;j++) { if(node[i].id < node[j].id && node[i].bottom == node[j].top && dp[j] < dp[i] + 1) { dp[j] = dp[i] + 1; path[j] = i; } if(ans < dp[j]) { ans = dp[j]; pos = j; } } }/* for(int i=1;i<tot;i++) if(ans < dp[i]) { ans = dp[i]; pos = i; }*/ ans++; if(Case) puts(""); cout<<"Case #"<<++Case<<endl; cout<<ans<<endl; Printf(pos); } } /* 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 Case #1 2 2 front 3 front Case #2 8 1 bottom 2 back 3 right 4 back 6 front 8 left 9 left 10 front */
原文:http://blog.csdn.net/yitiaodacaidog/article/details/19174351