Using greedy algorithm to solve bag packing problem

Source: Internet
Author: User

This is a Baidu know on the bag packing problem. My basic idea for solving this problem is to use greedy algorithms, also called greedy algorithms. The principle of the greedy algorithm is to find out what is currently the optimal solution.

The problem is described as follows:
There was a pile of sandbags, each with sand ranging from 1 to 100 in each bag.
It is now required to pack the sand into a 100-volume box.
The question is, how to use the fewest boxes to pack the sandbags?

My train of thought is this:
If you want to use the fewest boxes, then the boxes will be filled as much as possible. In order to achieve this goal, it is necessary to consider the strategy of composition.
The number of sandbags is more difficult to combine with other sandbags, so priority is placed in the box and then combined with other sandbags.
So the algorithm should be like this:
First from large to small sort, get sequence a,
Then take out the first element (the largest element) and remove the element from sequence a.
Then take out the next element, add this element and the first element, if less than 100, then remove this element from sequence a, and then continue to remove an element for this step.
If it is greater than 100, skip this element and continue with this step until all element traversal is complete.
The above sequence is then recorded and the sequence A is computed as described above, until there are no elements in sequence a.
The code is as follows:

1 class Program2 {3 Static voidMain (string[] args)4 {5 Try6 {7 int[] Sandpackages =New int[] { at, the, the, the, at, the, $, at,5, +, $, - };8 9 intTanksize = -;Ten  Onelist<int> sandlst =Newlist<int>(); A Sandlst.addrange (sandpackages); -  - //Sort the Sandlst.sort (); - //Flip, flip, interior sort to from big to small - sandlst.reverse (); -  +list<list<int>> Tanklst =Newlist<list<int>>(); -  + //loop, process the array until all the data is fetched A  while(Sandlst.count! =0) at { - //find a sequence where the data is added to the nearest 100 - Tanklst.add (Add2tank (Sandlst, tanksize)); - } -  - //Show in foreach(list<int> Sandsinchtanklst) - { to inttemp =0; + foreach(intSandinchSands) - { theTemp + =Sand; * console.write (sand); $Console.Write (" ");Panax Notoginseng } -Console.Write ("Total:"+temp); the Console.WriteLine (); + } A  the } + Catch(Exception ex) - { $ Throwex; $ } - } -  the Private Staticlist<int> Add2tank (list<int> Sandlst,inttanksize) - {Wuyilist<int> Sandlst2tank =Newlist<int>(); the intNowpos =0;//Current Position - intNowsize =0;//Current Totals Wu //iterating through an array -  while(Nowpos <sandlst.count) About { $ //adds the value of the current position to the current total - if((Nowsize + sandlst[nowpos]) <= -) - { - //if the calculated current total is less than or equal to 100, the data from the current position is placed in the list to be output (i.e. boxing) A //and remove from the original array + Sandlst2tank.add (Sandlst[nowpos]); theNowsize + =Sandlst[nowpos]; - sandlst.removeat (nowpos); $ } the Else the { thenowpos++; the } - } in  the returnSandlst2tank; the } About}

Using greedy algorithm to solve bag packing 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.