UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]
解题思路:
这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是:
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*迭代加深搜索]的更多相关文章
- POJ2286 The Rotation Game[IDA*迭代加深搜索]
The Rotation Game Time Limit: 15000MS Memory Limit: 150000K Total Submissions: 6325 Accepted: 21 ...
- uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索
迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- UVA - 11214 Guarding the Chessboard(迭代加深搜索)
题目: 输入一个n*m的棋盘(n,m<10),某些格子有标记,用最少的皇后守卫(即占据或攻击)所有的标记的格子.输出皇后的个数. 思路: 一开始没有想到用迭代加深搜索,直接dfs结果还没写完就发 ...
- 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 ...
- 埃及分数 迭代加深搜索 IDA*
迭代加深搜索 IDA* 首先枚举当前选择的分数个数上限maxd,进行迭代加深 之后进行估价,假设当前分数之和为a,目标分数为b,当前考虑分数为1/c,那么如果1/c×(maxd - d)< a ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- 7-10Editing aBook uva11212(迭代加深搜索 IDA*)
题意: 给出n( 2<=n<=9) 个乱序的数组 要求拍成升序 每次 剪切一段加上粘贴一段算一次 拍成1 2 3 4 ...n即可 求排序次数 典型的状态空间搜索问题 ...
随机推荐
- Vijos1775 CodeVS1174 NOIP2009 靶形数独
靶形数独 描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z博士请教, Z 博士拿出了他最近发 ...
- 【JZOJ4799】【NOIP2016提高A组模拟9.24】我的快乐时代
题目描述 输入 一行,两个整数l,r . 输出 一行,一个整数,表示第l 天到第r 天的愉悦值的和. 样例输入 64 89 样例输出 1818 数据范围 解法 可以参考数位动态规划的想法. 从个位开始 ...
- 大数据技术之Zookeeper
第1章 Zookeeper入门 1.1 概述 Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目. 1.2 特点 1.3 数据结构 1.4 应用场景 提供的服务包括:统 ...
- nginx与apache
参考链接:https://www.cnblogs.com/changning0822/p/7844004.html
- python 正则表达式简介
- oralce系统触发器
系统事件是指基于oracle事件(例如logon.logoff和startup.shutdown)所建立的触发器,通过使用系统事件触发器,提供了跟踪系统或是数据库变化机制.下面介绍使用的系统事件属性函 ...
- python中Sting字符串
字符串连接 方法1: 用字符串的join方法 a = ['a','b','c','d'] content = '' content = ''.join(a) print content 方法2: 用字 ...
- poj2391 最大流+拆点
题意:F块草坪,上面有n头牛,可以容纳m个牛遮雨.将草坪一份为2,成为二部图. 对于此题,和poj2112很像,只是2112很明显的二部图.这道题就开始敲,但是建图遇到问题,草坪的2个值怎么处理,于是 ...
- MaxCompute 费用暴涨之存储压缩率降低导致SQL输入量变大
现象:同样的SQL,每天处理的数据行数差不多,但是费用突然暴涨甚至会翻数倍. 分析: 我们先明确MaxCompute SQL后付费的计费公式:一条SQL执行的费用=扫描输入量 ️ SQL复杂度 ️ 0 ...
- phpexcel使用说明1
<?php /** * PHPEXCEL生成excel文件 * @author:firmy * @desc 支持任意行列数据生成excel文件,暂未添加单元格样式和对齐 */ require_o ...