【DFS】HDU1518Square
来源:互联网 发布:win7网络加速设置 编辑:程序博客网 时间:2024/05/22 03:16
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518
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
这题目是真的有毒哇。。。先TLE,在WA来回十几次。。。把能够合成的边定义的全局变量就WA,定义在传参dfs里面就AC了。。。
代码:
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int a[30];int vis[30];int flag,sum,n;void dfs(int t,int cnt,int st){ if(t==5){ flag=1; return; } if(cnt==sum){ dfs(t+1,0,0); if(flag) return; } for(int i=st;i<n;i++){ if(!vis[i]&&cnt+a[i]<=sum){ vis[i]=1; dfs(t,cnt+a[i],i+1); vis[i]=0; if(flag) return; } } return;}bool cmp(int a,int b){return a>b;}int main(){ int t; cin.sync_with_stdio(false); cin>>t; while(t--){ cin>>n; sum=0; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++){ cin>>a[i]; sum+=a[i]; } if(sum%4||n<4){ cout<<"no"<<endl; continue; } sum=sum/4; flag=0; sort(a,a+n,cmp); for(int i=n-1;i>0;i--){ if(sum<a[i]){ cout<<"no"<<endl; flag=1; break; } } if(flag) continue; dfs(1,0,0); if(flag) cout<<"yes"<<endl; else cout<<"no"<<endl; } return 0;}
0 0
- 【DFS】HDU1518Square
- hdu1518Square
- hdu1518Square【经典深搜】
- DFS
- DFS
- dfs
- dfs
- dfs
- dfs
- DFS
- DFS
- dfs
- DFS
- DFS
- DFS
- dfs
- DFS
- dfs
- windows下mysql中文乱码问题
- css特殊图形绘制
- 深入解析Windows窗口创建和消息分发
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- Manual Reference Counting AutoMatic Reference Counting 混编
- 【DFS】HDU1518Square
- XML Parser Errors See Details for more Information XML Parser Error on line 1: Document root ele
- tableView图片下拉放大
- 自定义标准广播
- POJ 1837 balance(dp)
- RVO(Return Value Optimization)和NRVO(Named Return Value Optimization)
- 看postgre源码看的好烦,记录一下吧
- Jenkins常用插件之Deploy Plugin
- 第八周实践项目1————数组做数据成员(2)