Php to solve the packing problem

Source: Internet
Author: User
Php to solve the packing problem
My solution to the problem of packing and cross-river stone
See http://www.oschina.net/question/117304_112681

The main implementation ideas are as follows:
1. large stones must be packed first (both early installation and later installation should be done first)
2. priority load is close to w
3. multiple parts are installed with the same weight, for example, 9, 6, 9, 5, and 1, and 951 packing is given priority.
4. use php functions to simplify code and generate functions based on K value
5. because of its nature, this type of problem requires a large amount of computing. set parameters for testing as appropriate.

Sample output: (when rocks is 1 ~ 9, w is 15, k is 3)
Find the maximum solution composed of three elements:
Array
(
[0] => 9
[1] => 5
[2] => 1
)

Find the maximum solution composed of two elements:
Array
(
[0] => 9
[1] => 6
)

Find the maximum solution composed of one element:
Array
(
[0] => 9
)

Find the maximum solution composed of three elements:
Array
(
[0] => 8
[1] => 4
[2] => 3
)

Find the maximum solution composed of two elements:
Array
(
[0] => 8
[1] => 7
)

Find the maximum solution composed of one element:
Array
(
[0] => 8
)

Find the maximum solution composed of three elements:
Array
(
[0] => 7
[1] => 6
[2] => 2
)

Find the maximum solution composed of two elements:
Array
(
[0] => 7
[1] => 6
)

Find the maximum solution composed of one element:
Array
(
[0] => 7
)

Minimum: 3
Shipment process: Array
(
[0] => Array
(
[0] => 9
[1] => 5
[2] => 1
)

[1] => Array
(
[0] => 8
[1] => 4
[2] => 3
)

[2] => Array
(
[0] => 7
[1] => 6
[2] => 2
)

)

  1. // Packing for php exercises
  2. // Author: mx (http://my.oschina.net/meikaiyuan)
  3. //
  4. // Problem:
  5. // Http://www.oschina.net/question/117304_112681
  6. /*
  7. Question:
  8. I have asked similar questions before, but I have not answered them well. So I want to ask again.
  9. A pile of stones, large and small, need to be pulled to the other side of the river by boat
  10. -- The stone has m blocks and the weight of each block is known.
  11. -- The ship can only hold k stones at a time, and the load weight cannot exceed w
  12. -- Find at least a few times to cross the river.
  13. ------------------------------------
  14. Example 1
  15. There are 9 stones in weight: 1, 2, 3, 4, 5, 6, 7, 8, and 9.
  16. K = 3
  17. W = 15
  18. The result is that the operation can be completed at least three times.
  19. ------------------------------------
  20. Example 2
  21. There are 9 stones with weights of, 6, and 9.
  22. K = 3
  23. W = 15
  24. The result is that the operation can be completed at least four times.
  25. */
  26. // Code starts
  27. // Stone
  28. Global $ rocks;
  29. // A ship can hold up to a few pieces at a time
  30. Global $ k;
  31. // Maximum load of the ship
  32. Global $ w;
  33. $ K = 3;
  34. $ Rocks = array (1, 2, 3, 4, 5, 6, 7, 8, 9 );
  35. // $ Rocks = array (, 6, 9); // use this data set to try the result?
  36. $ W = 15;
  37. // The number of current operations
  38. $ Count = 0;
  39. // Transportation process, two-dimensional array, such as 1 => array (9, 5, 1), indicates which
  40. $ Process = array ();
  41. // Evaluate the combination of array $ rocks, so that up to $ k elements and the sum of these elements is as large as possible but less than or equal to the specified value $ w, the array has been sorted from large to small
  42. Function getMaxCombination (){
  43. // Stone
  44. Global $ rocks;
  45. // A ship can hold up to a few pieces at a time
  46. Global $ k;
  47. // Maximum load of the ship
  48. Global $ w;
  49. // Save the largest set of all elements whose sum is less than or equal to w under $ k
  50. $ K_w_result = array ();
  51. // Maximum combination value
  52. $ Max_sum = 0;
  53. // Which is the largest?
  54. $ Max_one = 0;
  55. For ($ start = $ k; $ start> 0; $ start --){
  56. // Find the maximum solution composed of fixed $ start elements
  57. $ Start_w_arr = getMaxCombination2 ($ start );
  58. Echo "find the maximum solution consisting of $ start elements: \ n ";
  59. Print_r ($ start_w_arr );
  60. Echo "\ n ";
  61. $ Sum = array_sum ($ start_w_arr );
  62. // Note: Because it is sorted in descending order, $ k --, the earlier the sum is found, the larger the $ k combination, that is, the better the solution, so it is smaller than or equal
  63. If ($ sum> $ max_sum ){
  64. $ Max_sum = $ sum;
  65. $ Max_one = $ k-$ start;
  66. }
  67. $ K_w_result [] = $ start_w_arr;
  68. }
  69. Return $ k_w_result [$ max_one];
  70. }
  71. // Evaluate the combination of $ start elements in the array $ rocks. the sum of these elements is as large as possible but less than or equal to the specified value $ w. the array has been sorted from large to small.
  72. Function getMaxCombination2 ($ start ){
  73. // Stones
  74. Global $ rocks;
  75. // A ship can hold up to a few pieces at a time
  76. Global $ k;
  77. // Maximum load of the ship
  78. Global $ w;
  79. If (count ($ rocks) <$ start ){
  80. Return array (0 );
  81. }
  82. $ C = count ($ rocks );
  83. // Generate a function based on $ start, which contains the $ start layer for loop code, and then includes the code before calling this function.
  84. If (! File_exists ("$ start. php ")){
  85. $ Output_1 = "";
  86. $ Output_2 = '$ sum = ';
  87. $ Output_3 = 'If ($ sum <= $ w) {$ arr = array ();';
  88. $ Output_4 = '';
  89. For ($ I = 0; $ I <$ start; $ I ++ ){
  90. $ Output_1. = 'for ($ P '. $ I. '= '. $ I. '; $ P '. $ I. '<$ c -'. ($ start-$ i-1 ). '; $ P '. $ I. '++ ){'. "\ n ";
  91. If ($ I> 0 ){
  92. $ Output_2. = '+ ';
  93. }
  94. $ Output_2. = '$ rocks [$ p'. $ I.'] ';
  95. $ Output_3. = '$ arr [] = $ rocks [$ p'. $ I. '];';
  96. $ Output_4. = '}';
  97. }
  98. $ Output_2. = ';';
  99. $ Output_3. = 'return $ arr ;}';
  100. $ Output =' ';
  101. File_put_contents ("$ start. php", $ output );
  102. Include_once "$ start. php ";
  103. }
  104. Else {
  105. Include_once "$ start. php ";
  106. }
  107. Return call_user_func ('myfor '. $ start, $ rocks, $ c, $ w );
  108. }
  109. // Start calculation
  110. // The array is first sorted from large to small. this operation is the key to saving time and effort for subsequent algorithms.
  111. Rsort ($ rocks );
  112. // In order to prevent the stone from passing through a ship too small, the following algorithm will become an endless loop
  113. Foreach ($ rocks as $ v ){
  114. If ($ v> $ w ){
  115. Die! ");
  116. }
  117. }
  118. // Algorithm starts
  119. While (! Empty ($ rocks )){
  120. // Start to install a ship
  121. $ Process [$ count] = array ();
  122. // Shipment
  123. $ Process [$ count] = getMaxCombination ();
  124. // Remove the loaded
  125. Foreach ($ process [$ count] as $ v ){
  126. $ Key = array_search ($ v, $ rocks );
  127. Unset ($ rocks [$ key]);
  128. }
  129. $ Rocks = array_values ($ rocks );
  130. // Number of ships + 1
  131. $ Count ++;
  132. }
  133. // Output result
  134. Echo 'minimum Times: '. $ count. "\ n ";
  135. Echo 'shipping process :';
  136. Print_r ($ process );
  137. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.