C*n
2 楼
Is it 2D bin packing?
j*2
9 楼
我是个菜鸟,来胡乱写个DP pseudo code,大家表笑:
class pixel{ int right_blank; int below_blank;};
class img{
int h; int w; int area;
public: img(int x, int y)
{h=x;w=y;area=x*y;}
};
class wall{
pixel **matrix;
int w; int h;
int blank_area;
void init()
{
deoccupy(0,0,img(h,w));
blank_area=w*h;
}
bool can_put(int &x, int &y, img pic)
{
for(int i=0;i for(int j=0;j if( matrix[i][j].right_blank>=pic.w &&
matrix[i][j].below_blank>=pic.h)
{
x=i; y=j;
return true;
}
return false;
}
void occupy(int x, int y, img pic)
{
for(int i=x;i for(int j=y;j matrix[i][j]=pixel(0,0);
blank_area-=pic.w*pic.h;
}
void deoccupy((int x, int y, img pic)
{
for(int i=x;i for(int j=y;j matrix[i][j]=pixel(h-1-i,w-1-j);
blank_area+=pic.w*pic.h;
}
public:
int fit_wall(vector album)
{
int max_num=0;
for(int i=0;i {
img pic=album[i];
if(pic.area {
int x, y;
if(can_put(x,y,pic))
{
album.delete(album.begin()+i);
occupy(x,y,pic);
max_num=max(max_num, fit_wall(temp_album)+1);
deoccupy(x,y,pic);
album.insert(album.begin+i, pic);
}
}
}
return max_num;
}
};
【在 u******g 的大作中提到】
: 木有DP算法,NP-hard问题
: 只有近似做法或者加限制的情况下DP
class pixel{ int right_blank; int below_blank;};
class img{
int h; int w; int area;
public: img(int x, int y)
{h=x;w=y;area=x*y;}
};
class wall{
pixel **matrix;
int w; int h;
int blank_area;
void init()
{
deoccupy(0,0,img(h,w));
blank_area=w*h;
}
bool can_put(int &x, int &y, img pic)
{
for(int i=0;i
matrix[i][j].below_blank>=pic.h)
{
x=i; y=j;
return true;
}
return false;
}
void occupy(int x, int y, img pic)
{
for(int i=x;i
blank_area-=pic.w*pic.h;
}
void deoccupy((int x, int y, img pic)
{
for(int i=x;i
blank_area+=pic.w*pic.h;
}
public:
int fit_wall(vector album)
{
int max_num=0;
for(int i=0;i
img pic=album[i];
if(pic.area
int x, y;
if(can_put(x,y,pic))
{
album.delete(album.begin()+i);
occupy(x,y,pic);
max_num=max(max_num, fit_wall(temp_album)+1);
deoccupy(x,y,pic);
album.insert(album.begin+i, pic);
}
}
}
return max_num;
}
};
【在 u******g 的大作中提到】
: 木有DP算法,NP-hard问题
: 只有近似做法或者加限制的情况下DP
f*t
11 楼
我听说过的版本是给一堆图片,要求在最小的矩形范围内贴出来。这题难得逆天了
r*e
13 楼
google 到的:
http://codeincomplete.com/posts/2011/5/7/bin_packing/
Here's a summary: build a binary tree. Each branch in the tree contains a
sprite. Each leaf node represents available space. Initially the tree has
just the root node, which represents all available space. To add a sprite to
the tree, search the tree for an unoccupied (leaf) node big enough to hold
the sprite. Turn that node from a leaf into a branch by setting the sprite
as the node's occupant and giving the node two children. One child
represents the remaining space to the right of the sprite; the other
represents the remaining space below the sprite and the first child.
http://codeincomplete.com/posts/2011/5/7/bin_packing/
Here's a summary: build a binary tree. Each branch in the tree contains a
sprite. Each leaf node represents available space. Initially the tree has
just the root node, which represents all available space. To add a sprite to
the tree, search the tree for an unoccupied (leaf) node big enough to hold
the sprite. Turn that node from a leaf into a branch by setting the sprite
as the node's occupant and giving the node two children. One child
represents the remaining space to the right of the sprite; the other
represents the remaining space below the sprite and the first child.
w*f
14 楼
思路是对的, 一个小问题. 当你算can_fit()时, 应该check (i,j) ~ (i+a.w,j+a.h)中
的每个点是否available to fit the current picture. 因为有的点可能没有update.
尤其在以下情况, 大方块上的右边空白出的点的值没有update
****
* *
****
*******
* *
* *
* *
*******
【在 j******2 的大作中提到】
: 我是个菜鸟,来胡乱写个DP pseudo code,大家表笑:
: class pixel{ int right_blank; int below_blank;};
: class img{
: int h; int w; int area;
: public: img(int x, int y)
: {h=x;w=y;area=x*y;}
: };
: class wall{
: pixel **matrix;
: int w; int h;
的每个点是否available to fit the current picture. 因为有的点可能没有update.
尤其在以下情况, 大方块上的右边空白出的点的值没有update
****
* *
****
*******
* *
* *
* *
*******
【在 j******2 的大作中提到】
: 我是个菜鸟,来胡乱写个DP pseudo code,大家表笑:
: class pixel{ int right_blank; int below_blank;};
: class img{
: int h; int w; int area;
: public: img(int x, int y)
: {h=x;w=y;area=x*y;}
: };
: class wall{
: pixel **matrix;
: int w; int h;
相关阅读
这个怎么弄?问一下background check,如果需要international check要多久问一下那个红黑树transfer H1b从提交到收到receipt需要多长时间小白问题:才开始准备面试题,mitbbs里的面经怎么用?这样被录用的可能性有多大?继续postdoc呢还是去小的CRO? (转载)急问:接受了的offer还可以再拒掉吗老公即将三面A家,求祝福!为什么一个副总裁老说自己公司是小公司有个职位,没有相关的EXPERIENCEgoogle 面试中国科学院国家天文台射电天文技术联合实验室招聘电子技术和结(转载)来确认一下说好店面,没打电话来请教被多人轮着面的问题Design Intelligence Salary Survey 2011 (转载)两道面试题大家用哪个网站找opening?OPT 加急的一点点经验