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
)
)
- // Packing for php exercises
- // Author: mx (http://my.oschina.net/meikaiyuan)
- //
- // Problem:
- // Http://www.oschina.net/question/117304_112681
- /*
- Question:
- I have asked similar questions before, but I have not answered them well. So I want to ask again.
- A pile of stones, large and small, need to be pulled to the other side of the river by boat
- -- The stone has m blocks and the weight of each block is known.
- -- The ship can only hold k stones at a time, and the load weight cannot exceed w
- -- Find at least a few times to cross the river.
- ------------------------------------
- Example 1
- There are 9 stones in weight: 1, 2, 3, 4, 5, 6, 7, 8, and 9.
- K = 3
- W = 15
- The result is that the operation can be completed at least three times.
- ------------------------------------
- Example 2
- There are 9 stones with weights of, 6, and 9.
- K = 3
- W = 15
- The result is that the operation can be completed at least four times.
- */
- // Code starts
- // Stone
- Global $ rocks;
- // A ship can hold up to a few pieces at a time
- Global $ k;
- // Maximum load of the ship
- Global $ w;
- $ K = 3;
- $ Rocks = array (1, 2, 3, 4, 5, 6, 7, 8, 9 );
- // $ Rocks = array (, 6, 9); // use this data set to try the result?
- $ W = 15;
- // The number of current operations
- $ Count = 0;
- // Transportation process, two-dimensional array, such as 1 => array (9, 5, 1), indicates which
- $ Process = array ();
- // 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
- Function getMaxCombination (){
- // Stone
- Global $ rocks;
- // A ship can hold up to a few pieces at a time
- Global $ k;
- // Maximum load of the ship
- Global $ w;
- // Save the largest set of all elements whose sum is less than or equal to w under $ k
- $ K_w_result = array ();
- // Maximum combination value
- $ Max_sum = 0;
- // Which is the largest?
- $ Max_one = 0;
- For ($ start = $ k; $ start> 0; $ start --){
- // Find the maximum solution composed of fixed $ start elements
- $ Start_w_arr = getMaxCombination2 ($ start );
- Echo "find the maximum solution consisting of $ start elements: \ n ";
- Print_r ($ start_w_arr );
- Echo "\ n ";
- $ Sum = array_sum ($ start_w_arr );
- // 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
- If ($ sum> $ max_sum ){
- $ Max_sum = $ sum;
- $ Max_one = $ k-$ start;
- }
- $ K_w_result [] = $ start_w_arr;
- }
- Return $ k_w_result [$ max_one];
- }
- // 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.
- Function getMaxCombination2 ($ start ){
- // Stones
- Global $ rocks;
- // A ship can hold up to a few pieces at a time
- Global $ k;
- // Maximum load of the ship
- Global $ w;
- If (count ($ rocks) <$ start ){
- Return array (0 );
- }
- $ C = count ($ rocks );
- // Generate a function based on $ start, which contains the $ start layer for loop code, and then includes the code before calling this function.
- If (! File_exists ("$ start. php ")){
- $ Output_1 = "";
- $ Output_2 = '$ sum = ';
- $ Output_3 = 'If ($ sum <= $ w) {$ arr = array ();';
- $ Output_4 = '';
- For ($ I = 0; $ I <$ start; $ I ++ ){
- $ Output_1. = 'for ($ P '. $ I. '= '. $ I. '; $ P '. $ I. '<$ c -'. ($ start-$ i-1 ). '; $ P '. $ I. '++ ){'. "\ n ";
- If ($ I> 0 ){
- $ Output_2. = '+ ';
- }
- $ Output_2. = '$ rocks [$ p'. $ I.'] ';
- $ Output_3. = '$ arr [] = $ rocks [$ p'. $ I. '];';
- $ Output_4. = '}';
- }
- $ Output_2. = ';';
- $ Output_3. = 'return $ arr ;}';
- $ Output =' ';
- File_put_contents ("$ start. php", $ output );
- Include_once "$ start. php ";
- }
- Else {
- Include_once "$ start. php ";
- }
- Return call_user_func ('myfor '. $ start, $ rocks, $ c, $ w );
- }
- // Start calculation
- // The array is first sorted from large to small. this operation is the key to saving time and effort for subsequent algorithms.
- Rsort ($ rocks );
- // In order to prevent the stone from passing through a ship too small, the following algorithm will become an endless loop
- Foreach ($ rocks as $ v ){
- If ($ v> $ w ){
- Die! ");
- }
- }
- // Algorithm starts
- While (! Empty ($ rocks )){
- // Start to install a ship
- $ Process [$ count] = array ();
- // Shipment
- $ Process [$ count] = getMaxCombination ();
- // Remove the loaded
- Foreach ($ process [$ count] as $ v ){
- $ Key = array_search ($ v, $ rocks );
- Unset ($ rocks [$ key]);
- }
- $ Rocks = array_values ($ rocks );
- // Number of ships + 1
- $ Count ++;
- }
- // Output result
- Echo 'minimum Times: '. $ count. "\ n ";
- Echo 'shipping process :';
- Print_r ($ process );
- ?>
|