IDA*算法,即迭代加深的A*算法。实际上就是迭代加深+DFS+估价函数

题目传送:The Rotation Game

AC代码:

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#define LL long long
#define INF 0x7fffffff
using namespace std; int mp[25]; int n; int pos[] = {7, 8, 9, 12, 13, 16, 17, 18}; int depth;
int ans_num;
char ans[205]; bool is_ok(int *g) {//推断是否已达到结果
int t = g[7];
if(t == g[8] && t == g[9] && t == g[12] && t == g[13] && t == g[16] && t == g[17] && t == g[18]) {
return true;
}
return false;
} void change_state(int *g, int a1, int a2, int a3, int a4, int a5, int a6, int a7) {//状态转换(这里就是数字移位)
int tmp = g[a1];
g[a1] = g[a2]; g[a2] = g[a3]; g[a3] = g[a4];
g[a4] = g[a5]; g[a5] = g[a6]; g[a6] = g[a7];
g[a7] = tmp;
} int get_maxnum(int *g) {//获取中间8个数之间出现次数最大的那个数的次数
int cnt[4];
cnt[1] = cnt[2] = cnt[3] = 0;
for(int i = 0; i < 8; i ++) {
cnt[g[pos[i]]] ++;
}
return max(cnt[1], max(cnt[2], cnt[3]));
} //IDA*算法的核心即为DFS+迭代加深+估价函数
int dfs(int *g, int cur_depth, int pre_dir) {
if(depth - cur_depth < 8 - get_maxnum(g)) { //相似于估价函数,此处由于每次数字移位最多仅仅能使得中间的数字多一个一样的。
return 0; //而每次搜索相应一次数字移位。而当搜索次数小于8个数中要改变得几个数时。肯定不正确。 剪枝①
} if(cur_depth >= depth) {//迭代加深搜索的精髓
return 0;
} int tmp[25];
for(int i = 1; i <= 8; i ++) {//往八个方向搜索
if((i == 1 && pre_dir == 6) || (i == 6 && pre_dir == 1)) continue;//下面都是减去和前一个移位的方向相反方向的情况。剪枝②
if((i == 2 && pre_dir == 5) || (i == 5 && pre_dir == 2)) continue;
if((i == 3 && pre_dir == 8) || (i == 8 && pre_dir == 3)) continue;
if((i == 4 && pre_dir == 7) || (i == 7 && pre_dir == 4)) continue; for(int j = 1; j <= 24; j ++) tmp[j] = g[j]; switch(i) {
case 1: ans[cur_depth] = 'A'; change_state(tmp, 1, 3, 7, 12, 16, 21, 23); break;
case 2: ans[cur_depth] = 'B'; change_state(tmp, 2, 4, 9, 13, 18, 22, 24); break;
case 3: ans[cur_depth] = 'C'; change_state(tmp, 11, 10, 9, 8, 7, 6, 5); break;
case 4: ans[cur_depth] = 'D'; change_state(tmp, 20, 19, 18, 17, 16, 15, 14); break;
case 5: ans[cur_depth] = 'E'; change_state(tmp, 24, 22, 18, 13, 9, 4, 2); break;
case 6: ans[cur_depth] = 'F'; change_state(tmp, 23, 21, 16, 12, 7, 3, 1); break;
case 7: ans[cur_depth] = 'G'; change_state(tmp, 14, 15, 16, 17, 18, 19, 20); break;
case 8: ans[cur_depth] = 'H'; change_state(tmp, 5, 6, 7, 8, 9, 10, 11); break;
}
if(is_ok(tmp)) {
ans_num = tmp[7];
ans[cur_depth + 1] = '\0';
return 1;
}
if(dfs(tmp, cur_depth + 1, i)) return 1;
}
return 0;
} int main() {
int x;
while(1) {
scanf("%d", &mp[1]); if(mp[1] == 0) {
break;
} for(int i = 2; i <= 24; i ++) {
scanf("%d", &mp[i]);
} if(is_ok(mp)) {
printf("No moves needed\n");
printf("%d\n", mp[7]);
continue;
} depth = 1;
while(1) {
if(dfs(mp, 0, -1)) {
break;
}
depth ++;
} printf("%s\n", ans);
printf("%d\n", ans_num); }
return 0;
}

POJ - 2286 - The Rotation Game (IDA*)的更多相关文章

  1. POJ 2286 The Rotation Game(IDA*)

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

  2. POJ2286 The Rotation Game(IDA*)

    The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 5691   Accepted: 19 ...

  3. UVA-1343 The Rotation Game (IDA*)

    题目大意:数字1,2,3都有八个,求出最少的旋转次数使得图形中间八个数相同.旋转规则:对于每一长行或每一长列,每次旋转就是将数据向头的位置移动一位,头上的数放置到尾部.若次数相同,则找出字典序最小旋转 ...

  4. 【UVa】1343 The Rotation Game(IDA*)

    题目 题目     分析 lrj代码.... 还有is_final是保留字,害的我CE了好几发.     代码 #include <cstdio> #include <algorit ...

  5. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  6. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  7. POJ.3087 Shuffle'm Up (模拟)

    POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...

  8. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  9. Booksort POJ - 3460 (IDA*)

    Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...

随机推荐

  1. 中小型WEB系统权限日志数据表设计

    中小型WEB系统权限日志数据表设计 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjc1MDU3OA==/font/5a6L5L2T/fontsi ...

  2. Tomcat之——配置项目有虚拟路径

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47024863 非常easy,在Tomcat的Server.xml文件里的Host节 ...

  3. 各种join一目了然: join 、inner join、left join 、right join、full join

    各种join一幅图一目了然 一下每幅图都是指: A * join B on A.id = B.in 这个帖子也非常形象.比較好:http://www.phpddt.com/db/inner_join- ...

  4. c# 查询sql 返回多个參数

    1.依据须要查询mysql 语句,返回三个须要的參数,不是数据集 2.编写函数例如以下: public static void GetParas(string 条件1, out string 返回值1 ...

  5. Android学习笔记技巧之垂直和水平滚动视图

    <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android=" ...

  6. Zabbix监控告警

    一 钉钉告警 1.1.1 添加钉钉机器人 发起群聊 创建完群聊选择,机器人管理 选择你要绑定的群聊 复制下面地址留用 1.1.2 编写钉钉告警脚本 安装requests库,HTTP客户端, # yum ...

  7. ELK之日志查询、收集与分析系统

    项目由来 (1)开发人员不能登录线上服务器查看详细日志,经过运维周转费时费力 (2)日志数据分散在多个系统,难以查找与整合 (3)日志数据量巨大,查询速度太慢,无法满足需求 (4)无法全局掌控项目运行 ...

  8. Vue Cli 打包之后静态资源路径不对的解决方法

    cli2版本: 将 config/index.js 里的 assetsPublicPath 的值改为 './' . build: { ... assetsPublicPath: './', ... } ...

  9. code-代码平台服务器路径

    下面记录的是各个平台的服务器路径(va使用) ("repo init -u ssh://vanzo/platform_89/manifest.git") ("repo i ...

  10. Android学习笔记进阶八之Matrix矩阵

    Matrix,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 在Android里面,Matrix由9个float值构成,是一个3*3的矩阵.最好记住.如下图: ...