Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1562204
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2006-08-09 16:30:40

零件契合,两个零件形成一个4*4*H的长方体。
给定30000个零件,找出能形成的SUM(H)的最大值。

对于每个4*4的矩形M,先减去共有的底高,则每一位都是0-2
对于处理后的矩阵M1,与之对应的契合矩阵(每位也是0-2)是唯一的M2
M1和M2可以分别旋转三次,所得的4个矩阵都可与对方4个矩阵契合,每四个矩阵看做一个等价类
遍历30000个原始矩阵,用count数组记录其处理后矩阵等价类的出现次数,同时记录下处理前的底高。这样的等价类最多有60000个
遍历count数组,每两个等价类中分别依次取底高最高值作为可以契合的一对零件,直到一方取完

Ch数组记录等价类是否出现,每一维表示4*4数组的一行,由于每一位是0~2的三进制,所以每一维是3^4 = 81,其值为该等价类的processSeq
base数组记录每个零件的底高
processSeq记录数组处理序号,为0表示其等价类未处理过
mat数组用于计算契合数组

count[]数组每一行记录一个等价类,各位置含义:
0:为1表示该等价类出现过
1~8:表示底高为1~8的零件出现次数
9:该等价类与其契合等价类契合后的高度


  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <stdlib.h>

  4. #define MAX 30000
  5. #define P 81
  6. using namespace std;

  7. static int Ch[P][P][P][P];
  8. static int count[2 * MAX + 1][10];
  9. static int base[MAX];
  10. static int processSeq;
  11. static int mat[4][4];

  12. int processDelta(int delta[][4]) {
  13.     int i = 27 * delta[0][0] + 9 * delta[0][1] + 3 * delta[0][2] + delta[0][3];
  14.     int j = 27 * delta[1][0] + 9 * delta[1][1] + 3 * delta[1][2] + delta[1][3];
  15.     int k = 27 * delta[2][0] + 9 * delta[2][1] + 3 * delta[2][2] + delta[2][3];
  16.     int l = 27 * delta[3][0] + 9 * delta[3][1] + 3 * delta[3][2] + delta[3][3];

  17.     if (Ch[i][j][k][l] == 0) {
  18.         Ch[i][j][k][l] = processSeq;
  19.         return 0;
  20.     }
  21.     return Ch[i][j][k][l];
  22. }

  23. void rotate(int delta[][4]) {
  24.     int rot[4][4];
  25.     for (int i = 0; i < 4; i++) {
  26.         for (int j = 0; j < 4; j++) {
  27.             rot[i][j] = delta[3 - j][i];
  28.         }
  29.     }

  30.     for (int i = 0; i < 4; i++) {
  31.         for (int j = 0; j < 4; j++) {
  32.             delta[i][j] = rot[i][j];
  33.         }
  34.     }

  35.     return;
  36. }

  37. void match(int delta[][4], int max) {
  38.     for (int i = 0; i < 4; i++) {
  39.         for (int j = 0; j < 4; j++) {
  40.             mat[i][j] = max - delta[i][3 - j];
  41.         }
  42.     }
  43.     return;
  44. }

  45. int processCount() {
  46.     int result = 0;

  47.     for (int i = 1; i < MAX * 2; i += 2) {
  48.         if (count[i][0] == 1 && count[i + 1][0] == 1) {
  49.             int oddSum = 0, evenSum = 0;
  50.             for (int j = 1; j <= 8; j++) {
  51.                 oddSum += count[i][j];
  52.                 evenSum += count[i + 1][j];
  53.             }

  54.             int gap, idx, selection;
  55.             if (oddSum >= evenSum) {
  56.                 selection = evenSum;
  57.                 gap = oddSum - evenSum;
  58.                 idx = i;
  59.             } else {
  60.                 selection = oddSum;
  61.                 gap = evenSum - oddSum;
  62.                 idx = i + 1;
  63.             }
  64.             for (int j = 1; j <= 8; j++) {
  65.                 if (count[idx][j] > 0) {
  66.                     if (count[idx][j] > gap) {
  67.                         count[idx][j] -= gap;
  68.                         gap = 0;
  69.                     } else {
  70.                         gap -= count[idx][j];
  71.                         count[idx][j] = 0;
  72.                     }
  73.                     if (gap == 0) break;
  74.                 }
  75.             }

  76.             for (int j = 1; j <= 8; j++) {
  77.                 result += count[i][j] * j;
  78.                 result += count[i + 1][j] * j;
  79.             }

  80.             result += selection * count[i][9];
  81.         }
  82.     }

  83.     return result;
  84. }

  85. int test(int module[][4][4]) {
  86.     for (int a = 0; a < P; a++) {
  87.         for (int b = 0; b < P; b++) {
  88.             for (int c = 0; c < P; c++) {
  89.                 for (int d = 0; d < P; d++) {
  90.                     Ch[a][b][c][d] = 0;
  91.                 }
  92.             }
  93.         }
  94.     }

  95.     for (int i = 0; i < 2 * MAX + 1; i++) {
  96.         for (int j = 0; j < 10; j++) {
  97.             count[i][j] = 0;
  98.         }
  99.     }

  100.     processSeq = 1;
  101.     for (int i = 0; i < MAX; i++) {
  102.         int iBase = 8;
  103.         for (int j = 0; j < 4; j++) {
  104.             for (int k = 0; k < 4; k++) {
  105.                 if (module[i][j][k] < iBase)
  106.                     iBase = module[i][j][k];
  107.             }
  108.         }
  109.         base[i] = iBase;

  110.         int delta[4][4];
  111.         for (int j = 0; j < 4; j++) {
  112.             for (int k = 0; k < 4; k++) {
  113.                 delta[j][k] = module[i][j][k] - iBase;
  114.             }
  115.         }

  116.         int processed = processDelta(delta);
  117.         if (processed == 0) {
  118.             count[processSeq][0] = 1;
  119.             count[processSeq][base[i]]++;

  120.             for (int j = 0; j < 3; j++) {
  121.                 rotate(delta);
  122.                 processDelta(delta);
  123.             }

  124.             int max = 0;
  125.             for (int j = 0; j < 4; j++) {
  126.                 for (int k = 0; k < 4; k++) {
  127.                     if (delta[j][k] > max)
  128.                         max = delta[j][k];
  129.                 }
  130.             }
  131.             count[processSeq][9] = max;

  132.             processSeq++;
  133.             match(delta, max);
  134.             processDelta(mat);
  135.             for (int j = 0; j < 3; j++) {
  136.                 rotate(mat);
  137.                 processDelta(mat);
  138.             }

  139.             processSeq++;
  140.         } else {
  141.             count[processed][0] = 1;
  142.             count[processed][base[i]]++;
  143.         }
  144.     }

  145.     return processCount();
  146. }

  147. int main(void)
  148. {
  149.     static int module[MAX][4][4];//30000

  150.     srand(3); // 3 will be changed

  151.     for (int tc = 1; tc <= 10; tc++)
  152.     {
  153.         for (int c = 0; c < MAX; c++)
  154.         {
  155.             int base = 1 + (rand() % 6);//1~6
  156.             for (int y = 0; y < 4; y++)
  157.             {
  158.                 for (int x = 0; x < 4; x++)
  159.                 {
  160.                     module[c][y][x] = base + (rand() % 3);
  161.                 }
  162.             }
  163.         }
  164.         cout << "#" << tc << " " << test(module) << endl;
  165.     }

  166.     return 0;
  167. }




  1. #include <iostream>
  2. #include <stdlib.h>

  3. #define MAX 30000
  4. #define P 81
  5. using namespace std;

  6. static int Ch[P][P][P][P];
  7. static int count[MAX + 1][10];
  8. static int base[MAX];
  9. static int processSeq;
  10. static int mat[4][4];

  11. int processDelta(int delta[][4]) {
  12.    int i = 27 * delta[0][0] + 9 * delta[0][1] + 3 * delta[0][2] + delta[0][3];
  13.    int j = 27 * delta[1][0] + 9 * delta[1][1] + 3 * delta[1][2] + delta[1][3];
  14.    int k = 27 * delta[2][0] + 9 * delta[2][1] + 3 * delta[2][2] + delta[2][3];
  15.    int l = 27 * delta[3][0] + 9 * delta[3][1] + 3 * delta[3][2] + delta[3][3];
  16.    
  17.    if (Ch[i][j][k][l] == 0) {
  18.       Ch[i][j][k][l] = processSeq;
  19.       return 0;
  20.    }
  21.    return Ch[i][j][k][l];
  22. }

  23. void rotate(int delta[][4]) {
  24.    int rot[4][4];
  25.    for (int i = 0; i < 4; i++) {
  26.       for (int j = 0; j < 4; j++) {
  27.          rot[i][j] = delta[3 - j][i];
  28.       }
  29.    }

  30.    for (int i = 0; i < 4; i++) {
  31.       for (int j = 0; j < 4; j++) {
  32.        delta[i][j] = rot[i][j];
  33.       }
  34.    }
  35.    
  36.    return;
  37. }

  38. void match(int delta[][4], int max) {
  39.    for (int i = 0; i < 4; i++) {
  40.       for (int j = 0; j < 4; j++) {
  41.          mat[i][j] = max - delta[i][3 - j];
  42.       }
  43.    }
  44.    return;
  45. }

  46. int processCount() {
  47.    int result = 0;

  48.    for (int i = 1; i < MAX; i += 2) {
  49.       if (count[i][0] == 1 && count[i + 1][0] == 1) {
  50.          int oddSum = 0, evenSum = 0;
  51.          for (int j = 1; j <= 8; j++) {
  52.             oddSum += count[i][j];
  53.             evenSum += count[i + 1][j];
  54.          }

  55.          int gap, idx, selection;
  56.          if (oddSum >= evenSum) {
  57.             selection = evenSum;
  58.             gap = oddSum - evenSum;
  59.             idx = i;
  60.          } else {
  61.             selection = oddSum;
  62.             gap = evenSum - oddSum;
  63.             idx = i + 1;
  64.          }
  65.          for (int j = 1; j <= 8; j++) {
  66.             if (count[idx][j] > 0) {
  67.                if (count[idx][j] > gap) {
  68.                   count[idx][j] -= gap;
  69.                   gap = 0;
  70.                } else {
  71.                   gap -= count[idx][j];
  72.                   count[idx][j] = 0;
  73.                }
  74.                if (gap == 0) break;
  75.             }
  76.          }

  77.          for (int j = 1; j <= 8; j++) {
  78.             result += count[i][j] * j;
  79.             result += count[i + 1][j] * j;
  80.          }

  81.          result += selection * count[i][9];
  82.       }
  83.    }

  84.  return result;
  85. }

  86. int test2(int module[][4][4]) {
  87.    for (int a = 0; a < P; a++) {
  88.       for (int b = 0; b < P; b++) {
  89.          for (int c = 0; c < P; c++) {
  90.             for (int d = 0; d < P; d++) {
  91.                Ch[a][b][c][d] = 0;
  92.             }
  93.          }
  94.       }
  95.    }
  96.    
  97.    for (int i = 0; i < MAX + 1; i++) {
  98.       for (int j = 0; j < 10; j++) {
  99.          count[i][j] = 0;
  100.       }
  101.    }
  102.    
  103.    processSeq = 1;
  104.    for (int i = 0; i < MAX; i++) {
  105.       int iBase = 8;
  106.       for (int j = 0; j < 4; j++) {
  107.          for (int k = 0; k < 4; k++) {
  108.             if (module[i][j][k] < iBase)
  109.                iBase = module[i][j][k];
  110.          }
  111.       }
  112.       base[i] = iBase;
  113.       
  114.       int delta[4][4];
  115.       for (int j = 0; j < 4; j++) {
  116.          for (int k = 0; k < 4; k++) {
  117.             delta[j][k] = module[i][j][k] - iBase;
  118.          }
  119.       }
  120.       
  121.       int processed = processDelta(delta);
  122.       if (processed == 0) {
  123.          count[processSeq][0] = 1;
  124.          count[processSeq][base[i]]++;
  125.          
  126.          for (int j = 0; j < 3; j++) {
  127.             rotate(delta);
  128.             processDelta(delta);
  129.          }
  130.          
  131.          int max = 0;
  132.          for (int j = 0; j < 4; j++) {
  133.             for (int k = 0; k < 4; k++) {
  134.                if (delta[j][k] > max)
  135.                   max = delta[j][k];
  136.             }
  137.          }
  138.          count[processSeq][9] = max;
  139.          
  140.          processSeq++;
  141.          match(delta, max);
  142.          processDelta(mat);
  143.          for (int j = 0; j < 3; j++) {
  144.             rotate(mat);
  145.             processDelta(mat);
  146.          }
  147.          
  148.          processSeq++;
  149.       } else {
  150.          count[processed][0] = 1;
  151.          count[processed][base[i]]++;
  152.       }
  153.    }
  154.    
  155.    return processCount();
  156. }

  157. int main(void)
  158. {
  159.    static int module[MAX][4][4];
  160.    srand(3); // 3 will be changed
  161.    for (int tc = 1; tc <= 10; tc++)
  162.    {
  163.       for (int c = 0; c < MAX; c++)
  164.       {
  165.          int base = 1 + (rand() % 6);
  166.          for (int y = 0; y < 4; y++)
  167.          {
  168.             for (int x = 0; x < 4; x++)
  169.             {
  170.                module[c][y][x] = base + (rand() % 3);
  171.             }
  172.          }
  173.       }
  174.       cout << "#" << tc << " " << test2(module) << endl;
  175.    }

  176.    return 0;
  177. }








阅读(2389) | 评论(2) | 转发(0) |
0

上一篇:C语言的可变参数

下一篇:千字文

给主人留下些什么吧!~~