移动一块连续的区间使得数列递增。问最少次数。

直接IDA*暴搜,只是我没有想到A*函数,所以就随手写了个连续递增块数作为估价函数,WA了,然后除以2,还是WA,除以3,WA,除以4.。。过了= =

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<string>
  5. #include<list>
  6. #include<algorithm>
  7. using namespace std;
  8. #define stop system("pause")
  9. int n,a[20],lim;
  10. void print(int *a){for(int i=1;i<=n;i++) printf("%d ",a[i]);puts("");}
  11. int h(int *s)
  12. {
  13. int cnt=0;
  14. for(int i=1;i<n;i++)
  15. if(s[i]+1!=s[i+1])
  16. cnt++;
  17. if(s[n]!=n) cnt++;
  18. return cnt/4;
  19. }
  20. bool change(int from,int ed,int to,int *s)
  21. {
  22. if(to>=from-1&&to<=ed) return false;
  23. int next[20],tmp[20];
  24. for(int i=0;i<=n;i++) tmp[i]=s[i],next[i]=i+1;
  25. next[from-1]=ed+1;
  26. next[to]=from;
  27. next[ed]=to+1;
  28. for(int i=next[0],cnt=1;cnt<=n;i=next[i],cnt++)
  29. {
  30. s[cnt]=tmp[i];
  31. }
  32. return true;
  33. }
  34. bool over(int *a)
  35. {
  36. for(int i=1;i<=n;i++) if(a[i]!=i) return false;
  37. return true;
  38. }
  39. bool dfs(int dep,int *s)
  40. {
  41. if(over(s)) return true;
  42. if(dep>=lim) return false;
  43. if(dep+h(s)>=lim) return false;
  44. for(int from=1;from<=n;from++)
  45. {
  46. for(int ed=from;ed<=n;ed++)
  47. {
  48. for(int to=0;to<=n;to++)
  49. {
  50. int tmp[15];
  51. memcpy(tmp,s,sizeof(int)*(n+1));
  52. if(change(from,ed,to,tmp)==0) continue;
  53. if(dfs(dep+1,tmp)) return true;
  54. }
  55. }
  56. }
  57. return false;
  58. }
  59. int main()
  60. {
  61. int ca=1;
  62. while(~scanf("%d",&n))
  63. {
  64. if(n==0) break;
  65. for(int i=1;i<=n;i++) scanf("%d",&a[i]);
  66. for(lim=h(a);!dfs(0,a);lim++) ;
  67. printf("Case %d: %d\n",ca++,lim);
  68. }
  69. return 0;
  70. }
  71. /*
  72. 9
  73. 9 8 7 6 5 4 3 2 1
  74. */

貌似正解应该是 dep*3+h()>lim*3

UVA 11212 IDA*的更多相关文章

  1. 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 ...

  2. 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 ...

  3. UVa 11212 编辑书稿(dfs+IDA*)

    https://vjudge.net/problem/UVA-11212 题意:给出n个自然段组成的文章,将他们排列成1,2...,n.每次只能剪切一段连续的自然段,粘贴时按照顺序粘贴. 思路:状态空 ...

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

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

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

    题目: 给出n(1<n<10)个数字组成的序列,每次操作可以选取一段连续的区间将这个区间之中的数字放到其他任意位置.问最少经过多少次操作可将序列变为1,2,3……n. 思路: 利用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. uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索

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

  9. UVA 11212 Editing a Book

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

随机推荐

  1. 【HDU3487】【splay分裂合并】Play with Chain

    Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...

  2. windows编程中 一些前缀区分 IDR和IDD

    IDC_:控件的ID命名前缀(Control) IDM_:菜单的ID命名前缀(Menu) IDD_:对话框的ID命名前缀(Dialog) IDR_:资源的ID命名前缀(Resource) IDS_:字 ...

  3. ios开发之ios9UIWebView不显示网页问题

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  4. JavaScript 获取当月天数

    getDate() 方法可返回月份的某一天.取值范围是1~31 如果是0的话,就返回最后一天.这样就能取得当月的天数了 比如获取16年2月份的天数 var day = new Date(2016,2, ...

  5. php里 \r\n换行问题

    <?php echo "hello"; echo "\r\n"; echo "world"; ?> 在浏览器输出的是hello ...

  6. 你知道HTML标签设计的本意吗? 把HTML标签用到该用的地方去

    "DIV+CSS"这个词汇不知道害了多少人,也许其提出者本意并没有错,但是跟风者从表现曲解了其意思,认为整个页面就应当是DIV+CSS文件的组合.这样做,对于视觉上并没有什么影响, ...

  7. Android获取相册图片

    1. AlertDialog的使用 2. 显示和隐式意图的区别 3. 相册页面的跳转 4. 选择完成后返回图片的获取 ----------------------------------------- ...

  8. java中处理http连接超时的方法

    声明一个boolean公共变量,表明当前httpconnection是否得到服务器回应. 你的连接线程中在连接之前置这个变量为false; 另起一个监视线程,拿到那个HttpConnection的连接 ...

  9. python之加密

    import hashlib obj = hashlib.md5(bytes('adfasfasdfsfasf',encoding = 'utf-8')) obj.update(bytes('123' ...

  10. IOS“多继承”

    转自念茜的博客: 当单继承不够用,很难为问题域建模时,我们通常都会直接想到多继承.多继承是从多余一个直接基类派生类的能力,可以更加直接地为应用程序建模.但是Objective-C不支持多继承,由于消息 ...