poj 1020 Anniversary Cake
#include <iostream>
using namespace std;
int cnt[20], len[50];
int cake_side, npiece;
bool dfs(int used) //从上往下放,每次放在被覆盖最少的列上。
{
if(used == npiece)
return true;
int min = 100, ind;
for(int i = 0; i < cake_side; i++) //i=0表示第一列
{
if(len[i]<min)
{
min = len[i];
ind = i;
}
}
for(int i = 1; i <= 10; i++)
{
if(cnt[i]>0 && len[ind] + i <= cake_side && ind+i<=cake_side )
{
int tag=1;
for(int j = ind; j < ind+i; j++) //长度为i
if(len[j] != len[ind])
{
tag=0;break;
}
if(tag)
{
cnt[i]--;
for(int j = ind; j < ind + i; j++)
len[j] += i;
if(dfs(used + 1))
return true;
for(int j = ind; j < ind + i; j++)
len[j] -= i;
cnt[i]++;
}
}
}
return false;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
memset(len, 0, sizeof(len));
memset(cnt, 0, sizeof(cnt));
int area = 0;
scanf("%d %d", &cake_side, &npiece);
for(int i = 0; i < npiece; i++)
{
int side;
scanf("%d", &side);
cnt[side]++;
area += side * side;
}
if(cake_side * cake_side == area && dfs(0))
printf("KHOOOOB!\n");
else
printf("HUTUTU!\n");
}
return 0;
}