Ultraviolet A 11212-editing a book (iterative deepening search Ida *) iterative deepening search

Source: Internet
Author: User

Iterative deepening search

The first time I read it, I cannot understand it .. It's a lot of water, but there is no way to be quick bi.

Fortunately, a few of you have already done this question and have a certain understanding of the idea of deepening search through iteration, so they asked their opinions in some areas that they do not understand,

It is really helpful. Maybe you want to think for a long time before you can understand it. It will be very painful. Ask someone else's thoughts a little and click the previous direction. The rest of you will be able to understand it.

Iterations are deepened.

Increment answer (number of required steps or other steps) from scratch in the main function. This is called a "number of layers", which is also a "depth ". The words in the book are:

The maximum depth of enumeration is maxd, and only nodes whose depth cannot exceed maxd are considered at a time.

For questions that can be solved by using the backtracking method, but the depth of the answer tree is not significantly limited, you can consider using iterations to deepen the search.

Set the upper limit of depth to maxd (or set to Lim), and the depth of the current node n to Dep. An optimistic evaluation function is required to determine whether the function is necessary to continue DFS:

If, ideally, this node does not meet the final goal of the DFS, the pruning is required.


(Mom is just doing it. I wrote it here and found that my idea was a little unclear. I went back to study the Code if there were any vulnerabilities, and then I cried ...... I didn't understand it thoroughly, so I need to clarify it ~ So it took a long time to figure out that a certain step was used to do things. It was really amazing ...)


For the following code, I am confused about whether to re-recursion every time or continue the original state recursion .. I always thought it was a continuation of the original state (of course there were other misunderstandings ...... I was confused at the time .. Then I went back to the dormitory from Area D .. Then, I started to communicate with a child after washing. A child was so stupid that he was unhappy ..

Keke ,,

Then, after a child drove me to bed, I secretly caught a look at the code and added the following group of output to determine whether I had a cognitive error, then, what is really wrong... People just re-Traverse each time, and there is no continuation .. I mistakenly think of the content in that class as a global variable ..


Okay, those rows are all the same output ..

After all the problems are solved, the code is given as follows ..

This misunderstanding is still very painful. It's too much water for Nima .. Cainiao really is a cainiao. I don't know when it will evolve .....

#include <iostream>#include <cstdio>#include <cstring>using namespace std;class Board{public:    int a[16];    int h;    int n;    bool readIn()    {        int i;        scanf("%d",&n);        if (n == 0)            return false;        for (i=0; i<n; i++)        {            scanf("%d",a+i);        }        return true;    }    void printOut()    {        int i;        for (i=0; i<n; i++)        {            printf("%d ", a[i]);        }        printf("\n");        printf("h=%d\n",h);    }    void move(int s,int e,int p)    {        int t[16],i,j;        if (p < s)        {            for (i=0; i<p; i++)            {                t[i]=a[i];            }            for (j=s; j<=e; j++,i++)            {                t[i]=a[j];            }            for (j=p; j<s; j++,i++)            {                t[i]=a[j];            }            for (j=e+1; j<n; j++,i++)            {                t[i]=a[j];            }        }        else        {            for (i=0; i<s; i++)            {                t[i]=a[i];            }            for (j=e+1; j<p; j++,i++)            {                t[i]=a[j];            }            for (j=s; j<=e; j++,i++)            {                t[i]=a[j];            }            for (j=p; j<n; j++,i++)            {                t[i]=a[j];            }        }        memcpy(a,t,sizeof(t));    }    int getH()    {        int cnt,i;        cnt=0;        for (i=0; i<n-1; i++)        {            if (a[i+1] != a[i]+1)              cnt++;        }        if (a[i] != n)            cnt++;        h=cnt;        return cnt;    }};int lim;int f(int dep,int h){    return dep*3+h;}bool IDDFS(int dep,Board b){    int i,j,k;    Board tb;    b.getH();    if (f(dep,b.h) > lim*3)        return false;    if (b.getH() == 0)        return true;    for (i=0; i<b.n; i++)    {        for (j=i; j<b.n; j++)        {            for (k=0; k<i; k++)            {                tb=b;                tb.move(i,j,k);                if (IDDFS(dep+1,tb) == true)                    return true;            }            for (k=j+2; k<b.n; k++)            {                tb=b;                tb.move(i,j,k);                if (IDDFS(dep+1,tb) == true)                    return true;            }        }    }    return false;}int main(){    Board b;    int prob;    prob=1;    while (b.readIn() == true)    {        for (lim=0; ; lim++)        {            for(int i=0;i<b.n;i++)                printf("%d ",b.a[i]);            printf("\n");            if (IDDFS(0,b) == true)                break;        }        printf("Case %d: %d\n",prob,lim);        prob++;    }}
I have never used a struct written in a class .. After reading some questions and comments from some great gods, I feel that I am writing something like this. = It is really no difficulty, and I have nothing to learn. This is called an algorithm. It should be something that deserves it. It can be done without ACM .. It's just a cool job. It's good to do it in the future, and you should never be a big pen later ..

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.