Search pruning

Pruning, it is built nirvana search. Whether BFS or DFS, the search process will inevitably repeat the calculation or unwanted stuff, we can cut directly to it, it does not count, this process was aptly called pruning.

@(List)

in principle

Three words:
correctness, accuracy, efficiency.

classification

Feasibility pruning

This is the simplest pruning, and if the answer is not currently legal, direct return, everyone can grasp, basically every search will add to, very popular.

Example: horse traversal

Optimality pruning

This means pruning in the calculation of the answer, the answer that best big answer than currently known, direct return. easier.
We use a function to estimate the answer of 'lower bound' under the conditions at this time, it will be compared with the already launched answer, if the answer better than the current small, can be cut off.

Example: dangerous work

## memory search

Similar memory search DP, i.e. the calculated values ​​stored up to then considered later can be used directly in, the time complexity will be reduced a lot.

Example: recursion, the number of combinations enhanced version


Title:
the Description
compile a recursive procedure, find the number of combinations C (n, m)
is known C (n, m) = C (n--. 1, m) + C (n--. 1,. 1-m);
the Input
two line numbers N, M, which is less than equal to 5000
the Output

Program number 1000000007%
the Sample the Input

1 1

Sample Output

1
_____

Ideas:
First special sentence, all the special cases all written out, then began to search the memory, if this value is calculated that will be placed directly on the array of values taken out, assign a value to it, it is not counted direct count, count over again into the array, during the recursion.

Code:

#include<bits/stdc++.h>
using namespace std;
long long f[5010][5010];//记忆的数组
long long dfs(int n,int m){
    if(n<m){
        return 0;
    }
    if(m==0||m==n){
        return 1;
    }//特判
    int a,b;
    if(f[n-1][m]!=0){
        a=f[n-1][m];
    }else{
        a=dfs(n-1,m);
    }//算C(n-1,m)
    if(f[n-1][m-1]!=0){
        b=f[n-1][m-1];
    }else{
        b=dfs(n-1,m-1);
    }//算C(n-1,m-1)
    return f[n][m]=(a+b)%1000000007;
}
int main(){
   int n,m;
   cin>>n>>m;
   cout<<dfs(n,m);
   return 0;
}

to sum up

In general, pruning is a skill that comes with the search, so the search is not only violence, there is still violence, which is popular because search elements.

Guess you like

Origin www.cnblogs.com/herobrine-life/p/11229945.html