【暴力搜索】[UVa 11212]Editing a Book
2015-07-21 09:38
281 查看
就是纯暴力,每次枚举起点中点和向后移动的步数,加上if(dif(now) + 3 * (u-1) > 3 * maxd) return false;优化dif(now)表示每一位的后面是否是正确的
[code]#include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct State{int s[11];}; int n, maxd; int dif(State s){ int ret = 0; for(int i=1;i<n;i++) if(s.s[i+1]!=s.s[i]+1) ret++; if(s.s !=n) ret++; return ret; } State rret; void move_back(State st, int i, int j, int bk){ for(int k=1; k<= bk; k++) rret.s[i+k-1] = st.s[j+k]; for(int k=i; k<=j; k++) rret.s[k+bk] = st.s[k]; } bool dfs(int u, State now){ State tmp; if(u > maxd) return dif(now) == 0; if(dif(now) + 3 * (u-1) > 3 * maxd) return false; int bend; for(int i=1;i<=n;i++) for(int j=i;j<=n;j++){ bend = n-j; for(int back=1;back<=bend; back++){ rret = now; move_back(now, i, j, back);tmp = rret; if(dfs(u+1, tmp)) return true; } } return false; } int main(){ int ccnt=0;State be; while(scanf("%d", &n)!=EOF && n){ for(int i=1;i<=n;i++) scanf("%d", &be.s[i]); maxd = 0; while(!dfs(1, be)) maxd++; printf("Case %d: %d\n",++ccnt, maxd); } return 0; }
相关文章推荐
- POJ 3270 Cow Sorting(置换环)
- 读书笔记:大小端模式对位域的影响
- POJ3696 The Luckiest number 欧拉函数应用
- C#设计模式 - 观察者模式(使用委托)
- 收藏网站
- poj 3370 Halloween treats
- 静态数据竞争检测工具之LOCKSMITH-安装和使用
- 收藏网站56
- 《深入浅出struts2》--第三章,动作与结果
- POJ 1944:Fiber Communications
- 物联网的发展前途
- spring定时器配置
- Cocoa深入学习:NSOperationQueue、NSRunLoop和线程安全
- ZOJ3690Choosing number
- hive-0.11.0安装方法详解
- SQL Server Profiler工具
- AppCanCSS背景图片的属性
- SGU 280.Trade centers(贪心)
- POJ 1944:Fiber Communications
- Android通过反射打造可以存储任何对象的万能SharedPreferences