解题思路:

  这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是:

    1)状态转移代价很大,一次需要向八个方向寻找;

    2)哈希表更新频繁;

    3)采用广度优先搜索结点数越来越多,耗时过大;

  经过简单计算,最长大概10次左右的变换就能出解,于是笔者就尝试采用IDA*,迭代加深搜索的好处是:

    1)无需存储状态,节约时间和空间;

    2)深度优先搜索查找的结点数少;

    3)递归方便剪枝;

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <ctime> using namespace std;
8
#define time_ printf("time :%f\n",double(clock())/CLOCKS_PER_SEC)
#define maxs 735471
typedef int state[];
int init_p[];
state start;
int num;
int seq[maxs];
int cur;
char P1[maxs];
int maxd;
inline void get_P(char *P){
for(int i=;i<cur;i++)
P[i]=seq[i]+'A';
}
int pos[][]={
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,}
};
int tar[]={,,,,,,,}; inline int tar_num(const state &p){
int cnt=;
for(int k=;k<=;k++){
int c=;
for(int i=;i<;i++)
if(p[tar[i]]==k)
c++;
cnt=max(c,cnt);
}
return cnt;
}
inline void move(state& s,int i){
int temp=s[pos[i][]];
int j=;
for(;j<;j++)
s[pos[i][j]]=s[pos[i][j+]];
s[pos[i][j]]=temp;
}
bool dfs(state& u,int s_d){
if(s_d==maxd){
if(tar_num(u)==){
num=u[tar[]];
return true;
}
return false;
}
if(-tar_num(u)>maxd-s_d)
return false;
for(int i=;i<;i++){
move(u,i);
seq[cur++]=i;
if(dfs(u,s_d+))
return true;
cur--;
if(i%) move(u,(i+)%);
else move(u,(i+)%);
}
return false;
}
inline void init(){
memset(seq, -, sizeof seq);
cur=;
}
bool solve(){
init();
bool ok=false;
state u;
memcpy(u, init_p, sizeof u);
if(tar_num(u)==){
printf("‘No moves needed\n");
}
if(dfs(u,)){
ok=true;
get_P(P1);
}
return ok;
}
int main() { while(){
memset(P1, , sizeof P1);
for(int i=;i<;i++){
scanf("%d",&init_p[i]);
if(init_p[i]==) {
//time_;
return ;
}
}
state u;
memcpy(u, init_p, sizeof u);
if(tar_num(u)==){
printf("No moves needed\n%d\n",u[tar[]]);
continue;
}
for(maxd=;;maxd++)
if(solve())
break;
printf("%s\n%d\n",P1,num);
//time_;
}
return ;
}

UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]的更多相关文章

  1. POJ2286 The Rotation Game[IDA*迭代加深搜索]

    The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 6325   Accepted: 21 ...

  2. uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索

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

  3. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  4. UVA - 11214 Guarding the Chessboard(迭代加深搜索)

    题目: 输入一个n*m的棋盘(n,m<10),某些格子有标记,用最少的皇后守卫(即占据或攻击)所有的标记的格子.输出皇后的个数. 思路: 一开始没有想到用迭代加深搜索,直接dfs结果还没写完就发 ...

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

  6. 埃及分数 迭代加深搜索 IDA*

    迭代加深搜索 IDA* 首先枚举当前选择的分数个数上限maxd,进行迭代加深 之后进行估价,假设当前分数之和为a,目标分数为b,当前考虑分数为1/c,那么如果1/c×(maxd - d)< a ...

  7. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  8. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  9. 7-10Editing aBook uva11212(迭代加深搜索 IDA*)

    题意:  给出n( 2<=n<=9) 个乱序的数组  要求拍成升序  每次 剪切一段加上粘贴一段算一次  拍成1 2 3 4 ...n即可     求排序次数 典型的状态空间搜索问题   ...

随机推荐

  1. vue页面跳转传参

    跳转页 this.$router.push({name:'路由命名',params:{参数名:参数值,参数名:参数值}}) 接收页 this.$route.params.参数名

  2. 【51NOD1304】字符串的相似度

    题目描述 我们定义2个字符串的相似度等于两个串的相同前缀的长度.例如 "abc" 同 "abd" 的相似度为2,"aaa" 同 " ...

  3. 【数论】如何证明gcd/exgcd

    我恨数论 因为打这篇的时候以为a|b是a是b的倍数,但是懒得改了,索性定义 a|b 为 a是b的倍数 咳咳,那么进入正题,如何证明gcd,也就是 gcd(a,b) = gcd(b,a%b)? 首先,设 ...

  4. 使用pip出现 cannot import name "main"

    最近在linux使用pip install时遇到了这个报错 1.jpg ImportError: cannot import name main 遇到这个问题,我的解决办法是:cd 到usr/bin目 ...

  5. 用预编译包安装zabbix-agent

    如果主机无法上网,安装rpm又缺少依赖时,可以通过预编译包进行安装zabbix-agent,下载地址 https://www.zabbix.com/download 下载后,执行如下命令: wget ...

  6. Validation异常:No validator could be found for constraint '.....' validating type 'java.lang.Integer'.

    javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'java ...

  7. 2019-6-23-开源项目使用-appveyor-自动构建

    title author date CreateTime categories 开源项目使用 appveyor 自动构建 lindexi 2019-06-23 11:47:40 +0800 2019- ...

  8. 破解fireworks_cs6、phoneshop_cs6、dreamweaver_cs6

    我的Adobe密码是绿尘枫加**0,首字母大写,在我的百度云盘有这三款软件的补丁,这三款软件安装和破解的方式都一样.先下载正常安装好正版软件>正常试用一遍之后,fireworks的补丁装错了文件 ...

  9. Cython保护Python代码

    注:.pyc也有一定的保护性,容易被反编译出源码... 项目发布时,为防止源码泄露,需要对源码进行一定的保护机制,本文使用Cython将.py文件转为.so进行保护.这一方法,虽仍能被反编译,但难度会 ...

  10. LightOJ 1341 Aladdin and the Flying Carpet【整数分解】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1341 题意: 给定一个数,将其拆分成两个数的乘 ...