Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

dfs过了,C++菜鸟dfs16MS水过

Posted by 201501060326 at 2016-03-27 14:54:25 on Problem 1014
#include <iostream>
using namespace std;
int a[10];
int sum_value,half_value;
bool flag;
void dfs(int value,int num)
{
    if(flag) return;
    if(value==half_value)
    {
        flag=true;
        return;
    }
    for(int i=num; i>=1; i--)
    {
        if(a[i])
        {
            if(value+i<=half_value)
            {
                a[i]--;
                dfs(value+i,i);
                if(flag) break;
            }
        }
    }
    return;
}
int main()
{
    int i,count=1;
    flag=false;
    while(!flag)
    {
        sum_value=0;
        for(i=1; i<=6; i++)
        {
            cin>>a[i];
            if(a[i]) flag=true;
        }
        if(!flag) break;
        cout<<"Collection #"<<count++<<":"<<endl;
        for(i=1; i<=6; i++)
            sum_value+=i*a[i];
        flag=false;
        if(!(sum_value%2))
        {
            half_value=sum_value/2;
            dfs(0,6);
        }
        if(flag)
            cout<<"Can be divided."<<endl;
        else
            cout<<"Can't be divided."<<endl;
        cout<<endl;
        flag=false;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator