zoukankan      html  css  js  c++  java
  • 【解题报告】 加成序列

    【解题报告】 加成序列

    题目:加成序列

    解题思路:

    dfs+剪枝+迭代加深

    这道题我们可以看出任何序列的长度都在10左右,所以这道题我们可以很好地看出是用迭代加深来解决问题

    所以我们为了让序列中的数尽快逼近n,在枚举i和j时,可以从大到小枚举

    然后我们还需要一个判重,因为不让两个数加起来等于第三个数多次

    AC代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int maxn=110;
    int n;
    int path[maxn];
    bool dfs(int u,int depth)
    {
    	if(u==depth)
    	return path[u-1]==n;
    	bool st[maxn]={false};
    	for(int i=u-1;i>=0;i--)
    	{
    		for(int j=i;j>=0;j--)
    		{
    			int s=path[i]+path[j];
    			if(s>=path[u-1]&&s<=n&&!st[s])
    			{
    				st[s]=true;
    				path[u]=s;
    				if(dfs(u+1,depth))
    				return true;
    			}
    		}
    	}
    	return false;
    }
    int main()
    {
    	while(cin>>n&&n)
    	{
    		int depth=1;
    		path[0]=1;
    		while(!dfs(1,depth))
    		depth++;
    		for(int i=0;i<depth;i++)
    		cout<<path[i]<<" ";
    		cout<<endl;
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    2016.6.26考试
    爆搜。。。。。。。
    9.18目标
    9.17水题总结
    9.16测试
    9.10考试
    jzoj P1163 生日派对灯
    9.04考试总结
    8/8刷题记录
    a[i++]
  • 原文地址:https://www.cnblogs.com/wweiyi2004/p/11407510.html
Copyright © 2011-2022 走看看