Thinking about the approximate boxing problem.

Source: Internet
Author: User

There is a need for a set of elements to be packaged (boxed), the volume of the box must be, but at least one item can be loaded, even if the volume of the item is larger than the box, please load with the fewest boxes.

The problem is similar to boxing. At the time of the issue of the object, you can reach the minimum number of packages, very practical significance, to study the packing problem. The following code is an implementation of the problem. Because it is the record function, the writing is more coarse, the follow-up amendment is explained in detail.

The implementation draws on the boxing algorithm, and the boxed algorithm can refer to the network article. As follows:

http://blog.csdn.net/zhangnaigan/article/details/38352745

http://blog.csdn.net/x_i_y_u_e/article/details/46765093

The specific implementation code is as follows:

$items=Array(    0.1,0.3,0.8,0.4,0.5,0.2,1);//sort ($items, sort_desc);defined(' boxsize ') | |Define(' Boxsize ', 1);functionBestencasement ($items){    Sort($items); $cnt=Count($items, Sort_desc);//more conducive to boxing    $box=Array(); $DEALARR=Array();  for($j= 0;$j<$cnt;$j++){        $lastSize=intval(boxsize);  for($i=$cnt-1;$i>0;$i--){            if(In_array($i,$DEALARR)){                Continue;//if the element has already been processed, skip            }            //detects if the box is empty if an element is added to an empty            if(Empty($box[$j])){                $box[$j][] =$items[$i]; $DEALARR[] =$i; $lastSize-=$items[$i]; if($items[$i]>=boxsize) {                     Break;//the space is full, just jump out of the next box .                }            }Else{                $tmpSize=$lastSize-$items[$i];//detects if the box is sufficient to store the element                if($tmpSize= = 0){                    $box[$j][] =$items[$i]; $DEALARR[] =$i;  Break;//0 of the space left out of the loop to process the next box                }                if($tmpSize>0) {//enough to deposit a box to detect the next element                    $box[$j][] =$items[$i]; $lastSize-=$items[$i]; $DEALARR[] =$i; Continue; }Else{                    //insufficient processing of the next box                     Break; }            }        }    }    return $box;}Print_r(Bestencasement ($items));

Thinking about the approximate boxing problem.

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.