poj 1014 Dividing (搜索)
来源:互联网 发布:中国国家数据统计网 编辑:程序博客网 时间:2024/05/22 04:48
http://poj.org/problem?id=1014
Dividing
Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , n6 , where ni is the number of marbles of value i. So, the example from above would be described by the input-line "1 0 1 2 0 0". The maximum total number of marbles will be 20000.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
For each collection, output "Collection #k:", where k is the number of the test case, and then either "Can be divided." or "Can't be divided.".
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
Sample Output
Collection #1:Can't be divided.Collection #2:Can be divided.
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 2400#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-8#define met(a, b) memset (a, b, sizeof (a))typedef long long LL;int a[N], half, flag;void DFS (int v, int Index){ if (v == half) { flag = 1; return; } for (int i=Index; i>=1; i--) { if (a[i]) { if (v + i <= half) { a[i]--; DFS (v+i, i); if (flag) break; } } } return;}int main (){ int nCase = 1; while (scanf ("%d %d %d %d %d %d", &a[1], &a[2], &a[3], &a[4], &a[5], &a[6])) { int sum = a[1]+a[2]*2+a[3]*3+a[4]*4+a[5]*5+a[6]*6; if (!sum) break; printf ("Collection #%d:\n", nCase++); if (sum % 2) { puts ("Can't be divided.\n"); continue; } flag = 0; half = sum / 2; DFS (0, 6); if (flag) puts ("Can be divided."); else puts ("Can't be divided."); puts (""); } return 0;}
0 0
- poj 1014 Dividing (搜索)
- POJ 1014 Dividing DFS搜索
- poj 1014 Dividing (搜索:DFS)
- POJ 1014 Dividing
- POJ 1014 Dividing
- poj 1014 Dividing
- poj 1014Dividing
- POJ 1014 Dividing
- POJ 1014 Dividing
- POJ 1014 Dividing
- poj 1014 Dividing
- Poj 1014 Dividing
- POJ 1014 Dividing
- POJ 1014 Dividing
- poj 1014 Dividing
- POJ-1014-Dividing
- POJ 1014 dividing
- poj 1014 Dividing
- 基本Kmeans算法介绍及其实现
- 第八周实践项目1--建立顺序串的算法库
- lazarus的控件、例程集大成版本:CodeTyphone
- spring入门02 --AOP之jdk与cglib代理
- UML第六集
- poj 1014 Dividing (搜索)
- 黑马程序员---面向对象(二)(继承)
- 图像匹配
- Java中的深拷贝(深复制)和浅拷贝(浅复制)
- java线程简析
- 基于windows sever2010的FTP服务器搭建
- printf %m
- 航空电子全双工交换式以太网AFDX
- HDU 1028 整数分化