hdu--1518--DFS(C&&java深搜之简单)
来源:互联网 发布:数控铣床编程好学吗 编辑:程序博客网 时间:2024/06/06 06:56
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
34 1 1 1 15 10 20 30 40 508 1 7 2 6 4 4 3 5
Sample Output
yesnoyes#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[n]; array = new Integer[n]; 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; }}
- hdu--1518--DFS(C&&java深搜之简单)
- HDU-1213(简单DFS)
- hdu 2660 简单 dfs
- hdu 3274 简单 dfs
- hdu 3290 (简单dfs)
- hdu 1241 简单dfs
- 搜索--HDU简单DFS
- HDU 1181(简单dfs)
- HDU--1241 简单DFS
- hdu 1518 Square dfs深搜算法
- hdu Prime Ring Problem (java 简单DFS)
- HDU 1016 超简单DFS
- HDU 3368 Reversi (简单dfs)
- HDU/HDOJ 1016 简单dfs
- HDU 1241 简单的DFS
- HDU-5546(简单DFS)
- dfs hdu 1518 square
- HDU-1518 DFS
- poj1094 拓扑排序
- git常用命令
- Qt Creator 窗体控件自适应窗口大小布局
- 敏捷开发核心思想
- Andrid系统集成第三方pre
- hdu--1518--DFS(C&&java深搜之简单)
- PHP中CURL方法curl_setopt()函数的一些参数
- Windows文件关联和VC实现
- 几款手机点评
- 简单工厂模式
- Find out slow sql of mysql
- (转)C++ stringstream介绍,使用方法与例子
- Silverlight Business Application 与 WCF Ria Service的使用 (2)
- Struts2 从前端jsp向action传递自定义类型的链表数据