【搜索进阶】HDU 1667 The Rotation Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1667
IDA*的入门题目,状态过多容易超内存,正好体现了IDA*的优势,每次操作移动能使一个数字进入中间的八个位置,所以构造h()=8-max(1,2,3在中间8个位置的个数)
#include<cstdio>
#include<cstring>
using namespace std;
int maze[10][10]={0};
bool check(){if(maze[3][3]!=maze[3][4]) return false;if(maze[3][3]!=maze[3][5]) return false;if(maze[3][3]!=maze[4][3]) return false;if(maze[3][3]!=maze[5][3]) return false;if(maze[3][3]!=maze[5][4]) return false;if(maze[3][3]!=maze[5][5]) return false;if(maze[3][3]!=maze[4][5]) return false;return true;
}
void roA(){int tmp=maze[1][3];for(int i=1;i<=6;i++) maze[i][3]=maze[i+1][3];maze[7][3]=tmp;
}
void roB(){int tmp=maze[1][5];for(int i=1;i<=6;i++) maze[i][5]=maze[i+1][5];maze[7][5]=tmp;
}
void roC(){int tmp=maze[3][7];for(int i=7;i>=2;i--) maze[3][i]=maze[3][i-1];maze[3][1]=tmp;
}
void roD(){int tmp=maze[5][7];for(int i=7;i>=2;i--) maze[5][i]=maze[5][i-1];maze[5][1]=tmp;
}
void roE(){int tmp=maze[7][5];for(int i=7;i>=2;i--) maze[i][5]=maze[i-1][5];maze[1][5]=tmp;
}
void roF(){int tmp=maze[7][3];for(int i=7;i>=2;i--) maze[i][3]=maze[i-1][3];maze[1][3]=tmp;
}
void roG(){int tmp=maze[5][1];for(int i=1;i<=6;i++) maze[5][i]=maze[5][i+1];maze[5][7]=tmp;
}
void roH(){int tmp=maze[3][1];for(int i=1;i<=6;i++) maze[3][i]=maze[3][i+1];maze[3][7]=tmp;
}
int h(){int a[4]={0};for(int i=3;i<=5;i++){a[maze[3][i]]++;a[maze[5][i]]++;}a[maze[4][3]]++;a[maze[4][5]]++; int max=0;for(int i=1;i<=3;i++){if(max<a[i]) max=a[i];}return 8-max;
}
char ans[100];
int deep;
bool dfs(int d){if(d==deep) return check();if(h()+d>deep) return false;roA();ans[d]='A';if(dfs(d+1)) return true;roF();roB();ans[d]='B';if(dfs(d+1)) return true;roE();roC();ans[d]='C';if(dfs(d+1)) return true;roH(); roD();ans[d]='D';if(dfs(d+1)) return true;roG(); roE();ans[d]='E';if(dfs(d+1)) return true;roB(); roF();ans[d]='F';if(dfs(d+1)) return true;roA();roG();ans[d]='G';if(dfs(d+1)) return true;roD();roH();ans[d]='H';if(dfs(d+1)) return true;roC();return false;
}
int main(){for(int i=1;i<=7;i++) maze[3][i]=maze[5][i]=1;for(int i=1;i<=7;i++) maze[i][3]=maze[i][5]=1;while(true){for(int i=1;i<=7;i++){for(int j=1;j<=7;j++){if(maze[i][j]==0) continue;scanf("%d",&maze[i][j]);if(maze[i][j]==0) return 0;}}if(check()){puts("No moves needed");printf("%d\n",maze[3][3]);continue;}deep=1;while(true){if(dfs(0)) break;deep++;}for(int i=0;i<deep;i++) putchar(ans[i]);puts("");printf("%d\n",maze[3][3]);}return 0;
}
【搜索进阶】HDU 1667 The Rotation Game相关推荐
- HDU 1667 The Rotation Game
参考文章:http://blog.csdn.net/urecvbnkuhbh_54245df/article/details/5856756 The portal:http://acm.hdu.edu ...
- hdu 1667:The Rotation Game
题目可以看IDA*学习笔记-HDU1667 The Rotation Game_ex_voda的博客-CSDN博客 关于如何存储这个条状数组,我们开一个大的矩阵数组装下来就行了.当然,特殊坐标单独开两 ...
- 专题训练二 搜索进阶 HDU - 3085 Nightmare Ⅱ (双向BFS + 曼哈顿距离)
HDU - 3085 Nightmare Ⅱ (双向BFS + 曼哈顿距离) Problem Description Last night, little erriyue had a horrible ...
- hdu 1667 The Rotation Game(IDA*)
题目大意: 有一个井字形结构的图形,每条线上7个数字(1,2,3,),每次可以从八个方向将一条线上的数字循环移动1个距离,问经过最少几次什么操作,可以将图形中间的八个方块变成同一种数字. 解题思路: ...
- HDU 1667 The Rotation Game (A*迭代搜索)
题目大意:略 每次选择一个最大深度K,跑IDA* 估价函数H=8-中间8个格里出现次数最多的数的个数x,即把它填满这个数最少需要8-x次操作,如果dep+H>K,就跳出.. 深搜的时候暴力修改, ...
- [kuangbin]专题二 搜索进阶 Escape HDU - 3533【BFS】
[题目描述] The students of the HEU are maneuvering for their military training. The red army and the blu ...
- HUD 1043 Eight 八数码问题 A*算法 1667 The Rotation Game IDA*算法
先是这周是搜索的题,网站:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6041 主要内容是BFS,A*,IDA*,还有一道K短路的,.. ...
- 图论:SPFA 算法详解( 算法竞赛入门到进阶) HDU 2544 链式前向星 【提供ACM模板+图解,不会都难!】
文章目录 SPFA简介 链式前向星介绍 SPFA算法思路详细 模板-链式前向星 参考书籍:算法竞赛入门到进阶 罗勇军 SPFA简介 用队列处理Bellman-Ford算法可以很好地优化,这种方法叫做S ...
- hdu 1667(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1667 思路:大牛说是IDA*的入门题=.=构造h()=8-max(1,2,3); max(1,2,3 ...
最新文章
- easyui validate 后 使用ajax提交
- php 循环写入 缓冲,php利用缓冲实现动态输出(flush,ob_flush)
- C# 泛型可能导致的装箱操作陷阱
- 在Linux下用gdb检测内核rootkit
- 计网 - TCP 实战:如何进行 TCP 抓包调试?
- FreeBSD8.0搭建Apache+PHP+MySql平台
- JavaScript闭包小窥
- Oracle学习(十六)Oracle安装
- 从XML文件读取数据绑定到列表控件2
- 如何更新Jenkins作业发布config.xml
- html文档主体的根标签,2 HTML简介标签嵌套和并列关系文档声明
- ML Tools List
- PyTorch 靠谱的模型可视化教程
- python实现归并排序_排序算法学习(python版本)之归并排序(MergeSort)
- matlab仿真心型函数,matlab绘制心形函数
- English trip M1 - AC11 I Dreamed a Dream? 我做了一个梦 Teacher:Lamb
- 公众号第三方平台开发 教程五 代公众号处理消息和事件
- 【WPS】未安装VBA支持库,无法运行文档中的宏。如需要启用宏功能,请点击这里了解详情。
- 操作系统原理实验(5):内存管理
- 基于Unity开发实现的坦克游戏设计
热门文章
- matlab之字体上下标控制指令
- Java SE笔记(其三)
- mysql providername_providername是什么 如何解决数据库切换中DBProviderName配置问题
- 计算机合成图像 英文翻译,电脑影像合成
- MySQL常用增删改查语法命令
- Cesium深入浅出之3dtiles渲染
- iOS 最新版本调用相机与相册
- 2-(2,4-二氟苯基)吡啶铱二氯桥(dfppy)2Ir(μ-Cl)2(dfppy)2)|(tfmppy)2Ir(L1~L7)的磷光铱配合物合成试剂
- linux mysql 主从 1130_linux下安装Mysql(干货!!!)解决mysql 1130问题,远程登录问题...
- 【渝粤题库】广东开放大学 跨境电商实务之外贸B2B平台操作 形成性考核