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

1011改装版—0MS

Posted by zjy9107 at 2010-05-12 18:22:57 on Problem 2362
#include<iostream>
#include<algorithm>
using namespace std;

const int size=21;
int sq[size];
bool visit[size];
int start;
int n;

bool cmp(int a,int b){
	return a>b;
}

bool dfs(int m,int left,int len){
	if(m==0&&left==0)
		return true;
	int i;
	if(left==0){
		i=0;
		left=len;
	}
	else i=start+1;
	for(;i<n;i++){
		if(visit[i]||left<sq[i])
			continue;
		start=i;
		visit[i]=true;
		if(dfs(m-1,left-sq[i],len))
			return true;
		else
			visit[i]=false;
		if(left==sq[i]||left==len)
			break;
	}
	return false;
}

int main()
{
	int t;
	cin>>t;
	while(t--){
		cin>>n;
		int sum=0;
		for(int i=0;i<n;i++){
			cin>>sq[i];
			sum+=sq[i];
		}
		if(sum%4){
			cout<<"no"<<endl;
			continue;
		}
		sort(sq,sq+n,cmp);
		if(dfs(n,0,sum/4))
			cout<<"yes"<<endl;
		else 
			cout<<"no"<<endl;
		fill(visit,visit+n,0);
	}
	system("pause");
}
		

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