Ecological Bin Packing


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*
  2.  * ecological-bin.cpp
  3.  *
  4.  * Created on: Feb 23, 2011
  5.  * Author: dirk
  6.  */
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. enum Sequence { BCG = 0, BGC = 1, CBG = 2, CGB = 3, GBC = 4, GCB = 5, NUMBER = 6 };
  12.  
  13. char const * const &get_str(Sequence const &s) {
  14. static char const * const str[] = { "BCG", "BGC", "CBG", "CGB", "GBC", "GCB" };
  15. return str[s];
  16. }
  17.  
  18. int main(int, char**) {
  19. // bottles
  20. // B G C B G C B G C
  21. // 0 1 2 3 4 5 6 7 8
  22. long long bottles[9] = {0};
  23. long long m = 0, tmp = 0;
  24. Sequence index;
  25.  
  26. while(cin >> bottles[0]) {
  27. for(int i = 1; i < 9; ++i) {
  28. cin >> bottles[i];
  29. }
  30.  
  31. m = bottles[1] + bottles[2] + bottles[3] + bottles[4] + bottles[6] + bottles[8]; // BCG
  32. index = BCG;
  33.  
  34. if((tmp=bottles[1] + bottles[2] + bottles[3] + bottles[5] + bottles[6] + bottles[7]) < m) {
  35. m = tmp; // BGC
  36. index = BGC;
  37. }
  38. if((tmp=bottles[0] + bottles[1] + bottles[4] + bottles[5] + bottles[6] + bottles[8]) < m) {
  39. m = tmp; // CBG
  40. index = CBG;
  41. }
  42. if((tmp=bottles[0] + bottles[1] + bottles[3] + bottles[5] + bottles[7] + bottles[8]) < m) {
  43. m = tmp; // CGB
  44. index = CGB;
  45. }
  46. if((tmp=bottles[0] + bottles[2] + bottles[4] + bottles[5] + bottles[6] + bottles[7]) < m) {
  47. m = tmp; // GBC
  48. index = GBC;
  49. }
  50. if((tmp=bottles[0] + bottles[2] + bottles[3] + bottles[4] + bottles[7] + bottles[8]) < m) {
  51. m = tmp; // GCB
  52. index = GCB;
  53. }
  54.  
  55. cout << get_str(index) << ' ' << m << '\n';
  56. }
  57. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.