POJ2362《Square》题解
来源:互联网 发布:麦田的守望者 知乎 编辑:程序博客网 时间:2024/05/19 13:45
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
万恶的BZOJ几分钟内就封了,哎,只能先玩POJ了。
分析:dfs可以解决,因为数据量小,所以不用剪枝也能AC。
代码如下:
#include<iostream>#include<cmath>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int T,N,a[30],f[30];int maxx,minn,sum;bool DFS(int x,int num,int tot){if(tot==sum) num++,tot=x=0;if(minn+tot>sum) return 0;if(num==3) return 1;int last=-1;for(int i=x+1;i<=N;i++)if(!f[i]){if(a[i]==last)continue;f[i]=1;if(DFS(i,num,tot+a[i])) return 1;f[i]=0;last=a[i];}return 0;}int main(){scanf("%d",&T);while(T--){memset(f,0,sizeof(f));scanf("%d",&N);minn=10000000,maxx=sum=0;for(int i=1;i<=N;i++){scanf("%d",&a[i]);sum+=a[i];maxx=max(a[i],maxx);minn=min(a[i],minn);}sort(a+1,a+N+1,greater<int>());if(sum%4){printf("no\n");continue;}sum/=4;if(maxx>sum){printf("no\n");continue;}f[1]=1;if(DFS(1,0,a[1])) 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
- POJ2362:Square(DFS)
- HDU1518 & POJ2362 & ZOJ1909 Square
- POJ2362 Square 搜索
- POJ2362 Square(dfs)
- POJ2362 Square(dfs)
- poj2362 Square DFS剪枝
- poj2362——Square(深搜dfs)
- hdu1518 Square | ZOJ1909 | POJ2362 (dfs + 剪枝)
- Square poj2362深度优先搜索+剪枝
- POJ2362
- 最简单的基于libVLC的例子:最简单的基于libVLC的视频播放器
- 程序员必读书单
- 安装Nginx步骤
- 如何将Android数据库操作通用化(二)
- 最简单的基于libVLC的例子:最简单的基于libVLC的视频播放器(图形界面版)
- POJ2362《Square》题解
- Nginx 启动脚本/重启脚本
- 推荐大家一款在Mac上录制Gif动画的软件
- 最简单的基于libVLC的例子:最简单的基于libVLC的推流器
- 蓝桥杯--微生物增值
- iOS 自定义滑动返回和解决连续多次push,pop引起的crash问题
- edittext失去焦点
- bzoj3685题解(普通van Emde Boas树)
- JSONModel的原理