POJ 1014

链接: http://poj.org/problem?id=1014  Divding

#include
using namespace std;
bool dfs(int *p,int value);
int main()
{
for (int j=0;;j++)
{
int p[6],sum=0;
for (int i=0;i<6;i++)
{
cin>>p[i];
sum=sum+p[i]*(i+1);
}
if(sum==0)
break;
cout<<"Collection #"<<j+1<<":"<<endl;
if(sum%2==0)
{
if(dfs(p,sum/2))
{
cout<<"Can be divided."<<endl<<endl;
}
else
{
cout<<"Can't be divided."<<endl<<endl;
}
}
else
{
cout<<"Can't be divided."<<endl<<endl;
}
}
return 0;
}
//背包问题;
bool dfs(int *p,int value)
{
int *x=new int[value+1];
memset(x,0,sizeof(int)*(value+1));
for (int i=1;i<=6;i++)
{
x[0]=1;
int *count=new int[value+1];
memset(count,0,sizeof(int)*(value+1));
for (int j=i;j<=value&&count[j-i]
{
if(x[j-i]>0&&x[j]==0)
{
x[0]=0;
x[j]=x[j-i]+i;
count[j]=count[j-i]+1;
}
}
delete []count;
}
if(x[value]==value)
{
delete []x;
return true;
}
else
{
delete []x;
return false;
}
}

你可能感兴趣的:(POJ 1014)