第五章作业

  1. 你对回溯算法的理解:回溯法可以系统的搜索一个问题的所有解或者任一解。在解空间中,按深度优先策略,从根节点出发搜索解空间树。适用于组合数较大的问题。

  2. 请说明“子集和”问题的解空间结构和约束函数:第五章作业_第1张图片

     

    约束函数:用于扩展节点处剪去不满足约束的子树,可以提高算法的效率。插入代码如下:

    #include 
    
    using namespace std;
    
    const int Max = 1e6+9;
    const int N = 1e4+9;
    
    typedef long long ll;
    
    int n,m;
    int a[N],p[N];
    int sum,rest;
    
    bool backliu(int x){
        if(sum==m)return true;
        if(x>=n)return false;
        rest-=a[x];
        if(sum+a[x]<=m){
    
            sum+=a[x];
            p[x]=1;
            if(backliu(x+1))return true;
            p[x]=0;
            sum-=a[x];
        }
        if(sum+rest>=m){
            if(backliu(x+1))return true;
        }
        rest+=a[x];
        return false;
    }
    
    
    int main(){
        cin>>n>>m;
        for(int i=0;i){
            cin>>a[i];rest+=a[i];
        }
        if(backliu(0)){
            for(int i=0;i){
                if(p[i])cout<" ";
            }
        }
        else cout<<"No Solution!"<<endl;
    }

     

  3. 请说明在本章学习过程中遇到的问题及结对编程的情况:在学习剪枝函数的时候有一些不明白,对严格型限界函数和宽松型限界函数不是很会用,经过讨论后有深一步的了解。

你可能感兴趣的:(第五章作业)