poj2362

和1011拼木棒差不多,但是直接拿那个程序居然错了,就是在拼新的木棒的时候忘记判断第一个加入其中的小木棒是不是会超出要拼的木棒的长度。

ContractedBlock.gifExpandedBlockStart.gifView Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<algorithm>
using namespace std;

const int maxn = 64;

int t, n, stick[maxn], each, ok, total, num;
bool used[maxn];

void init()
{
int i;

total
= 0;
for (i = 0; i < n; i++)
{
scanf(
"%d", &stick[i]);
total
+= stick[i];
}
ok
= false;
memset(used,
0, sizeof(used));
}

void dfs(int length, int start)
{
int i;

if (ok)
return;
if (length >= total)
{
ok
= true;
return;
}
if (length % each == 0)
{
i
= start;
while (used[i])
i
++;
used[i]
= true;
if (length + stick[i] < length / each * each + each)
dfs(length
+ stick[i], i + 1);
else if (stick[i] + length == length / each * each + each)
dfs(length
+ stick[i], 0);
used[i]
= false;
return;
}
for (i = start; i < n; i++)
{
if (used[i])
continue;
if (stick[i] + length < length / each * each + each)
{
used[i]
= true;
dfs(length
+ stick[i], i + 1);
used[i]
= false;
if (ok)
return;
}
else if (stick[i] + length == length / each * each + each)
{
used[i]
= true;
dfs(length
+ stick[i], 0);
used[i]
= false;
return;
}
}
}

int main()
{
int i, t, ca;

//freopen("t.txt", "r", stdin);
scanf("%d", &ca);
while (ca--)
{
scanf(
"%d", &n);
init();
sort(stick, stick
+ n);
for (i = 0; i < n / 2; i++)
{
t
= stick[i];
stick[i]
= stick[n - i - 1];
stick[n
- i - 1] = t;
}
if (total % 4 == 0)
{
each
= total / 4;
num
= 4;
dfs(
0, 0);
}
else
ok
= false;
if (ok)
printf(
"yes\n");
else
printf(
"no\n");
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值