poj 1518 square dfs
来源:互联网 发布:淘宝怎么买115会员 编辑:程序博客网 时间:2024/05/22 10:40
Square
问题描述 :
Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
输入:
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.
输出:
For each case, output a line containing “yes” if is is possible to form a square; otherwise output “no”.
样例输入:
3
4 1 1 1 1
5 10 20 30 40 50
8 1 7 2 6 4 4 3 5
样例输出:
yes
no
yes
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>const int maxn = 100;using namespace std;int maxx = 0;int a[maxn];int vis[maxn];bool flag ;int avg;int n;void dfs(int num,int len,int start){ if(num == 4) { flag = true; return; } if(flag) return; if(len == avg ) { dfs(num+1,0,0); if(flag) return; } for(int i = start; i < n;i++) { if(!vis[i]&&len+a[i]<=avg) { vis[i] =1; dfs(num,len+a[i],i+1); vis[i] = 0; if(flag) return; } }}//成功的的变数,目前的长度,开始的位置int main(){ int t; cin>>t; for(int i = 0; i < t; i++) { int sum = 0; maxx = 0; cin>>n; for(int j = 0; j < n; j++) { cin>>a[j]; if(a[j]>maxx) maxx = a[j]; sum+=a[j]; } avg= sum/4; if(maxx >avg||sum%4!=0) { cout<<"no"<<endl; continue; } sort(a,a+n); memset(vis,0,sizeof(vis)); flag = false; dfs(0,0,0); if(flag) cout<<"yes"<<endl; else cout<<"no"<<endl; } return 0;}
0 0
- poj 1518 square dfs
- POJ 2362 HDOJ 1518 Square DFS剪枝
- hdu 1518||poj 2362 Square(dfs)
- POJ 2362 && HDU 1518 Square(dfs)
- poj 2362Square(DFS)
- POJ 2362 Square dfs
- Square DFS poj&hoj
- POJ 2362 Square DFS
- dfs Square poj 2362
- DFS POJ 2362 Square
- POJ 2362 Square [DFS]
- 【POJ】Square(DFS + 剪枝)
- 【POJ】2362 Square(DFS)
- dfs hdu 1518 square
- hdu 1518 Square (DFS)
- hdu 1518 Square(dfs)
- 【DFS】hdu 1518 Square
- HDU-1518 Square(DFS)
- JS限制文本框输入金额并保留两位小数
- Mac与Phy组成原理的简单分析
- Json Unexpected character in stream: r
- Tensorflow指定使用哪个GPU
- Spring单例模式多线程安全问题-有状态的Bean
- poj 1518 square dfs
- android面试题-与IPC机制相关面试题
- TatukGIS Developer Kernel DXperience
- 四元数的运算规则
- 打印1到最大的n位数(Java实现)
- 多线程常用方法
- NSHTTPCookie的使用
- 获取线程的名称
- 数组、Vector、List、ArrayList、LinkedList的区别