POJ 1014

Dividing

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 75060   Accepted: 19654

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.

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.

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.

Source

Mid-Central European Regional Contest 1999

参考网址:

https://blog.csdn.net/lyy289065406/article/details/6661449

解题思路:

首先判断输入

而后算出toatalv,若总价值不为2的倍数,直接break

否则,算出halfv,使用DFS,参数为当前价值和当前取用的价值集合

从最大价值的背包里取起,但凡是数量不为0,且加起来小于等于halfv的都尝试加起来。

大体思路如图,将所有可能的情况都利用递归DFS进行,一旦有一个成功,则所有DFS都停止。

从最高价值开始算起,是因为这样可以更快剪枝(这个思路跟POJ1011相似)

#include <iostream>
#include<cmath>
using namespace std;
int a[6];
int flag;
int halfv;
int totalv; 
void dfs(int value, int pre)
{
 
    if(value == halfv)
    {
        flag = 1;
        return;
    }
 
    if(flag == 1)
    {    //不可少的,感受其作用,让递归栈中所有DFS结束
        return;
    }
 
    int i = 0;
    for(i = pre; i > 0; i--)
    {
        if(a[i])//若该价值的大理石还有 
        {
            if(i + value <= halfv)//且加起来小于halfv 
            {
                a[i]--;//取用 
                dfs(i + value, i);//继续从该价值的背包检索 
 
                if(flag)
                {
                    //不可少的,感受其作用,让递归栈中所有DFS结束
                break; 
                }
            }
        }
    }
}
 
int main()
{
  int collection=0;
  while(cin>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6])
  {
      if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0&&a[6]==0) 
      break;
      else
      {
        collection++;
        cout<<"Collection #"<<collection<<":"<<endl;
      totalv=0;
        for(int i=1;i<=6;i++)
        totalv+=i*a[i];
        if(totalv==0)
       break;
 
       if(totalv%2!=0)
          {
            cout<<"Can't be divided."<<endl;
            cout<<endl;
            continue;
        }
        else
        {
            halfv=totalv/2;
            flag=0;
            dfs(0,6);    
            if(flag)
            {
                cout << "Can be divided." << endl;
                cout<<endl;
                continue;
            } 
            else 
            {  
                cout << "Can't be divided." << endl;
                cout<<endl;
                continue;
            }
        } 
     } 
  }
 return 0;
}

猜你喜欢

转载自www.cnblogs.com/fantastic123/p/9361367.html