Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef struct{
  5.  
  6. int x;
  7. int y;
  8. int z;
  9. }box;
  10.  
  11. vector<box> g;
  12.  
  13. int dp[13][(1<<13)+5][3];
  14.  
  15. void reseta(int lim){
  16.  
  17. for(int i=0;i<13;i++){
  18. for(int j=0;j<(1<<13)+5;j++){
  19. for(int k=0;k<3;k++){
  20. dp[i][j][k] = -1;
  21. }
  22. }
  23. }
  24. }
  25.  
  26. bool isvalid(int u,int l1,int v,int l2){
  27.  
  28. u--;
  29. v--;
  30.  
  31. if(u == -1) return true;
  32.  
  33. int a,b;
  34. int c,d;
  35.  
  36. if(l1 == 0){
  37. a = g[u].x;
  38. b = g[u].y;
  39. }
  40. else if(l1 == 1){
  41. a = g[u].x;
  42. b = g[u].z;
  43. }
  44. else{
  45. a = g[u].y;
  46. b = g[u].z;
  47. }
  48.  
  49. if(l2 == 0){
  50. c = g[v].x;
  51. d = g[v].y;
  52. }
  53. else if(l2 == 1){
  54. c = g[v].x;
  55. d = g[v].z;
  56. }
  57. else{
  58. c = g[v].y;
  59. d = g[v].z;
  60. }
  61.  
  62. if(c <= a && d <= b) return true;
  63. if(c <= b && d <= a) return true;
  64.  
  65. return false;
  66. }
  67.  
  68. int solve(int last,int bmask,int pos,int n){
  69.  
  70. if(bmask == 0) return 0;
  71. if(dp[last][bmask][pos] != -1) return dp[last][bmask][pos];
  72.  
  73. int maxi = 0;
  74.  
  75. for(int i=1;i<=n;i++){
  76. if((bmask&(1<<i))){
  77. for(int j=0;j<3;j++){
  78. if(isvalid(last,pos,i,j)){
  79. bmask = bmask^(1<<i);
  80. maxi = max(maxi,1+solve(i,bmask,j,n));
  81. bmask = bmask|(1<<i);
  82. }
  83. }
  84. }
  85. }
  86.  
  87. dp[last][bmask][pos] = maxi;
  88. return (maxi);
  89. }
  90.  
  91. int main(){
  92.  
  93. ios::sync_with_stdio(0);
  94. cin.tie(0);
  95. int n;
  96. int xx=1;
  97. cin >> n;
  98. while(n != 0){
  99.  
  100. box aux;
  101. reseta(n+1);
  102. g.clear();
  103. for(int i=0;i<n;i++){
  104. cin >> aux.x >> aux.y >> aux.z;
  105. g.push_back(aux);
  106. }
  107. int bmask = 0;
  108. for(int i=1;i<=n;i++){
  109. bmask = bmask|(1<<i);
  110. }
  111. cout << "Case " << xx++ << ": ";
  112. cout << solve(0,bmask,0,n) << endl;
  113. cin >> n;
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement