UVA11212-Editing a Book(迭代加深搜索)
Problem UVA11212-Editing a Book
Accept:572 Submit:4428
Time Limit: 10000 mSec
Problem Description
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
2 4 1 5 3 6
5
3 4 5 1 2
0
Sample Ouput
Case 1: 2
Case 2: 1
题解:第一到IDA*算法的题目。迭代加深搜索,就是在普通DFS上加了个深度限制,如果目前的深度无法得到结果,就让深度限制变大,直到找到解。该算法中最重要的就是估价函数,用该函数进行剪枝操作,当前深度为d,如果最理想的情况下还要搜索h层,那么当d+h > maxd时显然就可以剪枝,其余部分和普通dfs区别不大。
续:今天对这道题又进行了一个小小的尝试,收获很大。之所以对它进行二次尝试,主要是因为第一次写时按照lrj的思路copy的,想真正自己实现一次,这次实现完全以lrj在讲解该算法时的思路为框架进行code,dfs第一步判断深度是否达到maxd,达到了就进行判断是否成立,返回信息。关键在于下一步的是先枚举还是先剪枝,如果先剪枝,两个代码的效率几乎没有差距,如果先枚举,直接TLE,这份代码用时640ms而限制是10000ms,这一个顺序使得效率差了十倍不止,仔细想了想,先剪枝与后剪枝相比,把剪枝操作提前了一层,对于每一层都有很多节点的搜索来说是很不划算的。千万注意这个顺序!!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int maxn = ;
int n,order[maxn];
int maxd; int cal() {
int cnt = ;
for (int i = ; i < n-; i++) {
if (order[i] != order[i + ] - ) cnt++;
}
if (order[n - ] != n) cnt++;
return cnt;
} bool is_sorted() {
for (int i = ; i < n - ; i++) {
if (order[i] >= order[i + ]) return false;
}
return true;
} bool dfs(int d) {
if ( * d + cal() > maxd * ) return false;
if (is_sorted()) return true; int old[maxn],now[maxn];
memcpy(old, order, sizeof(order));
for (int i = ; i < n; i++) {
for (int j = i; j < n; j++) {
int cnt = ;
for (int k = ; k < n; k++) {
if (k < i || k > j) {
now[cnt++] = order[k];
}
}
for (int k = ; k <= cnt; k++) {
int cnt2 = ;
for (int p = ; p < k; p++) order[cnt2++] = now[p];
for (int p = i; p <= j; p++) order[cnt2++] = old[p];
for (int p = k; p < cnt; p++) order[cnt2++] = now[p];
if (dfs(d + )) return true;
memcpy(order, old, sizeof(order));
}
}
}
return false;
} int iCase = ; int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d", &n) && n) {
for (int i = ; i < n; i++) {
scanf("%d", &order[i]);
}
for (maxd = ; maxd < n; maxd++) {
if (dfs()) break;
}
printf("Case %d: %d\n", iCase++, maxd);
}
return ;
}
UVA11212-Editing a Book(迭代加深搜索)的更多相关文章
- 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 ...
- uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索
迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...
- 7-10Editing aBook uva11212(迭代加深搜索 IDA*)
题意: 给出n( 2<=n<=9) 个乱序的数组 要求拍成升序 每次 剪切一段加上粘贴一段算一次 拍成1 2 3 4 ...n即可 求排序次数 典型的状态空间搜索问题 ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
随机推荐
- Fundebug累计处理1000万条错误事件!
摘要上线半年时间,Fundebug累计处理1000万条错误事件.10000000啊! 自从去年双11[正式上线](https://blog.fundebug.com/2016/11/11/fundeb ...
- 有序列表ol,无序列表ul,定义列表dl
====================非常重要=================无序列表ul中有一个type属性四个属性值type="disc" 实心圆点(默认) type=&q ...
- 华为P20无线投屏到电脑 绝地求生投射电脑
如今出门在外,必不可少的就是手机,如果没有了手机,每个人都会感觉没有安全感,感觉和世界失去了联系,我们每天每个人都在使用手机,但是作为华为手机用户的你,了解华为P20无线投屏到电脑是怎么操作的吗? 使 ...
- 照葫芦画瓢系列之Java --- Maven的集成和使用
一.和Eclipse的集成 1.添加Maven 在windows--> preferences中找到maven选项,如下图: 如果没有上图的Name为apache-maven-3.5.2的项,则 ...
- 利用Syslog Watcher在windows下部署syslog日志服务器
1.概述 syslog协议是各种网络设备.服务器支持的网络日志记录标准.Syslog消息提供有关网络事件和错误的信息.系统管理员使用Syslog进行网络管理和安全审核. 通过专用的syslog服务器和 ...
- 关系数据库标准语言SQL——概述
SQL是一种介于关系代数与关系演算之间的结构化查询语言,其功能并不仅仅是查询.SQL是一个通用的.功能极强的关系数据库语言.SQL(Structured Query Language)结构化查询语 ...
- redis cluster是如何做到集两家之长的
站在读写分离的层次看redis的时候,redis和master和slave存在明显的主从关系,也就是说master处于管理状态,salve跟着大哥混,master给小弟slave发粮食[发送内存快照数 ...
- git 入门教程之 git 私服搭建教程
git 私服搭建教程 前几节我们的远程仓库使用的是 github 网站,托管项目大多是公开的,如果不想让任何人都能看到就需要收费,而且 github 网站毕竟在国外,访问速度太慢,基于上述两点原因,我 ...
- Scala并发编程【进阶】
package com.dingxin.entrance import java.text.SimpleDateFormat import java.util.Date import scala.ac ...
- hivesql优化的深入解析
转载:https://www.csdn.net/article/2015-01-13/2823530 一个Hive查询生成多个Map Reduce Job,一个Map Reduce Job又有Map, ...