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 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3
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(迭代加深搜索)的更多相关文章

  1. UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]

    解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...

  2. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  3. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  4. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  5. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  6. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  7. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  8. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  9. 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神

    题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...

随机推荐

  1. Java 原生网络编程.

    一.概念 Java 语言从其诞生开始,就和网络紧密联系在一起.在 1995 年的 Sun World 大会上,当时占浏览器市场份额绝对领先的网景公司宣布在浏览器中支持Java,从而引起一系列的公司产品 ...

  2. 几点建议帮你写出简洁的JS代码

    译者按: 规范的代码可以有效避免代码bug,fundebug才会报警少一点! 原文: Tips for Writing Cleaner Code 译者: Fundebug 为了保证可读性,本文采用意译 ...

  3. HDU6187(对偶图生成树)

    Destroy Walls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)T ...

  4. 使用Linux的Crontab定时执行PHP脚本

    0 */6 * * * /home/kdb/php/bin/php /home/kdb/apache/htdocs/lklkdbplatform/kdb_release/Crontab/index.p ...

  5. js 字符串转数组

    var obj = "123456".replace(/(.)(?=[^$])/g, "$1,").split(",");    conso ...

  6. javaScript 设计模式之中介者模式示例

    飞机把注册信息放到铁塔里,发送数据到铁塔,报告其它的飞机一些信息. var feiji = function( name ){ this.name = name; } feiji.prototype. ...

  7. Tars 服务调服务

    1,创建一个 tars 服务工程 2,把需要调用的服务的 客户端接口文件 拷贝到当前服务 3,构建通信器 CommunicatorConfig 调用,如果是 springboot 开发客户端可以使用注 ...

  8. vue-cil和webpack中本地静态图片的路径问题解决方案

    1 本地图片动态绑定img的src属性 一般我们在html中或者vue组件文件中引用图片是这样,这是不需要做特别处理的 我们将图片放入assets中或者重新建立个文件夹img什么的都可以,随意- 但是 ...

  9. 数据筛选和API优化

    筛选数据 需求:如果数据库中存在OrderNum相同,且IsDefault不同的记录,那么IsDefault值为0的记录将替换值为1的记录(IsDefault值为1的记录不展示). 由于查出来的数据不 ...

  10. mysql中需要注意的编码问题