Booksort POJ - 3460 (IDA*)

Posted iwannabe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Booksort POJ - 3460 (IDA*)相关的知识,希望对你有一定的参考价值。

Description

The Leiden University Library has millions of books. When a student wants to borrow a certain book, he usually submits an online loan form. If the book is available, then the next day the student can go and get it at the loan counter. This is the modern way of borrowing books at the library.

There is one department in the library, full of bookcases, where still the old way of borrowing is in use. Students can simply walk around there, pick out the books they like and, after registration, take them home for at most three weeks.

Quite often, however, it happens that a student takes a book from the shelf, takes a closer look at it, decides that he does not want to read it, and puts it back. Unfortunately, not all students are very careful with this last step. Although each book has a unique identification code, by which the books are sorted in the bookcase, some students put back the books they have considered at the wrong place. They do put it back onto the right shelf. However, not at the right position on the shelf.

Other students use the unique identification code (which they can find in an online catalogue) to find the books they want to borrow. For them, it is important that the books are really sorted on this code. Also for the librarian, it is important that the books are sorted. It makes it much easier to check if perhaps some books are stolen: not borrowed, but yet missing.

Therefore, every week, the librarian makes a round through the department and sorts the books on every shelf. Sorting one shelf is doable, but still quite some work. The librarian has considered several algorithms for it, and decided that the easiest way for him to sort the books on a shelf, is by sorting by transpositions: as long as the books are not sorted,

  1. take out a block of books (a number of books standing next to each other),
  2. shift another block of books from the left or the right of the resulting ‘hole’, into this hole,
  3. and put back the first block of books into the hole left open by the second block.

One such sequence of steps is called a transposition.

The following picture may clarify the steps of the algorithm, where X denotes the first block of books, and Y denotes the second block.

Original situation: 技术图片
After step 1: 技术图片
After step 2: 技术图片
After step 3: 技术图片

Of course, the librarian wants to minimize the work he has to do. That is, for every bookshelf, he wants to minimize the number of transpositions he must carry out to sort the books. In particular, he wants to know if the books on the shelf can be sorted by at most 4 transpositions. Can you tell him?

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with one integer n with 1 ≤ n ≤ 15: the number of books on a certain shelf.
  • One line with the n integers 1, 2, …, n in some order, separated by single spaces: the unique identification codes of the n books in their current order on the shelf.

Output

For every test case in the input file, the output should contain a single line, containing:

  • if the minimal number of transpositions to sort the books on their unique identification codes (in increasing order) is T ≤ 4, then this minimal number T;
  • if at least 5 transpositions are needed to sort the books, then the message "5 or more".

Sample Input

3
6
1 3 4 6 2 5
5
5 4 3 2 1
10
6 8 5 3 4 7 2 9 1 10

Sample Output

2
3
5 or more

题意:给n本书,每次可以选取其中一段移动到另外的位置,使得书的编号变成1~n,如果步数超过4,就输出5 or more,反则输出步数。

 思路:抽取书同一长度的书,有n-len+1种选择,有n-len个位置可以插入,并且因为一位置len长度后移等于后面位置前移

Σ(n-len)*(n-len+1)/2 <= (15*14+14*13+...+2*1)/2 == 560 ,因为只用判断是否步数<=4,不同搜索(560)4,时间复杂度过大。

通过IDA*(迭代加深+A*)

迭代加深:因为我们确定答案会在5之前收到

A*:评估函数,因为一次移动,最多可以使得3本书后面的书籍编号改变,所以该状态到达终态的至少步数f = ⌈错误后继/3⌉

技术图片

(红色为三本书,是一次移动种最多改变的三本,橙色区域代表移动的区域,移动至第一个红色书的后面,那么第二个和第三个红色书的后继都改变了)

 

技术图片
#include<iostream>
#include<cstdio>
#include<cmath>

using namespace std;

const int maxn = 22;
int t;
int n;

struct Node
{
    int s[maxn];
};

bool check(int s[])
{
    for(int i=1; i<n; i++)
        if(s[i] + 1 != s[i+1])
            return 0;
    return 1;
}

Node change(Node x,int l,int r,int len)
{
    Node tmp = x;
    for(int i=l; i<l+len; i++)
        x.s[r-len-l+i+1] = tmp.s[i];
    for(int i=l+len; i<=r; i++)
        x.s[i-len] = tmp.s[i];
    return x;
}

int cal(int s[])
{
    int ans = 0;
    for(int i=1; i<n; i++)
        if(s[i] + 1 != s[i+1])
            ans++;
    return ceil(ans/3.0);
}

bool dfs(Node x,int now,int lim)
{
    if(now + cal(x.s) > lim)
        return 0;
    if(check(x.s))return 1;
    for(int len=1; len<n; len++)
    {
        for(int i=1; i+len-1 <=n; i++)
        {
            for(int j=i+len; j<=n; j++)
            {
                if(dfs(change(x,i,j,len),now+1,lim))return 1;
            }
        }
    }
    return 0;
}

int main()
{
    scanf("%d",&t);
    Node start;
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
            scanf("%d",&start.s[i]);
        for(int i=0; i<5; i++)
        {
            if(dfs(start,0,i))
            {
                printf("%d\\n",i);
                break;
            }
            else if(i == 4)printf("5 or more\\n");

        }
    }
}
View Code

 

以上是关于Booksort POJ - 3460 (IDA*)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2286 The Rotation Game(IDA*)

POJ - 1753 Flip Game (IDA*)

(IDA*)POJ 2286 The Rotation Game

POJ - 2286 - The Rotation Game (IDA*)

[poj2286]The Rotation Game (IDA*)

POJ3134 Power Calculus IDA*搜索的一个应用