poj3460&hdu1685Booksort(IDA*)

Booksort
Time Limit: 15000MS Memory Limit: 65536K
Total Submissions: 1908 Accepted: 862

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的范围是1-15,最多15!种状态,bfs很难hold住,要求4步出解,可见搜索深度很小,所以考虑迭代加深搜索。随便写一个发现跑样例都费劲,需要剪枝。加个启发函数,升级成IDA*。这是黑书上的例题,启发函数不好想。很遗憾自己没想出来,看了黑书才明白的。

启发函数要满足三角不等式h(s1) <= h(s2) + cost(s1,s2)的。就是说从状态s1到状态s2,deta(h)<= cost(s1,s2),而本题的每一步状态转移的cost是1,所以要找到保证deta(h)<=1的启发函数确实不容易。

详情请见黑书。

上代码:

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 16;
int n;
int book[N];
int lim;
bool flag;

bool isok()
{
    int i = 1;
    for(i = 1;i <= n;i ++)
        if(book[i] != i)
            return false;
    return true;
}

int h()
{
    int i,ret = 0;
    if(book[1] != 1)
        ret ++;
    if(book[n] != n)
        ret ++;
    for(i = 2;i < n;i ++)
        if(book[i] + 1 != book[i + 1])
            ret ++;
    return ret;
    //return ceil(ret/3.0);
}

void put(int st,int len,int pos)
{
    int i,j,tmp[N];
    for(i = 1;i <= len;i ++)
        tmp[i] = book[st + i - 1];
    if(pos > st)
    {
        for(i = st + len;i <= pos;i ++)
            book[i - len] = book[i];
        for(i -= len,j = 1;i <= pos;i ++,j ++)
            book[i] = tmp[j];
    }
    else
    {
        for(i = st - 1;i > pos;i --)
            book[i + len] = book[i];
        for(i = 1;i <= len;i ++)
            book[pos + i] = tmp[i];
    }
}

void dfs(int dp)
{
    if(flag)
        return;
    if(dp * 3 + h() > 3 * lim)
        return;
    if(isok())
    {
        flag = true;
        return;
    }
    int i,j,k;
    int tmp[N];
    for(i = 1;i <= n;i ++)
        tmp[i] = book[i];
    for(i = 1;i < n;i ++)//len
    {
        for(j = 1;j + i <= n + 1;j ++)//start
        {
            for(k = 0;k <= n;k ++)//tag
            {
                if(k >= j && k < j + i)
                    continue;
                put(j,i,k);
                dfs(dp + 1);
                for(int tt = 1;tt <= n;tt ++)
                    book[tt] = tmp[tt];
            }
        }
    }
}

int main()
{
    int i,t;
    scanf("%d",&t);
    while(t --)
    {
        scanf("%d",&n);
        lim = 0;
        for(i = 1;i <= n;i ++)
        {
            scanf("%d",&book[i]);
            if(book[i] == i)
                lim ++;
        }
        if(lim == n)
        {
            printf("0\n");
            continue;
        }
        lim = 1;
        flag = false;
        while(1)
        {
            dfs(0);
            if(flag)
                break;
            lim ++;
            if(lim > 4 || flag)
                break;
        }
        if(flag)
            printf("%d\n",lim);
        else
            printf("5 or more\n");
    }
    return 0;
}
//625MS	228K



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值