简单搜索 Dividing
来源:互联网 发布:mysql官网怎么下载 编辑:程序博客网 时间:2024/06/05 00:14
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=79720#problem/H
Dividing
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uDescription
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 <cctype>using namespace std;#define N 110#define INF 0xfffffff#define PI acos (-1.0)#define EPS 1e-8const int dir[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};int a[7], sum, flag1;void DFS (int x);int main (){ int flag = 0; while (1) { if (flag++) cout << endl; flag1 = 0; sum = 0; for (int i=1; i<=6; i++) { cin >> a[i]; sum += i * a[i]; } if (!sum) break; cout << "Collection #" << flag << ':' << endl; if (sum & 1) { cout << "Can't be divided." << endl; continue; } DFS (0); if (flag1) cout << "Can be divided." << endl; else cout << "Can't be divided." << endl; }}void DFS (int x){ if (x == sum/2) { flag1 = 1; return; } for (int i=6; i>=1; i--) { if (a[i]) if (x + i <= sum / 2) { a[i]--; DFS (x + i); if (flag1) return; } }}
0 0
- 简单搜索 Dividing
- hdu1059 Dividing 搜索
- poj 1014 Dividing (搜索)
- poj1014.Dividing (暴力 && 搜索)
- dividing coins 简单dp
- POJ 1014 Dividing DFS搜索
- BNU - Dividing Stones 记忆化搜索
- poj 1014 Dividing (搜索:DFS)
- Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- uva562 - Dividing coins(简单动规)
- SpringMVC框架介绍
- LCA-最小公共父节点
- Android高效加载大图、多图解决方案,有效避免程序OOM
- String 字符串题目
- Android动画之正弦曲线运动
- 简单搜索 Dividing
- 【Hibernate三】关系映射之一对多单项关联
- 偷懒代码块之Activity的Intent带参数跳转
- linearlayout 点击变换颜色(点击效果)
- m个点取n个点计算
- A*寻路算法与它的速度
- 1046. Shortest Distance (20)
- Ducci序列(Ducci Sequence ,UVa1594)
- C++面试知识点总结