关于DFS心得:

1、利用结构体,记录mark和题目要求的基本属性。

2、用到递归,使用递归时注意要设置出口,即符合要求时return,注意对递归的理解,对于不同情况可能要传递不同的参数,但出口都是一样的。

3、在DFS时可以剪枝,即对明显不符合条件的情况(基本判断,比如正方形的四边一样长,三角形的两边大于第三边)直接排除,不参与递归,在剪枝的时候注意正确的剪枝。所以在DFS超时时可以考虑剪枝。

4、在递归发现不符合条件需要返回上一层的时候,要将不符合条件的元素flag消除(DFS、BFS的区别之一)。

例题:

Problem 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

3

4 1 1 1 1

5 11 20 30 40 50

8 1 7 2 6 4 4 3 5

.

Sample Output

yes

no

yes

大神滴代码。。。。。。。。。。。。

//剪支62MSAC
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std; int n,cnt,sum; struct node
{
int lenth;
int mark;
}stick[25]; int cmp(node a,node b)
{
return a.lenth>b.lenth;
} int dfs(int len,int count,int l,int pos)
{
if(count==4)return 1;
for(int i=pos;i<n;i++)
{
if(stick[i].mark)continue; if(len==(stick[i].lenth+l))
{
stick[i].mark=1;
if(dfs(len,count+1,0,0))
return 1;
stick[i].mark=0;
return 0;
}
else if(len>(stick[i].lenth+l))
{
stick[i].mark=1;
l+=stick[i].lenth;
if(dfs(len,count,l,i+1))
return 1;
l-=stick[i].lenth;
stick[i].mark=0;
if(l==0) return 0;
while(stick[i].lenth==stick[i+1].lenth)i++;
}
}
return 0;
}
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d",&n);
cnt=sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&stick[i].lenth);
sum+=stick[i].lenth;
stick[i].mark=0;
}
sort(stick,stick+n,cmp);
if(sum%4||n<4)
{
cout<<"no"<<endl;
continue;
}
cnt=sum/4;
if(dfs(cnt,0,0,0))
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
return 0;
} #include <iostream>
#include <algorithm>
using namespace std;
int n,s,k,a[22],visit[22]={0};? //a数组用来记录那一组数据,visit数组用来记录当前组的数据是否可用
void DFS(int sum,int x,int d)? //sum是当前计算的和,等于正方形边长的话就配好了一条边了,x是记录当前配好了多少条边,d是上一次匹配到了哪里,是剪枝的关键
{
if(k||x==3)? //当配上了三边的时候就说明能构成正方形了,如果已经能配成正方形就直接返回了
{
k=1;return;? //k=1标记已经能够成正方形了
}
int i;
for(i=d;i<n;i++)? //从上次断点开始匹配
{
if(k)return;? //已经构成正方形就直接返回
if(!visit[i])? //询问这第i组数据是否可用
if(sum+a[i]==s)? //如果正好配成边
{
visit[i]=1;
DFS(0,x+1,0);? //初始化s和d并已经配成的边+1
visit[i]=0;
}
else if(sum+a[i]<s)? //如果比边小
{
visit[i]=1;
DFS(sum+a[i],x,i+1);? //传送断点和此时的值
visit[i]=0;
}
}
}
bool cmp(int a,int b)
{
return a<b;
}
int main(void)
{
int m,i;
cin>>m;
while(m--&&cin>>n)
{
for(i=s=0;i<n;i++)
cin>>a[i],s+=a[i];? //统计正方形周长
sort(a,a+n,cmp);? //从小到大排序
if(s%4==0)? //判断周长是否是正方形的形式
{
s/=4;? //s变成记录正方形的边长
k=0;? //初始化k
DFS(0,0,0);? //和为0,已配成的边数量为0,断点是起点
if(k)cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
else cout<<"no"<<endl;
}
return 0;
}

DFS初级剪枝及心得的更多相关文章

  1. hdoj--1010<dfs+奇偶剪枝>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况 ...

  2. hdu1010 Tempter of the Bone —— dfs+奇偶性剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  3. HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意: 输入 n m t,生成 n*m 矩阵,矩阵元素由 ‘.’ 'S' 'D' 'X' 四 ...

  4. hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  5. hdu 1728:逃离迷宫(DFS,剪枝)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. [HDOJ5952]Counting Cliques(DFS,剪枝)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5952 题意:求图中规模为s的团的个数. DFS+剪枝,姿势不好很容易TLE啊. #include &l ...

  8. hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) && hdu-1015 Safecracker(简单搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心 ...

  9. hdu-1242 dfs+各种剪枝

    思路: 可以和1010一个思路.这个抽象的说就是让你求给定图中两点的最短距离,其实dfs的题目能变化的地方就在“终点的回溯处”,在我们到达终点后,再判断一些附加的值(本题里是最短距离是否更新),从而得 ...

随机推荐

  1. __getattr__,__getattribute__????

    class Foo(object): def __getattr__(sel,item): print('y') def __getattribute(self,item): print('x') o ...

  2. mysql启动报错,与selinux相关

    mysql启动报错,与selinux相关 如果遇到报错,可能的情况是 selinux 的关系,可以安装 setroubleshoot-server 工具,使用 sealert -a /var/log/ ...

  3. python JSON性能测试与simplejson对比

    简单测试了一下,如果用JSON,也就是python2.6以上自带的json处理库,效率还算可以: 1K的数据,2.9GHz的CPU,单核下每秒能dump:36898次.大约是pyamf的5倍.但数据量 ...

  4. vue项目里的日期格式化(摘录)

    export function formatDate (date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date. ...

  5. EF写INNER JOIN 链接

    面对多表的查询,一般都是多表连接后下面再写条件,但是有一种写法可以提升一下EF生成的语句的效率 首先先去查询每一个表,把每一个表对应的条件附加上去,注意:过滤数据最多的条件放在首先位置 var lt ...

  6. 使用selenium 检测js报错

    背景:接到一个需求,想检测页面是否能检测js报错,何为js报错,如下图所示,在控制台中,使用console,如果有js报错,就会出现错误 如何检测,简单版操作,打开一个url,使用manage获取浏览 ...

  7. Family Gathering at Christmas(思维题)

    Family Gathering at Christmas 时间限制: 1 Sec  内存限制: 128 MB提交: 13  解决: 4[提交] [状态] [讨论版] [命题人:admin] 题目描述 ...

  8. Spring MVC的一些学习笔记-入门配置和HttpMessageConverter

    1.初步配置 [1]. 配置web.xml以及在web.xml中配置DispatcherServlet: <context-param> <param-name>context ...

  9. 【造轮子】开发vue组件库MeowMeowUI

    项目示例 github ​ 1. 创建项目 # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新项目 $ vue in ...

  10. cookie和session的介绍

    1.cookie和session cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此产生cookie. cookie的工作原理是:由服务器产生 ...