您的位置:首页 > 编程语言 > Java开发

hdu--1518--DFS(C&&java深搜之简单)

2013-08-07 13:56 375 查看


Square

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5902    Accepted Submission(s): 1882


Problem Description

Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?

 

Input

The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.

 

Output

For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".

 

Sample Input

3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5

 

Sample Output

yes
no
yes

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define max(a,b) ((a)>(b)?(a):(b))
int a[25];
int edge_len,m,tag[26];

int search(int start,int s,int cnt){
if(s==edge_len){
return cnt==3?1:search(0,0,cnt+1);
//return cnt==3?1:search(cnt+1,0,cnt+1);
//当找到符合的一条边时,其中必然包含a[0],因为edge_len=a[0]+a[1]+...a[m-1]。
//所以当搜索第二条边时,可以从cnt+1开始,也就是从a[1]开始

}else{
int i;
for(i=start; i<m; i++){
if(!tag[i]&&s+a[i]<=edge_len){
tag[i]=1;
if(search(i+1,s+a[i],cnt))
return 1;
tag[i]=0;
}
}
}
return 0;
}
int main(){
int n,i,sum;
int maxn;
scanf("%d",&n);
while(n--){
memset(tag,0,sizeof(tag));
scanf("%d",&m);
edge_len=sum=maxn=0;
for(i=0; i<m; i++){
scanf("%d",&a[i]);
sum += a[i];
maxn=max(maxn,a[i]);
}
if(sum%4 || maxn>sum>>2){
printf("no\n");
}else{
edge_len=sum>>2;
if(search(0,0,1)){
printf("yes\n");
}
else{
printf("no\n");
}
}
}
return 0;
}
   java版: 
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class hdu1518DFS{
private static boolean [] vis;
private static Integer [] array;
private static int edge;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cases,n,sum;
cases = in.nextInt();
while(cases-->0){
n = in.nextInt();
sum = 0;
vis = new boolean
;
array = new Integer
;
for(int i = 0; i < array.length; ++i){
array[i] = in.nextInt();
sum += array[i];
}
edge = sum >>2;
if(sum%4 == 0 && edge >= array[0]){
Arrays.sort(array,new Comparator<Integer>(){    //从大到小排序
@Override
public int compare(Integer o1, Integer o2){
return o2-o1;
}
});
if(dfs(0,0,0)){
System.out.println("yes");
}else{
System.out.println("no");
}
}else{
System.out.println("no");
}
}
}
private static boolean dfs(int curLen, int num, int cur){
if(num == 3){
return true;
}
if(curLen == edge){
if(dfs(0,num+1,num+1)){
return true;
}else{
return false;
}
}else{
for(int i = cur; i < array.length; ++i){
if(!vis[i] && curLen+array[i] <= edge){
vis[i] = true;
if(dfs(curLen+array[i],num,i+1)){
return true;
}
vis[i] = false;
}
}
}
return false;
}
}


 


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: