HDU1518-Square
来源:互联网 发布:matlab 三维数组 画图 编辑:程序博客网 时间:2024/06/05 20:56
原文链接:HDU1518-Square
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
原文大意:就是好多棍子,看能不能拼成正方形
- 所有的数据都得用上
- 输入第一行n:在4和20之间,以下n行,每行第一个数据m代表该行有m个数据
试题分析:输入数据总和需要是4的倍数,当拼好3条边的时候就意味着可以。利用深度搜索即可得出答案
AC代码:
#include<iostream>//#include<string>#include<algorithm>using namespace std;bool used[21];int arr[21];int t,m;//t:边长,sum:周长 ,m:输入的根数 bool cmp(int x,int y){ return x>y;}bool dfs(int count,int x,int y){ int i; //count:完成几条边,x:在遍历第几根,y:距离拼好目标差多少 if(count==3) {//若是已经拼完3根,意味着成功 return true; } for(i=x;i<m;i++) { if(used[i]) continue; used[i]=true; if(arr[i]==y)//若是第i个数据与y相等则拼成一条边 { if(dfs(count+1,0,t)) return true; } else if(arr[i]<y)//若小于,则遍历下面的边 { if(dfs(count,i+1,y-arr[i])) return true; } used[i]=false;//未成功拼成一条边 } return false;}int main(){ int i,n,sum; cin>>n; while(n--) { cin>>m; sum=0; for(i=0;i<m;i++) { cin>>arr[i]; sum+=arr[i]; } if(sum%4) { cout<<"no"<<endl; continue; } t=sum/4; memset(used,false,sizeof(used));//dev一直提示没有定义memset,但是oj可以通过 sort(arr,arr+m,cmp); if(dfs(0,0,t)) { cout<<"yes"<<endl; } else{ cout<<"no"<<endl; } } return 0;}
阅读全文
1 0
- hdu1518 Square
- HDU1518 Square
- hdu1518 Square
- HDU1518 Square
- Hdu1518 Square
- HDU1518-Square
- hdu1518 Square
- hdu1518 Square
- HDU1518-Square
- HDU1518:Square(DFS)
- hdu1518(DFS+剪枝 Square )
- HDU1518 & POJ2362 & ZOJ1909 Square
- HDU1518:Square(DFS)
- HDU1518 Square 【剪枝】
- HDU1518 Square (DFS)
- hdu1518 Square--DFS
- HDU1518 DFS Square
- HDU1518:Square(DFS)
- 并发编程(12)-生产者消费者
- javascript中的双感叹号语法(!!)
- 习题6.1(1)
- IE8兼容那些事(包括react)
- 漫谈兼容内核之二十二:Windows线程的调度和运行
- HDU1518-Square
- 奇怪的Java题:为什么1000 == 1000返回为False,而100 == 100会返回为True?
- mysql排序同值排名一致
- virtuoso 安装与使用
- POJ 1017
- Turtlebot学习指导第二篇_安装Turtlebot软件包,配置主从机网络
- SPPNet论文翻译-空间金字塔池化Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
- 第一个shell脚本
- GreenDao深入学习