uva529 iterative deepening + essential pruning

Source: Internet
Author: User

main.cpp//529////Created by Fangpin on 15/3/14.//Copyright (c) 2015 Fangpin. All rights reserved.//#include <iostream> #include <cstring>using namespace Std;int a[10005]={1,2},n;bool            ok;void DFS (int limit,int d) {if (d==limit+1) {if (a[limit]==n) {//printf ("%d%d", n,d);            Ok=true;                for (int i=0;i<d;++i) {if (i) printf ("");            printf ("%d", a[i]);                    } printf ("\ n");    } return;    } if ((a[d-1]<< (limit-d+1)) <n) return; for (int i=d-1;i>=0 &&!ok;--i) {for (int j=d-1;j>=i &&!ok;--j) {if (a[i]+a[j]<=a [D-1])            Break            A[D]=A[I]+A[J];        DFS (LIMIT,D+1); }}}int Main (int argc, const char * argv[]) {//Insert code here...//freopen ("OUT.txt", "w", stdout);//For (n=        1;n<=10000;++n) {while (scanf ("%d", &n), N) {if (n==1) puts ("1"); else if (n==2) puts ("1 2");                else{for (int i=2;i<=n;++i) {ok=false;                DFS (i,2);                if (OK) {break; }}}} return 0;}

Main topic:

Give a number n and then output a sequence of 1 to n with a minimum number of elements (there may be multiple scenarios, one of which can be output).

Where for the number of K AK, its value equals Ai+aj ().

Analysis and Summary:

This problem is a typical iterative deepening search + the topic of subtraction.

Iterative deepened searches (ids,iterative deepening search):

Iterative deepening search is essentially a deep-first search that limits the nether. That is, first allow depth-first search for K-level search tree, if not found feasible solution, and then repeat the above steps after k+1 search, until the search for a viable solution.

In the iterative deepening search algorithm, successive depth-first searches are introduced, and each depth constraint is added 1 at a successive point until the target is searched.

The iterative deepening search algorithm is the depth-first search of the imitated breadth-first search. It can satisfy the linear storage requirement of depth-first search and ensure the discovery of a minimum depth target node.

From a practical point of view, the effect of iterative deepening search is not much slower than breadth-first search, but the spatial complexity is the same as the depth-first search, which is much smaller than the breadth-first search.

For this problem, you can first find that the minimum number of elements can be reached N. According to the greedy strategy, for each element of the value, choose to make it equal to a number of twice times, that is, for each Ai = Ai-1 + Ai-1, when the ai>=n jump out of the loop, to get the minimum number of elements.

Then the iteration deepens the search, starting with the minimum number of steps. Then use some of the techniques to reduce the branch.



uva529 iterative deepening + essential pruning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.