UVA 11212 IDA*
移动一块连续的区间使得数列递增。问最少次数。
直接IDA*暴搜,只是我没有想到A*函数,所以就随手写了个连续递增块数作为估价函数,WA了,然后除以2,还是WA,除以3,WA,除以4.。。过了= =
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<list>
#include<algorithm>
using namespace std;
#define stop system("pause")
int n,a[20],lim;
void print(int *a){for(int i=1;i<=n;i++) printf("%d ",a[i]);puts("");}
int h(int *s)
{
int cnt=0;
for(int i=1;i<n;i++)
if(s[i]+1!=s[i+1])
cnt++;
if(s[n]!=n) cnt++;
return cnt/4;
}
bool change(int from,int ed,int to,int *s)
{
if(to>=from-1&&to<=ed) return false;
int next[20],tmp[20];
for(int i=0;i<=n;i++) tmp[i]=s[i],next[i]=i+1;
next[from-1]=ed+1;
next[to]=from;
next[ed]=to+1;
for(int i=next[0],cnt=1;cnt<=n;i=next[i],cnt++)
{
s[cnt]=tmp[i];
}
return true;
}
bool over(int *a)
{
for(int i=1;i<=n;i++) if(a[i]!=i) return false;
return true;
}
bool dfs(int dep,int *s)
{
if(over(s)) return true;
if(dep>=lim) return false;
if(dep+h(s)>=lim) return false;
for(int from=1;from<=n;from++)
{
for(int ed=from;ed<=n;ed++)
{
for(int to=0;to<=n;to++)
{
int tmp[15];
memcpy(tmp,s,sizeof(int)*(n+1));
if(change(from,ed,to,tmp)==0) continue;
if(dfs(dep+1,tmp)) return true;
}
}
}
return false;
}
int main()
{
int ca=1;
while(~scanf("%d",&n))
{
if(n==0) break;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(lim=h(a);!dfs(0,a);lim++) ;
printf("Case %d: %d\n",ca++,lim);
}
return 0;
}
/*
9
9 8 7 6 5 4 3 2 1
*/
貌似正解应该是 dep*3+h()>lim*3
UVA 11212 IDA*的更多相关文章
- 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 ...
- 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 编辑书稿(dfs+IDA*)
https://vjudge.net/problem/UVA-11212 题意:给出n个自然段组成的文章,将他们排列成1,2...,n.每次只能剪切一段连续的自然段,粘贴时按照顺序粘贴. 思路:状态空 ...
- UVA - 11212 Editing a Book (IDA*)
给你一个长度为n(n<=9)的序列,每次可以将一段连续的子序列剪切到其他地方,问最少多少次操作能将序列变成升序. 本题最大的坑点在于让人很容易想到许多感觉挺正确但实际却不正确的策略来避开一些看似 ...
- UVA - 11212 Editing a Book (IDA*搜索)
题目: 给出n(1<n<10)个数字组成的序列,每次操作可以选取一段连续的区间将这个区间之中的数字放到其他任意位置.问最少经过多少次操作可将序列变为1,2,3……n. 思路: 利用IDA* ...
- UVa 11212 Editing a Book (IDA* && 状态空间搜索)
题意:你有一篇n(2≤n≤9)个自然段组成的文章,希望将它们排列成1,2,…,n.可以用Ctrl+X(剪切)和Ctrl+V(粘贴)快捷键来完成任务.每次可以剪切一段连续的自然段,粘贴时按照顺序粘贴.注 ...
- UVA - 11212 Editing a Book(IDA*算法+状态空间搜索)
题意:通过剪切粘贴操作,将n个自然段组成的文章,排列成1,2,……,n.剪贴板只有一个,问需要完成多少次剪切粘贴操作可以使文章自然段有序排列. 分析: 1.IDA*搜索:maxn是dfs的层数上限,若 ...
- uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索
迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...
- UVA 11212 Editing a Book
题意: 有一篇由n个自然段组成的文章.希望将他们排成递增序列.只能剪贴和粘贴交替进行,剪贴时可以剪贴一段连续的自然段. 分析: 用IDA*算法求解.当3*d+h>maxd时剪枝. 代码: #in ...
随机推荐
- 初试ubuntu14.4问题集锦
接触Linux系统也好长时间了,但每次都是浅尝则止.前几天突发兴趣,想认真地学习一下这个名扬天下的稳定的操作系统.于是试着装了一下炒作的最凶的Ubuntu. 安装的Ubuntu系统版本为14.04. ...
- javascript——闭包
<script type="text/javascript"> //什么是闭包: //是指语法域位于某个特定的区域,具有持续参照(读写)位于该区域内自身范围之外的执行域 ...
- Linq查询IEnumerable与IQueryable
class Program { static void Main(string[] args) { System.Diagnostics.Stopwatch stp = new Stopwatch() ...
- PHPCMS 使用图示和PHPCMS二次开发教程(转)
PHPCMS V9 核心文件说明 模块与控制器 模块: phpcms v9框架中的模块,位于phpcms/modules目录中 每一个目录称之为一个模块.即url访问中的m. 访问content模块示 ...
- hdu3652B-number
Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal for ...
- [BZOJ - 2819] Nim 【树链剖分 / DFS序】
题目链接: BZOJ - 2819 题目分析 我们知道,单纯的 Nim 的必胜状态是,各堆石子的数量异或和不为 0 .那么这道题其实就是要求求出树上的两点之间的路径的异或和.要求支持单点修改. 方法一 ...
- 关于 Boolean 的转换
前端经常喜欢这样写 if else if(value) { //do something } javascript 能智能的把任何类型的 value 转换成 boolean 来进行 if 判断 转换是 ...
- python MySQLdb segmentation fault
import MySQLdb conn = MySQLdb.connect(host=_host,user=_user,passwd=_passwd,db=_db,charset=_charset,p ...
- 揪出“凶手”——实战WinDbg分析电脑蓝屏原因
http://www.appinn.com/blue-screen-search-code/ 蓝屏代码查询器 – 找出蓝屏的元凶 11 文章标签: windows / 系统 / 蓝屏. 蓝屏代码查询器 ...
- 用 SQL 脚本读取Excel 中的sheet数量及名称
-- Get table (worksheet) or column (field) listings from an excel spreadsheet -- 设置变量 declare @linke ...