dfs算法

1.dfs应用1

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7, 
A solution set is: 
[
  [7],
  [2, 2, 3]
]


// From: http://www.tuicool.com/articles/2Y3iay3

// http://blog.csdn.net/eddy_liu/article/details/50452430
// 见运行图:http://blog.csdn.net/xiaobaohe/article/details/7897966

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {

        int len = candidates.size();

        if( len <= 0 )
        {
            return res;
        }

        sort( candidates.begin(), candidates.end() );  //先对C中候选数升序排序,为后面的剪枝做准备
        source = candidates;
        vector<int> tmp;
        dfs( tmp, target, 0 );

        return res;

    }


    private:
        vector< vector<int> > res;
        vector<int> source;

        int mysum( vector<int> &ve )
        {
            int sum = 0;

            int len = ve.size();

            for( int i = 0; i < len; ++i )
            {
                sum += ve[i];
            }

            return sum;
        }


        void dfs( vector<int> tmp, int tag, int l )
        {

            int total = mysum(tmp);

            if( total == tag )
            {
                res.push_back(tmp);
                return;
            }
            else if( total > tag ) //剪枝
            {
                return;
            }
            else
            {
                for( int i = l; i < source.size(); ++i ) //因为C中每个数可以选多次,所以i从l开始,而不是l+1
                {
                    tmp.push_back(source[i]);
                    dfs( tmp, tag, i );
                    tmp.pop_back();  //回溯,恢复tmp状态
                }
            }


            return;
        }


};


2. 

class Solution {
public:


    int mysum( vector<int> &ve )
    {
        int sum = 0;

        int len = ve.size();

        for( int i = 0; i < len; ++i )
        {
               sum += ve[i];
        }

        return sum;
    }

    void dfs( vector<int> &candidates, vector<int> save, int depth, int target, vector< vector<int> > &result )
    {

        int total = mysum(save);
        int len = candidates.size();

        if( total == target )
        {
            result.push_back(save);
            return;
        }

        if( total > target )
        {
            return;
        }
        else 
        {
            for( int i = depth; i < len; i++ )    
            {
                if( i > 0 && candidates[i] == candidates[ i - 1 ] ) //已经遍历过了
                {
                   // continue;
                }

                save.push_back( candidates[i] );
                dfs( candidates, save, i + 1, target, result );
                save.pop_back();
            }
        }


        return;
    }

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {

        vector< vector<int> > result;
        int len = candidates.size();

        if( 0 == len )
        {
            return result;
        }

        if( 1 == len )
        {
            vector<int> tmp;

            if( candidates[0] == target )
            {
                tmp.push_back(target);
                result.push_back(tmp);

            }

            return result;
        }

        sort( candidates.begin(), candidates.end(), less<int>() );

        vector<int> tmp;
        dfs( candidates, tmp, 0, target, result );
        return result;

    }
};



//
class Solution {
public:


    int mysum( vector<int> &ve )
    {
        int sum = 0;

        int len = ve.size();

        for( int i = 0; i < len; ++i )
        {
               sum += ve[i];
        }

        return sum;
    }

    void dfs( vector<int> &candidates, vector<int> save, int depth, int target, vector< vector<int> > &result )
    {

        int total = mysum(save);
        int len = candidates.size();

        if( total == target )
        {
            result.push_back(save);
            return;
        }

        int pre = 0;

        if( total > target )
        {
            return;
        }
        else 
        {
            for( int i = depth; i < len; i++ )    
            {
                if( i > depth )
                {
                    if( candidates[i] == pre ) //已经遍历过了
                    {
                        continue;
                    }
                }

                save.push_back( candidates[i] );
                pre = candidates[i];
                dfs( candidates, save, i + 1, target, result );
                save.pop_back();
            }
        }


        return;
    }

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {

        vector< vector<int> > result;
        int len = candidates.size();

        if( 0 == len )
        {
            return result;
        }

        if( 1 == len )
        {
            vector<int> tmp;

            if( candidates[0] == target )
            {
                tmp.push_back(target);
                result.push_back(tmp);

            }

            return result;
        }

        sort( candidates.begin(), candidates.end(), less<int>() );  //从小到大排序

        vector<int> tmp;
        dfs( candidates, tmp, 0, target, result );
        return result;

    }
};


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页