POJ 1014

来源:互联网 发布:口袋弹弓 淘宝 编辑:程序博客网 时间:2024/05/16 04:54

感想:这次是借鉴别人的,写了很久都是超时,好菜啊我。。。。。

代码:http://blog.csdn.net/li4951/article/details/7434598

#include<iostream>using namespace std;int amount[7] = {0};int half_value = 0;int flag = 0;void DFS(int value, int pre){if(value == half_value){flag = 1;return;}if(flag == 1){//当已经完成时,结束所有父层递归return;}int i = 0;for(i = pre; i > 0; i--){//这里i从大到小是为了快一点到half_value,剪枝效果明显一点if(amount[i]){if(i + value <= half_value){amount[i]--;DFS(i + value, i);if(flag == 1){//当已经完成时,结束本层的其他递归return;}}}}}int main(){int testcase = 1;while(true){flag = 0;int totalvalue = 0;int N = 6;int i = 1;while(i <= N){cin >> amount[i];totalvalue += amount[i] * i;i++;}if(!totalvalue){break;}printf("Collection #%d:\n", testcase++);if(totalvalue % 2 != 0){cout << "Can't be divided." << endl << endl;continue;}half_value = totalvalue / 2;DFS(0, 6);if(flag){cout << "Can be divided." << endl;} else {cout << "Can't be divided." << endl;}cout << endl;}return 0;}


0 0
原创粉丝点击