蒜头君手上有一些小木棍,它们长短不一,蒜头君想用这些木棍拼出一个等边三角形,并且每根木棍都要用到。 例如,蒜头君手上有长度为 11,22,33,33 的4根木棍,他可以让长度为11,22 的木棍组成一条边,另外 22跟分别组成 22 条边,拼成一个边长为 33 的等边三角形。蒜头君希望你提前告诉他能不能拼出来,免得白费功夫。

输入格式

首先输入一个整数 n(3 \le n \le 20)n(3≤n≤20),表示木棍数量,接下来输入 nn 根木棍的长度 p_i(1 \le p_i \le 10000)p​i​​(1≤p​i​​≤10000)。

输出格式

如果蒜头君能拼出等边三角形,输出"yes",否则输出"no"

样例输入1

5
1 2 3 4 5

样例输出1

yes

样例输入2

4
1 1 1 1

样例输出2

no

这也是典型的dfs:
对每一根木棍,要么加到a边,要么加到b边,要么加到c边。(因为所有的木棍都要使用)。dfs搜索每一个木棍即可。

#include<bits/stdc++.h>
using namespace std;
int n;
int l[];
int len;
bool dfs(int a,int b,int c,int id)
{
if(a==b&&b==c&&id==n) return true;
if(a>len||b>len||c>len) return false;
if(id>=n) return false;
if(dfs(a+l[id],b,c,id+)||dfs(a,b+l[id],c,id+)||dfs(a,b,c+l[id],id+)) return true;
return false;
}
int main()
{
cin>>n;
int sum=;
int maxn=-;
for(int i=; i<n; i++)
{
cin>>l[i];
sum+=l[i];
maxn=max(maxn,l[i]);
}
if((sum/*)!=sum||maxn>(sum/))
{
cout<<"no"<<endl;
return ;
}
int a,b,c;
a=b=c=;
len=sum/;
if(dfs(a,b,c,)) cout<<"yes"<<endl;
else cout<<"no"<<endl;
return ;
}

错误的搜索,之前学dfs的时候,习惯用循环,导致喜欢套用这个模板。。。。。。。。

#include<bits/stdc++.h>
using namespace std;
int n;
int a[];
int len;
bool vis[];
bool dfs(int sum,int index)
{
if(sum==len) return true;
if(sum>len) return false;
if(index>=n) return false;
if(dfs(sum+a[index],index+))
{
vis[index]=;
return true;
}
else
{
if(dfs(sum,index+)) return true;
}
return false;
}
int main()
{
cin>>n;
int sum=;
int maxn=-;
for(int i=; i<n; i++)
{
cin>>a[i];
sum+=a[i];
maxn=max(maxn,a[i]);
}
if((sum/*)!=sum||maxn>(sum/))
{
cout<<"no"<<endl;
return ;
}
len=sum/;
bool flag=false;
memset(vis,,sizeof(vis));
for(int i=; i<n; i++)
{
if(!vis[i])
{
flag=true;
if(!dfs(,i))
{
cout<<i<<endl;
for(int i=; i<n; i++)
cout<<vis[i]<<" ";
cout<<"no"<<endl;
return ;
}
flag=false;
}
} if(!flag) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}

等边三角形---dfs的更多相关文章

  1. 计蒜客 等边三角形 dfs

    题目: https://www.jisuanke.com/course/2291/182238 思路: 1.dfs(int a,int b,int c,int index)//a,b,c三条边的边长, ...

  2. 计蒜客的一道题dfs

    这是我无聊时在计蒜客发现的一道题. 题意: 蒜头君有一天闲来无事和小萌一起玩游戏,游戏的内容是这样的:他们不知道从哪里找到了N根不同长度的木棍, 看谁能猜出这些木棍一共能拼出多少个不同的不等边三角形. ...

  3. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  4. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  5. BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1352  Solved: 780[Submit][Stat ...

  6. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  7. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  8. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  9. 深度优先搜索(DFS)

    [算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...

随机推荐

  1. 关闭使用ShellExecute打开的进程!!!!!

    前言: 最近做一个项目使用到ShellExecute来打开一个带参数的外部exe文件,关闭时遇到不少问题,最终解决,总结如下. 对于关闭ShellExecute打开的进程窗口,网上比较多的是用Find ...

  2. OpenCV学习笔记(四十)——再谈OpenCV数据结构Mat详解

    原文:http://blog.csdn.net/yang_xian521/article/details/7107786 我记得开始接触OpenCV就是因为一个算法里面需要2维动态数组,那时候看cor ...

  3. [Grunt] Concatenating Your Javascript with grunt-contrib-concat

    Combine serval javascript files together. For angular project, make sure you add angular.min.js firs ...

  4. JQMobile引入外部CSS,JS文件

    使用CDN <!DOCTYPE html> <html> <head> <title>html5</title> <meta name ...

  5. 你在使用assetbundle时可能遇到的坑【转】

    在公司项目开发中,用到了assetbundle,由于是webplayer不像手机,流量限制几乎没有,所以场景都是用assetbundle打包后动态加载的,但是这个过程中,遇到不少坑: 1.Editor ...

  6. QtGui.QCalendarWidget

    A QtGui.QCalendarWidget provides a monthly based calendar widget. It allows a user to select a date ...

  7. D3js-对柱状图的增,删,排序

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  8. C# 判断是否是节假日

    1.引用Newtonsoft.Json.dll 2. /// <summary>        /// 判断是不是节假日,节假日返回true         /// </summar ...

  9. MySQL-锁研究

    隔离级别研究: http://www.cnblogs.com/JohnABC/p/3521061.html 表级:引擎 MyISAM, 理解为锁住整个表, 锁定期间, 其它进程无法对该表进行写操作, ...

  10. Redis总结(七)Redis运维常用命令(转载)

    redis 服务器端命令 redis 127.0.0.1:6380> time  ,显示服务器时间 , 时间戳(秒), 微秒数 1) "1375270361" 2) &quo ...