【POJ】2362 Square(DFS)
来源:互联网 发布:emlog源码下载 编辑:程序博客网 时间:2024/05/21 02:51
【POJ】2362 Square(DFS)
【题目链接】http://poj.org/problem?id=2362
题目内容
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 asquare; 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
题目大意
给出n根木棍问能不能拼出正方形
解题思路
如果木棍长度总和不是4的倍数,直接排除,不然长度和/=4,如果能dfs构成三条边,就能构成正方形。
具体的dfs过程见注释。
AC代码
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<string> #include<algorithm> #include<math.h> #include<limits.h> #include<stack> #include<queue> #define LL long long using namespace std; int a[25]; int b[25];标记木棍i有没有被用过 bool cmp(int a,int b) { return a>b; } int n,sum; bool flag; void dfs(int x,int i,int num)//x是已经拼好的长度,i是待检测的木棍,num是已经拼好的边的数量; { if(num==3||flag) { flag=true; return; } for(int j=i;j<n;j++) { if(a[j]>sum-x) return; if(b[j]==0&&a[j]==sum-x) { b[j]=1; dfs(0,0,num+1); b[j]=0; return; } if(b[j]==0&&a[j]<sum-x) { b[j]=1; dfs(x+a[j],j+1,num); if(flag) return; b[j]=0; } } } int main() { int t; scanf("%d",&t); while(t--) { sum=0; flag=false; memset(b,0,sizeof(b)); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } if(sum%4!=0) { printf("no\n"); continue; }//排除总长度不是4的倍数的情况 sum/=4; sort(a,a+n); if(a[n-1]>sum) { printf("no\n"); continue; } dfs(0,0,0); if(flag) printf("yes\n"); else printf("no\n"); } return 0; }
阅读全文
0 0
- poj 2362Square(DFS)
- POJ 2362 Square dfs
- POJ 2362 Square DFS
- dfs Square poj 2362
- DFS POJ 2362 Square
- POJ 2362 Square [DFS]
- 【POJ】2362 Square(DFS)
- TOJ 1398 POJ 2362 Square DFS
- poj 2362 || zoj 1909 Square (DFS+剪枝)
- POJ 2362 HDOJ 1518 Square DFS剪枝
- POJ 2362 Square(DFS回溯构造)
- POJ 2362 Square 经典DFS回溯
- hdu 1518||poj 2362 Square(dfs)
- POJ 2362 Square (DFS +剪枝)
- POJ 2362 && HDU 1518 Square(dfs)
- poj 2362 Square(dfs, 剪枝)
- POJ 2362 square(DFS范例题)
- Square DFS poj&hoj
- 为什么要源码编译
- HDU 6165 FFF at Valentine(强连通 缩点 17多校第九场)
- iOS进阶之巧用IBInspectable属性和IB_DESIGNABLE宏定义
- linux系统性安装mysql服务器(基于CentOS)
- HTML&CSS基础篇之十五:表格&列表
- 【POJ】2362 Square(DFS)
- 完整css选择器总结
- 1010:“水仙花数”问题1
- libsvm正则化和liblinear正则化的区分
- 我的工作——认知
- explain mysql详解
- Mybatis的<where><foreach><set><trim>等标签详解
- banner图片轮播
- lua解析树形字符串拼接问题