poj 1014 Dividing

#include <iostream>
using namespace std;
int num[7],total;
bool flag;
bool find(int sum)
{
if(sum==total)
{
flag
=1;
return true;
}
else if(sum>total)
return false;
else
{
int s;
for(int i=6;i>=1;i--) //这里相当于下面程序的pre
{
if(num[i]==0||i>total)
continue;
s
=sum+i;
num[i]
--;
if(find(s))
return true;
else
num[i]
++;
}
}
return false;
}
int main()
{
int t=1;
while(scanf("%d%d%d%d%d%d",&num[1],&num[2],&num[3],&num[4],&num[5],&num[6]))
{
total
=0;
for(int i=1;i<=6;i++)
total
+=num[i]*i;
if(total==0)
break;
printf(
"Collection #%d:\n",t++);
flag
=0;
if(total%2==0)
{
total
=total/2;
if(total%2==1&&num[1]==0&&num[3]==0&&num[5]==0) //运行时间为0ms,但如果没有这个剪枝,居然就TLE
flag=0;
else if(find(0))
printf(
"Can be divided.\n\n");
}
if(!flag)
printf(
"Can't be divided.\n\n");
}
return 0;
}

  

posted on 2011-07-20 22:30  sysu_mjc  阅读(158)  评论(0编辑  收藏  举报

导航