【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

迭代加深搜。
很容易想到,最多只要搜8层就可以得到答案了
->最多8下肯定可以还原。
则枚举一下最大层数。然后做个深搜就好。
优化。
设0..n每个数字的后继不为a[i]+1的个数为cnt
则每次操作显然最多只能减少3个cnt..
可以画图分析一下。
则可以拿这个来做剪枝。。
然后插入的过程可以用链表来实现。

【代码】

/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
#include <bits/stdc++.h>
using namespace std; const int N = 10; int n,a[N+5],l[N+5],r[N+5],templ[N+5],tempr[N+5],maxdep,ans = -1; void Move(int i,int j,int pos){
r[l[i]] = r[j];
l[r[j]] = l[i];
l[r[pos]] = j;
r[j] = r[pos]; r[pos] = i;
l[i] = pos;
} void dfs(int dep){
if (dep==maxdep){
for (int i = 0;i <= n;i++)
if (a[r[i]]!=a[i]+1)
return; if (ans==-1) ans = dep;
else ans = min(ans,dep); return;
} int cnt = 0;
for (int i = 0;i <= n;i++)
if (a[r[i]]!=a[i]+1)
cnt++;
int delta = maxdep-dep;
if (delta*3<cnt) return; for (int i = r[0];i != n+1;i=r[i])
for (int j = i;j != n+1;j = r[j]){
//1,2,3..i..j,j+1....n
for (int pos = 0;pos!=l[i];pos=r[pos]){
int pre = l[i];
Move(i,j,pos);
dfs(dep+1);
Move(i,j,pre);
}
for (int pos = r[j];pos != n+1;pos = r[pos]){
int pre = l[i];
Move(i,j,pos);
dfs(dep+1);
Move(i,j,pre);
}
}
return;
} int main(){
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int kase = 0;
while (cin >> n && n){
cout <<"Case "<<++kase<<": ";
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= n+1;i++) l[i] = i-1,r[i] = i+1;
r[0] = 1;
a[n+1] = n+1;
ans = -1;
for (maxdep = 0;maxdep <= 9;maxdep++){
dfs(0);
if (ans!=-1) break;
}
cout << ans << endl;
}
return 0;
}

【例题 7-10 UVA - 11212】Editing a Book的更多相关文章

  1. UVA 11212 Editing a Book [迭代加深搜索IDA*]

    11212 Editing a Book You have n equal-length paragraphs numbered 1 to n. Now you want to arrange the ...

  2. UVA - 11212 Editing a Book (IDA*搜索)

    题目: 给出n(1<n<10)个数字组成的序列,每次操作可以选取一段连续的区间将这个区间之中的数字放到其他任意位置.问最少经过多少次操作可将序列变为1,2,3……n. 思路: 利用IDA* ...

  3. UVA 11212 Editing a Book

    题意: 有一篇由n个自然段组成的文章.希望将他们排成递增序列.只能剪贴和粘贴交替进行,剪贴时可以剪贴一段连续的自然段. 分析: 用IDA*算法求解.当3*d+h>maxd时剪枝. 代码: #in ...

  4. UVA - 11212 Editing a Book (IDA*)

    给你一个长度为n(n<=9)的序列,每次可以将一段连续的子序列剪切到其他地方,问最少多少次操作能将序列变成升序. 本题最大的坑点在于让人很容易想到许多感觉挺正确但实际却不正确的策略来避开一些看似 ...

  5. uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索

    迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...

  6. UVa 11212 Editing a Book (IDA* && 状态空间搜索)

    题意:你有一篇n(2≤n≤9)个自然段组成的文章,希望将它们排列成1,2,…,n.可以用Ctrl+X(剪切)和Ctrl+V(粘贴)快捷键来完成任务.每次可以剪切一段连续的自然段,粘贴时按照顺序粘贴.注 ...

  7. UVA - 11212 Editing a Book(IDA*算法+状态空间搜索)

    题意:通过剪切粘贴操作,将n个自然段组成的文章,排列成1,2,……,n.剪贴板只有一个,问需要完成多少次剪切粘贴操作可以使文章自然段有序排列. 分析: 1.IDA*搜索:maxn是dfs的层数上限,若 ...

  8. Editing a Book UVA - 11212 IDA*

    You have n equal-length paragraphs numbered 1 to n . Now you want to arrange them in the order of 1 ...

  9. 【UVa】11212 Editing a Book(IDA*)

    题目 题目     分析 get一下IDA*的技巧,感觉总体来说不难,主要是剪枝比较难想. 这是lrj的代码,比较通俗易懂,关键就是选定一个区间再取出来,插入到一个位置,接下来转移到这个状态.     ...

随机推荐

  1. UTC时间 GMT时间 本地时间 北京时间 时区 夏令时简要说明

    1.UTC时间 与 GMT时间 整个地球分为二十四时区,每个时区都有自己的本地时间.为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated). ...

  2. mpvue 开发小程序

    转换成vue语法, 小程序中原生的事件用@ 原生的属性用:

  3. <QT之Bug制造机>QT中串口类“QSerialPort”的学习笔记

    QT5中已经增加了串口类QSrialPort,可以直接调用API函数进行快速开发. 1. 获取串口信息 Dialog::Dialog(QWidget *parent) : QDialog(parent ...

  4. Yeslab 华为安全HCIE-第七门-Agile Controlle

    课程目录:   华为安全HCIE-第七门-Agile Controller(12篇)\1_aglie_controller产品亮点讲解.avi 华为安全HCIE-第七门-Agile Controlle ...

  5. chgrp---改变文件或目录所属的用户组

    chgrp命令用来改变文件或目录所属的用户组.该命令用来改变指定文件所属的用户组.其中,组名可以是用户组的id,也可以是用户组的组名.文件名可以 是由空格分开的要改变属组的文件列表,也可以是由通配符描 ...

  6. [Python] Find available methods and help in REPL

    For example you want to know what methods are available in Python for String, you can do : dir(" ...

  7. hdu1856 More is better (并查集)

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  8. The incident LOST_EVENTS occured on the master. Message: error writing to the binary log, Error_code

    1 mysq error日志报错例如以下: 2014-05-12 11:29:54 22977 [ERROR] Slave SQL: The incident LOST_EVENTS occured ...

  9. js---16继承

    123 instanceof Number;//false,要左边是对象右边是函数 typeof 123 ; //number new Number(123) instanceof Number; / ...

  10. js---- localStorage的基本用法

    <body> <div> <span>用户名</span> <input type="text" class='usernam ...