Ppoj 1014 深搜

时间:2022-09-05 15:37:41

这个题题意是给你价值1-6的珠宝个数输出能否平分为两份(如果平分为三分就不知道怎么做了……)

主要是用回溯DFS,但是要剪枝,对200取模……!!(很重要……)

代码……

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstdlib> using namespace std; int n[];
bool flag;
int aver; void dfs(int num,int t){
if(flag)
return;
if(num == aver){
flag = ;
return;
} for(int i = t;i <= ;i++){
if(n[i] && num + i <= aver){
n[i]--;
dfs(num+i,i);
n[i]++;
if(flag)
return;
}
}
} int main()
{
//freopen("input.txt","r",stdin);
int cas = ;
while(){
int sum = ;
for(int i = ;i <= ;i++){
cin >> n[i];
n[i] %= ;
sum += (i*n[i]);
}
if(sum == )
break;
cout << "Collection #" << cas++ << ":" << endl;
if(sum % == ){
cout << "Can't be divided." << endl << endl;
continue;
}
aver = sum /;
flag = ;
dfs(,);
if(flag){
cout << "Can be divided." << endl << endl;
}
else{
cout << "Can't be divided." << endl << endl;
}
}
return ;
}