UVA - 11212 Editing a Book : IDA*

题目点此跳转

You have n equal-length paragraphs numbered 1 to n. Now you want to arrange them in the order of 1, 2, … , n. With the help of a clipboard, you can easily do this: Ctrl-X (cut) and Ctrl-V (paste) several times. You cannot cut twice before pasting, but you can cut several contiguous paragraphs at the same time - they’ll be pasted in order.
 For example, in order to make {2, 4, 1, 5, 3, 6}, you can cut 1 and paste before 2, then cut 3 and paste before 4. As another example, one copy and paste is enough for {3, 4, 5, 1, 2}. There are two ways to do so: cut {3, 4, 5} and paste after {1, 2}, or cut {1, 2} and paste before {3, 4, 5}.

Input

The input consists of at most 20 test cases. Each case begins with a line containing a single integer n (1 < n < 10), thenumber of paragraphs. The next line contains a permutation of 1, 2, 3, … , n. The last case is followed by a single zero, which should not be processed.

Output

For each test case, print the case number and the minimal number of cut/paste operations.

Sample Input

6
2 4 1 5 3 6
5
3 4 5 1 2
0

Sample Output

Case 1: 2
Case 2: 1

思路

 题目意思是你有一篇由n(2≤n≤9)个自然段组成的文章,希望将它们排列成1,2,…, n。可以用Ctrl+X(剪切) 和Ctrl+V(粘贴)快捷键来完成任务。每次可以剪切一段连续的自然段,粘贴时按照顺序粘贴。注意,剪贴板只有一个,所以不能连续剪切两次,只能剪切和粘贴交替。
 例如,为了将{2,4,1,5,3,6}变为升序,可以剪切1将其放到2前,然后剪切3将其放到4前。再如,对于排列{3,4,5,1,2},只需一次剪切和一次粘贴即可——将{3,4,5}放在{1,2}后,或者将{1,2}放在{3,4,5}前。

 以下两点是显而易见的:

  • 最多操作8次一定会使其有序
  • 每次操作最多有三个段落的后续段落会改变

 那么我们搜索深度的上界和下界就都有了,直接使用IDA*即可.
 至于状态,枚举所有可以进行的操作即可.

代码

#include <algorithm>
#include <iostream>
#include <sstream>
#include <utility>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#define met(a,b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
#define OT freopen("ot.txt", "w", stdout);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e1 + 10;
const int INF = 0x7fffffff;
const int dir[5][2] = {0,0,-1,0,1,0,0,-1,0,1};

int n, a[maxn], tmp[maxn], maxd, ans;

void moves(int L, int R, int to) {
    memcpy(tmp, a, sizeof(tmp));
    for(int i = R+1; i <= to; ++i) a[i-(R-L+1)] = tmp[i];
    for(int i = to-(R-L), j = L; i <= to; ++i,++j) a[i] = tmp[j];
}

int getH() {
    int cnt = 0;
    for(int i = 1; i <= n-1; ++i) { if(a[i]+1 != a[i+1]) ++cnt; }
    return cnt;
}

bool dfs(int deep) {
    int h = getH();
    if((maxd-deep)*3 < h) return 0;
    if(h == 0) return 1;

    int t[maxn];
    for(int i = 1; i <= n; ++i)
    for(int j = i; j <= n; ++j)
    for(int k = j+1; k <= n; ++k) {
        if(k >= i-1 && k <= j) continue;
        memcpy(t, a, sizeof(t));
        moves(i, j, k);
        if(dfs(deep+1)) return 1;
        memcpy(a, t, sizeof(a));
    }
    return 0;
}

int IDAS() {
    bool ok = 0;
    if(getH() == 0) return 0;
    for(maxd = 1; maxd < 9; ++maxd) {
        if(dfs(0)) { ok = 1; break; }
    }
    return maxd;
}

int main() {
    #ifdef _LOCAL
    IN; //OT;
    #endif // _LOCAL

    int kase = 0;
    while(scanf("%d", &n) == 1 && n) {
        for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
        printf("Case %d: %d\n", ++kase, IDAS());
    }

    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值