UVA1434-The Rotation Game(迭代加深搜索)
Problem UVA1434-The Rotation Game
Accept:2209 Submit:203
Time Limit: 3000 mSec
Problem Description

Input
The input consists of no more than 30 test cases. Each test case has only one line that contains 24 numbers, which are the symbols of the blocks in the initial configuration. The rows of blocks are listed from top to bottom. For each row the blocks are listed from left to right. The numbers are separated by spaces. For example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There are no blank lines between cases. There is a line containing a single ‘0’ after the last test case that ends the input.
Output
For each test case, you must output two lines. The first line contains all the moves needed to reach the final configuration. Each move is a letter, ranging from ‘A’ to ‘H’, and there should not be any spaces between the letters in the line. If no moves are needed, output ‘No moves needed’ instead. In the second line, you must output the symbol of the blocks in the center square after these moves. If there are several possible solutions, you must output the one that uses the least number of moves. If there is still more than one possible solution, you must output the solution that is smallest in dictionary order for the letters of the moves. There is no need to output blank lines between cases.
Sample Input
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
0
Sample Ouput
AC
2
DDHH
2
题解:做了两道IDA*的题,对这个算法有了一个初步的印象,感觉挺强大的,而且代码短,思路清晰。
这个题搜索的框架很简单(IDA*的好处),但是具体实现起来不太容易。第一个技巧,用静态数组来搞定旋转的问题,第二个技巧,使用rev数组来精简代码。
rev数组不只是节省了一些代码量,而且可以轻松实现回溯时数组的复原,否则还要先把原数组存到一个另一个数组里,回溯时再copy回来,省了空间和时间,非常巧妙。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; /*
00 01
02 03
04 05 06 07 08 09 10
11 12
13 14 15 16 17 18 19
20 21
22 23
*/ const int maxn = ;
int num[maxn];
char ans[];
int maxd; int line[][] =
{
{ , , ,,,,},
{ , , ,,,,},
{, , , , , , },
{,,,,,,},
}; const int rev[] = { ,,,,,,, };
const int center[] = { ,,,,,,, }; bool is_ok() {
int t = num[center[]];
for (int i = ; i < ; i++) {
if (num[center[i]] != t) return false;
}
return true;
} int cal(int tar) {
int cnt = ;
for (int i = ; i < ; i++) {
if (num[center[i]] != tar) cnt++;
}
return cnt;
} inline int h() {
return min(min(cal(), cal()), cal());
} void move(int i) {
int tmp = num[line[i][]];
for (int j = ; j < ; j++) {
num[line[i][j]] = num[line[i][j + ]];
}
num[line[i][]] = tmp;
} bool dfs(int d) {
if (is_ok()) {
ans[d] = '\0';
printf("%s\n", ans);
return true;
}
if (d + h() > maxd) {
return false;
}
for (int i = ; i < ; i++) {
ans[d] = 'A' + i;
move(i);
if (dfs(d + )) {
return true;
}
move(rev[i]);
}
return false;
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
line[i][j] = line[rev[i]][ - j];
}
}
while (scanf("%d", &num[]) != - && num[]) {
for (int i = ; i < ; i++) {
scanf("%d", &num[i]);
}
for (int i = ; i < ; i++) {
if (!num[i]) return ;
} if (is_ok()) {
printf("No moves needed\n");
}
else {
for (maxd = ;; maxd++) {
if (dfs()) break;
}
}
printf("%d\n", num[center[]]);
}
return ;
}
UVA1434-The Rotation Game(迭代加深搜索)的更多相关文章
- UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]
解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神
题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...
随机推荐
- 汇编语言--微机CPU的指令系统(五)(位操作指令)
(6) 位操作指令 1.位扫描指令(Bit Scan Instruction) 指令的格式:BSF/BSR Reg, Reg/Mem ;80386+ 受影响的标志位:ZF 位扫描指令是在第二个操作数中 ...
- Java并发编程学习:volatile关键字解析
转载:https://www.cnblogs.com/dolphin0520/p/3920373.html 写的非常棒,好东西要分享一下 Java并发编程:volatile关键字解析 volatile ...
- JAVA 多线程(2)
一.首先 Thread 是实现了Runable 接口的类 理论上Thread 实例与直接实现runable接口的实例运行起来没有什么不同,但是由于JAVA 是单继承,所以如果想再一个类中实现2个不同的 ...
- 二进制安装 kubernetes 1.12(二) - 安装docker, 部署Flannel网络
在 node 节点上安装 docker 参考 https://www.cnblogs.com/klvchen/p/8468855.html Flannel 工作原理: 部署Flannel网络 在 ma ...
- 2018-10-04 [日常]用Python读取word文档中的表格并比较
最近想对某些word文档(docx)的表格内容作比较, 于是找了一下相关工具. 参考Automate the Boring Stuff with Python中的word部分, 试用了python-d ...
- 在PeopleSoft中,什么是AlterAudit,Sysaudit和DDDAudit报告
Alter Audit-是一个进程,它标识任何需要SQL Alter process的记录.即:如果AD中定义的record与数据库的中定义不匹配则标识该记录为应该修改. SQL Alter-AD中的 ...
- (后端)springboot 在idea中实现热部署(转)
自己用到了iIntelliJ IDEA 这个ide工具,但是和以前的工具写html,css,js直接刷新页面不同,这个需要去热部署,网上搜的解决方法: SpringBoot的web项目,在每一次修改了 ...
- 这几天上海移动网络可以直接打开 Google Play 了
这几天上海移动网络可以直接打开 Google Play (谷歌应用商店)了. 速度还不错.基本无延迟. 想当初,为了防止国内应用市场里的木马或恶意软件,想从 Google Play 应用市场下载,折腾 ...
- 使用VSTS的Git进行版本控制(七)——管理仓库
使用VSTS的Git进行版本控制(七)--管理仓库 在团队项目中创建Git repo管理项目的源代码.每个Git repo都有自己的权限和分支,可以与项目中的其他工作隔离开来. 任务1:从web门户创 ...
- 智能POS承接口碑点餐FAQ
1.一体机正餐后付(正餐6.0.0.8),口碑订单在商家后台无优惠(小票上有),但实收不等于订单金额. 原因:订单回流问题(线上线下数据没有及时回流造成的):造成这个问题的操作: 1.在线下加菜线上同 ...