【POJ 2248】 Addition Chain

【题目链接】

           http://poj.org/problem?id=2248

【算法】

           搜索剪枝

           剪枝1 : 优化搜索顺序,从大到小枚举

           剪枝2 : Ai + Aj可能相等,只需搜一次即可

           剪枝3 : 通过观察发现 : m <= 10,可以用迭代加深搜索

【代码】

            

#include <algorithm>  
#include <bitset>  
#include <cctype>  
#include <cerrno>  
#include <clocale>  
#include <cmath>  
#include <complex>  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <ctime>  
#include <deque>  
#include <exception>  
#include <fstream>  
#include <functional>  
#include <limits>  
#include <list>  
#include <map>  
#include <iomanip>  
#include <ios>  
#include <iosfwd>  
#include <iostream>  
#include <istream>  
#include <ostream>  
#include <queue>  
#include <set>  
#include <sstream>  
#include <stdexcept>  
#include <streambuf>  
#include <string>  
#include <utility>  
#include <vector>  
#include <cwchar>  
#include <cwctype>  
#include <stack>  
#include <limits.h> 
using namespace std;

int i,n,step;
int ans[15];

inline bool dfs(int dep)
{
        int i,j;
        bool visited[110];
        if (dep > step)
        {
                if (ans[step] == n) return true;
                else return false;        
        }    else
        {
                memset(visited,false,sizeof(visited));
                for (i = dep - 1; i >= 1; i--)
                {
                        for (j = i; j >= 1; j--)
                        {
                                if (ans[i] + ans[j] <= ans[i-1]) break;
                                if (ans[i] + ans[j] > n || visited[ans[i]+ans[j]]) continue;
                                visited[ans[i]+ans[j]] = true;
                                ans[dep] = ans[i] + ans[j];
                                if (dfs(dep+1)) return true;    
                        }    
                }
                return false;
        }
}

int main() 
{
        
        while (scanf("%d",&n) != EOF && n)
        {
                ans[1] = 1;
                for (i = 1; i <= 10; i++)
                {
                        step = i;
                        if (dfs(2)) break;
                }
                for (i = 1; i <= step; i++) printf("%d ",ans[i]);
                printf("\n");
        }
        
        return 0;
    
}

猜你喜欢

转载自www.cnblogs.com/evenbao/p/9263869.html