Simple implementation of Grid backpack

Source: Internet
Author: User

If it's a backpack interface that uses a grid backpack, like the "Diablo" backpack, that's how I define the structure. The discovery is similar to the original paper-opening algorithm, but it happens in reverse. Anyway, it's good to think about it and realize it.

The placeholder for an item, the default upper-left corner is the (0,0) point. Planning in filling in the form, only need to fill in according to the diagonal relationship (the) this point on it.

The server item object only needs to store the position of the two-dimensional array where the upper-left corner (0,0) is located, and if the rotation of the item (Row and column interchange) is taken into account, a value can be added to make the decision
class item{
Public:
int uid; //Unique ID
int quality; //Quality
int pos[2]; //In backpack position (upper left corner)
int Isrota; //whether rotating
};

This can be conveniently stored in a map or inside a vector.

So the only problem is the automatic pack! That's how I arranged my backpack.

Each time you put an item in a backpack, you will cut the backpack into two areas, one on the other, one below, and three if another line. Occupy a lattice of props and items, no matter how the backpack to organize these items can be placed under. Because the total number of squares is fixed. So these can be filled in finally. Not to be considered for the time being.

Hypothesis: There are n different quality items, occupying the standard as follows, regardless of rotation, and to ensure that the backpack (max_width,max_height) can be put down, not to abandon. So how to tidy the backpack (regardless of rotation).

Input: n items, data structure above

Output: n items, the location information in the backpack, the default location is not fit (0,0)

The algorithm describes the horizontal placement until it is placed and then wrapped. Because of the high consistency, from the width of the large to the small put, in order to ensure that each line is full

According to the parity of max_width, the odd number of first release vessel is even first put.

Add: In order to ensure that the new items can be quickly put into the backpack, in fact, you can store the int rightpos[2],int bottopos[2] These two location information. Due to the fact that there is a case that occupies a lattice item. To prevent the effect on the algorithm. Items that occupy a single lattice, are placed by default from the bottom row of the backpack if the bottom row is full, start with the right column.

   1:#include"stdafx.h"
   2:#include <map>
   3:#include <vector>
   4:#define max_width  100
   5:#define max_height 100
   6:constint initpos_0[2] = {0, 0};
   7:constint initpos_1[2] = {2, 0};
   8:constint initpos_2[2] = {2, 1};
   9:constint initpos_3[2] = {2, 2};
  10:
  11:
  :class Item;
  :typedef std::map<int, item*> item_map;
  :typedef std::map<int, Item*>::iterator  Item_itertor_map;
  15:
  :class Item
  : {
  :public:
  :     int uid;
  :     int quality;
  £ º     int pos[2];
  :     int //Occupied location
  23:
  :     Item (intintconstintconst int init_val[2])
  :     {
  :         uid = uid_val;
  :         quality = Quality_val;
  :         sizeof (POS));
  £ º         sizeof (init));
  :     }
  : };
  32:
  :void sort (item_map itemmap)
  : {
  :     int //Record the start of the remaining position on the right
  £ º     int //Record the start of the remaining position at the bottom
  Panax Notoginseng:     std::vector<item*>    Initpos1vec;  Initpos_1 a group
  £     std::vector<item*>  Initpos2vec;  Initpos_2 a group
  :     std::vector<item*>  Initpos3vec;  Initpos_3 a group
Max   :      for (Item_itertor_map itor = Itemmap.begin (); Itor! = Itemmap.end (); itor++)
  :     {
  £ º         item* item = itor->second;
  :         if (item->init[1] = = 0)
  :         {
  :             initpos1vec.push_back (item);
  :         }
  :         Else if (Item->init[1] = = 1)
  :         {
  :             initpos2vec.push_back (item);
  :         }
  Wuyi:         Else if (Item->init[1] = = 2)
  :         {
  :             initpos3vec.push_back (item);
Wu   :         }
  :     }
  :     if (Max_width% 2)
  £ º     {
  :         //self-fulfilling, confidential
  :     }
  :     Else
A   :     {//Even in first place
  :         //self-fulfilling, confidential
  :     }
  :     return;
  : }
  66:
  :void output (Item_map itemmap)
  : {
  :      for (Item_itertor_map itor = Itemmap.begin (); Itor! = Itemmap.end (); itor++)
  :     {
  :         item* item = itor->second;
  :         if (Item->uid < 10)
  :         {
About   :             printf ("uid:0%d,quality:%d,pos:[%d,%d],init:[%d,%d] \ n", Item->uid, item-> Quality, item->pos[0], item->pos[1], item->init[0], item->init[1]);
  :             continue;
  :         }
  :         printf ("uid:%d,quality:%d,pos:[%d,%d],init:[%d,%d] \ n", Item->uid, item- >quality, Item->pos[0], item->pos[1], item->init[0], item->init[1]);
  :     }
  : }
  80:
  Bayi:int _tmain (int argc, _tchar* argv[])
  : {
  :     Item_map Itemmap;
A   :      for (int i = 0; i <; i++)
  £ º     {
  :         if (I < 5)
by   :         {
  :             New Item (i + 1, rand ()% 3 + 1, initpos_0, initpos_1)));
  :             continue;
  :         }
  :         if (I < 10)
The   following:         {
  :             New Item (i + 1, rand ()% 3 + 1, initpos_0, initpos_2)));
  94:             continue;
  :         }
  :         if (I < 15)
  :         {
  98:             New Item (i, rand ()% 3 + 1, initpos_0, Initpos_3)));
  :         }
:     }
101:     Sort (itemmap);
102:     Output (ITEMMAP);
103:     GetChar ();
104:     return 0;
: }
106:

Simple implementation of Grid backpack

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.