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即可 求排序次数 典型的状态空间搜索问题 ...
随机推荐
- php中array_slice和array_splice函数解析方式方法
array_slice array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_k ...
- word Stock Market Indices
Stock Market Indices USA Africa Asia and Pacific Canada Europe Middle East South America Internation ...
- 【JZOJ4790】【NOIP2016提高A组模拟9.21】选数问题
题目描述 在麦克雷的面前有N个数,以及一个R*C的矩阵.现在他的任务是从N个数中取出R*C个,并填入这个矩阵中.矩阵每一行的法值为本行最大值与最小值的差,而整个矩阵的法值为每一行的法值的最大值.现在, ...
- Directx11教程(48) depth/stencil buffer的作用
原文:Directx11教程(48) depth/stencil buffer的作用 在D3D11中,有depth/stencil buffer,它们和framebuffer相对应,如下图所 ...
- 威胁快报|首爆,新披露Jenkins RCE漏洞成ImposterMiner挖矿木马新“跳板”
简介 阿里云安全于近日捕获到一起使用Jenkins RCE漏洞进行攻击的挖矿事件.除挖矿外,攻击者还曾植入具有C&C功能的tsunami木马,也预留了反弹shell的功能,给用户带来极大安全隐 ...
- @划水记@ THUWC2020 (?)
目录 @day -1@ @day 0@ @day 1@ @day 2@ @day 2+@ @day 3@ @day ?@ @day -1@ 听闻 THUWC 在 12 月举行的消息,突然就停了大概一周 ...
- 洛谷1014 Cantor表
水题.随便搞搞就过了. //Serene #include<algorithm> #include<iostream> #include<cstring> #i ...
- bzoj1911 特别行动队
Description Input Output Sample Input 4 -1 10 -20 2 2 3 4 Sample Output 9 斜率优化 推式子 #include< ...
- laravel 参数设置
路由命名 Route::get('user/{id?}', function ($id = 1) { return "用户ID: " . $id; })->name('use ...
- OpenStack宣布用Kubernetes重写底层编排引擎
Mirantis是OpenStack的主要贡献者,今天他宣布将使用Kubernetes作为底层编排引擎重写其私有云平台.我们认为这是推进OpenStack和Kubernetes 社区伟大的一步. Op ...