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 ...
随机推荐
- js作用域面试题大全
什么是作用域:浏览器给js的生存环境叫作用域. 什么是变量提升: Js代码执行前,浏览器会给一个全局作用域window Window分两个模块一个是存储模块一个是执行模块 存储模块找到所有的var和f ...
- 10个常见的JavaScript BUG
译者按: 安全起见,在开发中我基本不用==. 原文: 10 COMMON JAVASCRIPT BUGS AND HOW TO AVOID THEM 译者: Fundebug 为了保证可读性,本文采用 ...
- 【 js 工具 】如何在Github Pages搭建自己写的页面?
最近发现 github 改版了,已没有像原来的 Launch automatic page generator 这样的按钮等,所以我对我的文章也进行了修正,对于新版来说,步骤更加简单了.欢迎享用. - ...
- vue-基于elementui换肤
思路: 生成不同的css颜色文件,每个文件内部命名前加上.custom-颜色值做命名空间. 然后app.vue里引入全部的颜色文件. 用户点击某颜色,就在body加上class:custom-00a5 ...
- vuejs 1.x - 实例:搜索过滤
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- 浅谈pc和移动端的响应式
身为一个前端攻城狮,是不是经常遇到各种各样的响应式问题?下面我们来说一下: 1.响应式跟自适应有什么区别? 有些人可能还不知道响应式跟自适应的区别,甚至认为他们是同一个东西,其实不是的. 自适应是最早 ...
- [CSS] 点击事件触发的动画
源码 https://github.com/YouXianMing/CSS-Animations/tree/master/Event 效果 细节 1) 一个完整的可回溯的动画至少包括了两种状态,以及两 ...
- ajax简单登录(踩过的坑)
登陆页面: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- IIS 配置 HTTPS
前言 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer 或 Hypertext Transfer Protocol Secu ...
- 软件工程:java实现wc项目基本功能
项目相关要求 项目地址:https://github.com/xiawork/wcwork 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处理多个 ...