1317: Square(DFS+剪枝)
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".
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<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int data[250],state[250];
int ave,N,M,sum;
int dfs(int x,int pos,int len)
{
int i;
if(x==3)//如果有三条边都满足,结束
return 1;
for(i=pos;i>=0;i--){
if(!state[i]){
state[i]=1;
if(len+data[i]<ave){
if(dfs(x,i-1,len+data[i]))//i-1的作用是剪枝
return 1;
}
if(len+data[i]==ave){
if(dfs(x+1,M-1,0))
return 1;
}
state[i]=0;
}
}
return 0;
}
int main ()
{
scanf("%d",&N);
while(N--){
sum=0;
memset(state,0,sizeof(state));
//memset(data,0,sizeof(data));
scanf("%d",&M);
for(int i=0;i<M;sum+=data[i],i++)
scanf("%d",&data[i]);
ave=sum/4;
if(M<4||ave*4!=sum||ave<data[M-1]){
printf("no\n");
continue;
}
if(dfs(0,M-1,0))
printf("yes\n");
else
printf("no\n");
}
return 0;
}
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int data[250],state[250];
int ave,N,M,sum;
int dfs(int x,int pos,int len)
{
int i;
if(x==3)//如果有三条边都满足,结束
return 1;
for(i=pos;i>=0;i--){
if(!state[i]){
state[i]=1;
if(len+data[i]<ave){
if(dfs(x,i-1,len+data[i]))//i-1的作用是剪枝
return 1;
}
if(len+data[i]==ave){
if(dfs(x+1,M-1,0))
return 1;
}
state[i]=0;
}
}
return 0;
}
int main ()
{
scanf("%d",&N);
while(N--){
sum=0;
memset(state,0,sizeof(state));
//memset(data,0,sizeof(data));
scanf("%d",&M);
for(int i=0;i<M;sum+=data[i],i++)
scanf("%d",&data[i]);
ave=sum/4;
if(M<4||ave*4!=sum||ave<data[M-1]){
printf("no\n");
continue;
}
if(dfs(0,M-1,0))
printf("yes\n");
else
printf("no\n"); }
return 0;
}
1317: Square(DFS+剪枝)的更多相关文章
- HDU1518 Square(DFS,剪枝是关键呀)
Square Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- 【DFS+剪枝】Square
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/J [题意] 给定n个木棍,问这些木棍能否围成一个正方形 [Accepted] # ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- HDU 3410 Passing the Message
可以先处理出每个a[i]最左和最右能到达的位置,L[i],和R[i].然后就只要询问区间[ L[i],i-1 ]和区间[ i+1,R[i] ]最大值位置即可. #include<cstdio&g ...
- oracle账户锁定解决方法
今天进使用orcle中,发现系统中,system账户登录里提示账户被锁定 ,后来查了查资料,问题解决,方法如下: Microsoft Windows [版本 5.2.3790] (C) 版权所有 19 ...
- js键盘键值大全
原文地址:http://blog.csdn.net/avenccssddnn/article/details/7950524 js键盘键值 keycode 8 = BackSpace BackSpac ...
- selenium 百度登陆
using System;using OpenQA.Selenium;using OpenQA.Selenium.Firefox;//引用命名空间using System.IO; using Syst ...
- hdu_4734_F(x)(数位DP水)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:给你一个F(x)的定义,然后给你a,b,问你在0到b包括b有多少个数满足F(x)<= ...
- MVC 数据绑定
在做Asp.Net MVC项目中,都知道View负责页面展示数据或者提供页面收集数据,而所展示的数据或者收集的数据都是从Controller的Action中获取或提交到Controller的Actio ...
- NSNotificationCenter消息通信机制
作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的. 注册通知:即要在什么地方接受消息 [[NSNotificationCenter defaultCenter] ...
- 给table加边框的两种方法
<!DOCTYPE html><html><head><style>.a { border-spacing: 1px; background-color ...
- C#在Json反序列化中处理键的特殊字符
假设有如下Json 数据: 1.{ 2."id" : 1, 3."@value" : "this a @", 4."$p" ...
- ecshop 去版权
与官网通信的几个地方: 1,打开admin/templates/index.htm,查找并删除 <frameset rows="0, 0" framespacing=&quo ...