poj2362 Square
来源:互联网 发布:拍照姿势的软件 编辑:程序博客网 时间:2024/05/17 00:52
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
这题题意是给你一些边,看能够构成正方形,这题的数据比较水,为后面的poj1011埋下伏笔。
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;int vis[30],liang,a[30],n;//liang表示每条边的长bool cmp(int a,int b){return a<b;}int dfs(int x,int pos,int len)//x表示已经拼了几根,pos表示下次从哪根开始拼,len表示当前拼的这根已经拼了多少长度{int i,j;if(x==3)return 1;for(i=pos;i>=1;i--){if(!vis[i]){if(a[i]+len<liang){vis[i]=1;if(dfs(x,i-1,len+a[i]))return 1;vis[i]=0;}else if(a[i]+len==liang){vis[i]=1;if(dfs(x+1,n,0))return 1;vis[i]=0;}}}return 0;}int main(){int m,i,j,T,sum;scanf("%d",&T);while(T--){scanf("%d",&n);sum=0;for(i=1;i<=n;i++){scanf("%d",&a[i]);sum+=a[i];}if(sum%4!=0){printf("no\n");continue;}liang=sum/4;sort(a+1,a+1+n,cmp);memset(vis,0,sizeof(vis));if(dfs(0,n,0))printf("yes\n");else printf("no\n");}return 0;}
0 0
- poj2362 - Square
- poj2362--square
- POJ2362 Square
- POJ2362 Square
- poj2362 Square
- POJ2362:Square
- poj2362——Square
- POJ2362:Square(DFS)
- HDU1518 & POJ2362 & ZOJ1909 Square
- POJ2362 Square 搜索
- POJ2362《Square》题解
- POJ2362 Square(dfs)
- POJ2362 Square(dfs)
- poj2362 Square DFS剪枝
- poj2362——Square(深搜dfs)
- hdu1518 Square | ZOJ1909 | POJ2362 (dfs + 剪枝)
- Square poj2362深度优先搜索+剪枝
- POJ2362
- 如何配置android sdk manager进行强制下载
- 数据库
- CSU 1511 残缺的棋盘
- 基于 Android NDK 的学习之旅-----HelloWorld
- 535 Error: authentication failed
- poj2362 Square
- MySQL数据库与登录注册
- java基础第十讲——数据库、单例设计模式、服务器、项目(数据库—服务器—网络)
- 数据结构(15)静态链表
- python发送各类邮件的主要方法 smpt
- 1374 - Power Calculus (迭代加深+剪枝)
- 选择了tableView的某一行后以modal的方式跳转的到子控制器,以及子控制器导航栏左上角"关闭"按钮
- CheckBox的一些用法(内容包含动态加载布局)
- hdu 2023 求平均成绩