小y 有一个宽度为 100cm,高度为 20cm,深度为 1cm 的柜子,如下图。
小y 还有 3636 个礼物盒,他们的深度都为 1cm。
他们对应的宽度和高度如下,单位(cm)。
11 3
8 12
11 17
16 13
1 14
2 8
6 10
10 18
17 11
10 15
6 14
5 6
2 19
19 10
4 9
7 9
5 14
5 20
15 19
3 17
15 11
7 25
11 20
9 12
17 4
9 19
4 18
10 10
12 19
17 3
19 9
20 16
11 16
10 2
20 15
3 14
现在小y 想把这些盒子放到柜子上,由于礼物盒里面都装有礼物,礼物盒必须向上放置,并且不能堆放。由于礼物盒深度和柜子深度一样,所以礼物盒和柜子深度方向也必须一致。并且礼物盒的高度还不能大于柜子的高度,否者放不进去。小y 希望放到柜子上礼物盒的宽度和正好等于柜子的宽度,也就是希望柜子两边都不存在间隙。如下图符合条件的放置。
满足条件的情况下,小y 希望能尽可能多的放置礼物盒,算出最多能放多少个礼物盒。
#include<iostream>
#include<algorithm>
using namespace std;
//已经去掉了高度超过20的礼物盒
void dfs(int num,int index,int wth);//num为已经选好的礼物数 index为当前所指礼物的下标 wth是当前礼物的宽度和
int widths[35] = {11,8,11,16,1,2,6,10,17,10,6,5,2,19,4,7,5,5,15,3,15,11,9,17,9,4,10,12,17,19,20,11,10,20,3};
int step[36];
int cnt;
int flag = 0;
int main()
{
int temp1,temp2;
sort(widths,widths+35);
dfs(0,0,0);
cout<<cnt<<endl;
return 0;
}
void dfs(int num,int index,int wth)
{
if(flag) return ;
if(wth>100) return ;
if(wth==100)
{
cnt = num;
flag = 1;
}
if(index>=36) return;
if(wth<100&&index<35)
{
for(int i = index;i<35;i++)
{
dfs(num+1,i+1,wth+widths[i]);
}
}
}