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 ...
随机推荐
- android基础知识点复习之短信发送
界面布局: activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...
- VMware 虚拟机(linux)增加根目录磁盘空间
今天查看学校的监控报修系统,不能访问了!!!系统运行很慢,用top命令查看发现内存使用率90%,用"df -h ”查看“/”目录使用率已达到80%,导致系统运行很慢.我用以下方法扩大根目录磁 ...
- Word试卷文档模型化解析存储到数据库
最近在搞一套在线的考试系统,有许多人反映试题的新增比较麻烦(需要逐个输入),于是呼就整个了试卷批量导入了 poi实现word转html 模型化解析html html转Map数组 Map数组(数组的操作 ...
- C++primer 9.43
题目要求:编写一个函数,接受三个string参数s,oldVal和newVal.使用迭代器及insert和erase函数将s中所有oldVal替换为newVal.测试你的程序,用它替换通用的简写形式, ...
- set集合容器
set集合容器几条特点 1.它不会重复插入相同键值的元素,而采取忽略处理 2.使用中序遍历算法,检索效率高于vector.deque.list容器,在插入元素时,会自动将元素按键值从小到大排列 3 ...
- Unity 的ICallHander在C#项目中实现注入
项目包引用: Install-Package Unity.Interception 创建项目的接口和其实类: public interface ICalculator { double Calcula ...
- Ansible8:Playbook循环【转】
在使用ansible做自动化运维的时候,免不了的要重复执行某些操作,如:添加几个用户,创建几个MySQL用户并为之赋予权限,操作某个目录下所有文件等等.好在playbook支持循环语句,可以使得某些需 ...
- Redis如何保存数组和对象
个人建议使用PHP自带的序列化函数serialize和unserialize函数 我们可以封装一个自己的Redis类 <?php class MyRedis{ private static $h ...
- Android编译环境(1) - 编译Native C的模块
Android编译环境本身比较复杂,且不像普通的编译环境:只有顶层目录下才有Makefile文件,而其他的每个component都使用统一标准的Android.mk. Android.mk文件本身是比 ...
- 【转】linux ls -l的详解
原文:http://blog.csdn.net/sjzs5590/article/details/8254527 以root的家目录为例: 可以看到,用ls -l命令查看某一个目录会得到一个7个字段的 ...