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

大家帮忙看一下为什么会WA?

Posted by sunpy at 2008-07-31 17:01:03 on Problem 2362
#include<stdio.h>

#define N 20
#define M 4

int edge_len,findtag;
int a[N];
int used[N];

void sort(int a[],int n);
void square(int s,int n);
void dfs(int m,int curlen,int cur,int n);


int main()
{
	int testcase,i;
	scanf("%d",&testcase);
	for(i=0;i<testcase;i++)
	{
		int stick_num,j;
		scanf("%d",&stick_num);
		for(j=0;j<stick_num;j++)
			scanf(" %d",a+j);
		int sum_len=0;
		for(j=0;j<stick_num;j++)
		{
			used[j]=0;
			sum_len+=a[j];
		}
		if(sum_len%4!=0)	//pruning first time
		{
			printf("no\n");
			continue;
		}
		edge_len=sum_len/4;
		sort(a,stick_num);
		if(a[0]>edge_len)//pruning the second time
		{
			printf("no\n");
			continue;
		}
		findtag=0;
		square(4,stick_num);//start from the first edge
		if(findtag)
			printf("yes\n");
		else
			printf("no\n");
	}//for testcase
	return 0;
}

void square(int m,int n)
{
	int i;
	if(m==1)
	{
		findtag=1;
		return ;
	}
	else
	{
		for(i=0;i<n;i++)
			if(!used[i])
				break;
		used[i]=1;
		dfs(m,a[i],i,n);//current position has not been used,and it must exist a road
		//used[i]=0;
	}
	return ;
}
				

void dfs(int m,int curlen,int next,int n)
{
	int i;
	if(findtag)
		return ;
	if(edge_len==curlen)
	{
		square(m-1,n);
			return ;
	}
	else
	{
		for(i=next+1;i<n;i++)
			if(!used[i])
			{
				used[i]=1;
				if(curlen+a[i]<=edge_len)
					dfs(m,curlen+a[i],i,n);
				if(findtag)
					return ;
				used[i]=0;
			}
	}
}


void sort(int a[],int n)
{
	int t;
	int i,j;
	for(i=1;i<n;i++)
	{
		t=a[i];
		j=i-1;
		while(j>=0&&a[j]<t)
		{
			a[j+1]=a[j];
			j--;
		}
		a[j+1]=t;
	}
}

我把square函数中的used[i]=0;注释掉就会错,但是我想了很久还是觉得这句可以不要
因为a[i]以后都不会被用到了

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