剪枝的定义&&hdu1010

半年前在POJ上遇到过一次剪枝的题目,那时觉得剪枝好神秘。。。今天在网上查了半天资料,终于还是摸索到了一点知识,但是相关资料并不多,在我看来,剪枝是技巧,而不是方法,也就是说,可能一点实用的小技巧,让程序可以少判断一点,这就是剪枝,剪枝无处不在,

搜索的进程可以看作是从树根出发,遍历一棵倒置的树—-搜索树的过程。而所谓的剪枝,顾名思义,就是通过某种判断,避免一些不必要的遍历过程,形象的说,就是减去了搜索树中的某些“枝条”,故称剪枝。
(杭电课件上是这么说的:即剪去解答树上已被证明不可能存在可行解或最优解的子树.)


然采用了搜索,剪枝就显得十分的必要,即使就简简单单的设一个槛值,或多加一两条判断,就可对搜索的效率产生惊人的影响。例如N后问题,假如放完皇后再判
断,则仅仅只算到7,就开始有停顿,到了8就已经超过了20秒,而如果边放边判断,就算到了10,也没有停顿的感觉。所以,用搜索一般都就要剪枝。

剪枝至少有两方面,一是从方法上剪枝,如采用分枝定界,启发式搜索等,适用范围比较广;二是使用一些小技巧,这类方法适用性虽不如第一类,有时甚至只能适用一道题,但也十分有效,并且几乎每道题都存在一些这样那样的剪枝技巧,只是每题有所不同而已。

剪枝的原则:
1.正确性:必须保证不丢失正确的结果。
2.准确性:能够尽可能多的减去不能通向正解的枝条
3.高效性:在很多时候,为了加强优化的效果,我们会增加一些判断,这样对程序效率也带来了副作用,所以要考虑剪枝的高效性,否则得不偿失。

最后说一下:剪枝在搜索中用的是非常的广泛的。

参照杭电课件第47页一句话:
ACMer们:
为了ACM事业,努力地剪枝吧!!

题目我不多说,HDOJ 1010就是一个很好的剪枝题目。

另外,杭电的课件–搜索篇里面也讲了搜索与剪枝。

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 91553    Accepted Submission(s): 24900

Problem Description

The
doggie found a bone in an ancient maze, which fascinated him a lot.
However, when he picked it up, the maze began to shake, and the doggie
could feel the ground sinking. He realized that the bone was a trap, and
he tried desperately to get out of this maze.

The maze was a
rectangle with sizes N by M. There was a door in the maze. At the
beginning, the door was closed and it would open at the T-th second for a
short period of time (less than 1 second). Therefore the doggie had to
arrive at the door on exactly the T-th second. In every second, he could
move one block to one of the upper, lower, left and right neighboring
blocks. Once he entered a block, the ground of this block would start to
sink and disappear in the next second. He could not stay at one block
for more than one second, nor could he move into a visited block. Can
the poor doggie survive? Please help him.

Input

The
input consists of multiple test cases. The first line of each test case
contains three integers N, M, and T (1 < N, M < 7; 0 < T <
50), which denote the sizes of the maze and the time at which the door
will open, respectively. The next N lines give the maze layout, with
each line containing M characters. A character is one of the following:

‘X‘: a block of wall, which the doggie cannot enter;
‘S‘: the start point of the doggie;
‘D‘: the Door; or
‘.‘: an empty block.

The input is terminated with three 0‘s. This test case is not to be processed.

Output

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

Sample Input

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

Sample Output

NO
YES

Author

ZHANG, Zheng

Source

ZJCPC2004

Recommend

JGShining   |   We have carefully selected several similar problems for you:  1016 1242 1072 1026 1240

#include<iostream>
#include<math.h>
using namespace std;
char s[10][10];
int ax,ay,bx,by,n,m,k;
int t[4][2]={1,0,-1,0,0,1,0,-1},vist[10][10],flag;

void dfs(int x,int y,int count){
    int i,mx,my;
    if(x==bx&&y==by){
        if(k==count)
            flag=1;
        return;
    }
    if(count>=k)
        return;
    if(s[x][y]!=‘X‘){
        for(i=0;i<4;i++){
            mx=x+t[i][0];
            my=y+t[i][1];
            if(s[mx][my]!=‘X‘&&mx>=1&&mx<=n&&my>=1&&my<=m&&!vist[mx][my]){
                vist[mx][my]=1;
                dfs(mx,my,count+1);
                vist[mx][my]=0;
                if(flag)                       //注意,在找到了目标之后,就不需要再找!以往编写dfs时,没有注意这点
                    return;
            }
        }
    }
}
int main(){
    while(scanf("%d%d%d",&n,&m,&k)>0&&(n+m+k)){
        int i,count;
        for(i=1;i<=n;i++){
            getchar();
            for(int j=1;j<=m;j++){
                scanf("%c",&s[i][j]);
                if(s[i][j]==‘S‘){
                    ax=i;
                    ay=j;
               }
                if(s[i][j]==‘D‘){
                    bx=i;
                    by=j;
               }
            }
        }
        getchar();
        memset(vist,0,sizeof(vist));
        if(abs(ax-bx)+abs(ay-by)>k||(ax+bx+ay+by+k)%2==1){//剪枝
            printf("NO\n");
            continue;
        }
        vist[ax][ay]=1;
        flag=0;
        count=0;
        dfs(ax,ay,count);
        if(flag==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
时间: 2024-10-25 18:14:28

剪枝的定义&&hdu1010的相关文章

COJ 1686:记忆化搜索

看了N遍才看懂题意... 题意:给N个区间,每次能向左或向右走区间长度这么多,问能不能每次都在[0,m]这个范围内 思路:爆搜是不行的..这里把状态记录一下能剪枝很多 定义:s[pos][now]=-1\0\1分别为“时间now走到pos这个位置是不能.没试过.能走到终点而满足要求” pos最大是100,now最大是1e4,开得下.然后搜一下就好了 #include"cstdio" #include"queue" #include"cmath"

深度优先搜索---一个懵逼的东东

深度优先搜索属于图算法的一种,英文缩写为DFS即Depth First Search.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次.(来自百度百科) 先解释以下深度: 深度指在搜索的过程中沿着一条路一直向下进行,直到这条路没有下一个节点停止,然后返回到上一步接着进行上述操作 所以深度优先搜索的整体结构就是: 1.递归2.剪枝 可能这样说有点不清楚,举个例子 打印1-4的全排列开头为1和2的所有序列; 1 2 3 4 1 2 4 3 1 3 2 4 1 3

HDU1010 Tempter of the Bone 【DFS】+【剪枝】

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70895    Accepted Submission(s): 19535 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU1010 DFS+剪枝

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 110290    Accepted Submission(s): 29967 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU1010 Tempter of the Bone(回溯 + 剪枝)

题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D’代表门,‘.’代表可行走的地方,小狗每次可以选择往周围的四个方向行走,问这个小狗能否正好T步找到门. 思路: 利用回溯 + 剪枝,这道题剪枝特别重要. 剪枝一: 可以把图看成这样: 1 0 1 0 10 1 0 1 01 0 1 0 10 1 0 1 01 0 1 0 1 则假设从点 a(i + j,横纵坐标之和) 走到点 b(i + j) ,如果 a 和 b 的奇偶性相同,那么从 a 到 b 必须是偶数步

HDU--1010 Tempter of the Bone(深搜+奇偶剪枝)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1010 我认为的剪枝就是在本来的代码中加入一些附加条件使之不去进行多余的计算,防止超时 奇偶剪枝的知识链接 AC代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int n,m,t,k,flag,starex,starey,endx,endy

Tempter of the Bone —HDU1010(DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 102071    Accepted Submission(s): 27649 Problem Description The doggie found a bone in an ancient maze, which fascinated him

hdu1010 dfs+路径剪枝

题意:用一个案例来解释 4 4 5 S.X. ..X. ..XD .... 在这个案例中,是一个4*4的地图, . 表示可走的地方, X 表示不可走的地方,S表示起始点,D表示目标点,没走到一个点之后,这个点就不可走啦.起始点的时间是0,那么问 在 时间刚好是 5 的时候能不能走到 D ,对于这个案例来说显然不能 题解:这里学习到了一个剪枝 : 路径剪枝,是看了这个博客的解释,结合题目讲了两个剪枝,讲得很详细:http://acm.hdu.edu.cn/forum/read.php?tid=61

hdu1010 Tempter of the Bone(DFS+剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 90716    Accepted Submission(s): 24683 Problem Description The doggie found a bone in an ancient maze, which fascinated him a