POJ2362:Square(DFS)
来源:互联网 发布:java 邮件发送多个人 编辑:程序博客网 时间:2024/05/06 18:05
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
题意:看给出的所有棍子能不能组成正方形
思路:直接搜索每条边即可
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int a[250],vis[205],sum,l,n;int dfs(int x,int pos,int len){ if(x == 3) return 1; int i; for(i = pos; i>=0; i--) { if(!vis[i]) { vis[i] = 1; if(len+a[i]<l) { if(dfs(x,i-1,len+a[i])) return 1; } else if(len+a[i]==l) { if(dfs(x+1,n-1,0)) return 1; } vis[i] = 0; } } return 0;}int main(){ int t,i,j; scanf("%d",&t); while(t--) { sum = 0; scanf("%d",&n); for(i = 0; i<n; sum+=a[i],i++) scanf("%d",&a[i]); l = sum/4; memset(vis,0,sizeof(vis)); sort(a,a+n); if(l*4!=sum || n<4 || l<a[n-1])//剪枝 { printf("no\n"); continue; } if(dfs(0,n-1,0)) printf("yes\n"); else printf("no\n"); } return 0;}
- POJ2362:Square(DFS)
- POJ2362 Square(dfs)
- POJ2362 Square(dfs)
- poj2362 Square DFS剪枝
- poj2362——Square(深搜dfs)
- hdu1518 Square | ZOJ1909 | POJ2362 (dfs + 剪枝)
- poj2362 - Square
- poj2362--square
- POJ2362 Square
- POJ2362 Square
- poj2362 Square
- POJ2362:Square
- ZOJ1909 POJ2362 HDU1518 Square,很经典的DFS+剪枝!
- HDU1518 & POJ2362 & ZOJ1909 Square(DFS,剪枝是关键呀)
- poj2362 dfs
- POJ2362(DFS)
- poj2362——Square
- HDU1518 & POJ2362 & ZOJ1909 Square
- UVA 658 - It's not a Bug, it's a Feature!(最短路)
- delphi中DateTimePicker同时修改日期和时间的实现
- PL/SQL语言环境设置
- c语言程序的存储区域与const关键字的使用方法
- 求帮助 http://bbs.tianya.cn/post-no04-2361448-1.shtml
- POJ2362:Square(DFS)
- unity3d editor splitview
- JAVA的零碎知识1
- 初代 iPhone:那道不清说不尽的故事
- Java反射机制(Reflection )
- Oracle EBS中删除可执行、并发程序
- 客户、订单知识
- 编译型语言VS解释型语言
- 善于总结运用才能更深刻的了解你所学习的东西