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. windows10系统盘瘦身

  2. js 数据类型具体分析

            复习 点运算符 xxx.sss xxx是对象  sss是属性和方法.任何数据类型都是拥有属性和方法的.字符串 String var st=“hello world”.字符串的定义    ...

  3. Flask 系列之 Bootstrap-Flask

    说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 通过使用 Bootstrap-Flask 来进行页面美化 ...

  4. TCP连接与释放

    TCP连接的建立 三次握手 TCP服务器进程先创建传输控制块TCB,时刻准备接受客户进程的连接请求,此时服务器就进入了LISTEN(监听)状态. TCP客户进程也是先创建传输控制块TCB,然后向服务器 ...

  5. js 实现 0-9 随机排序

    function randomsort(a, b) {return Math.random()>0.5 ? -1 : 1;//用Math.random()函数生成0~1之间的随机数与0.5比较, ...

  6. JS中的柯里化(currying)

    何为Curry化/柯里化? curry化来源与数学家 Haskell Curry的名字 (编程语言 Haskell也是以他的名字命名). 柯里化通常也称部分求值,其含义是给函数分步传递参数,每次传递参 ...

  7. 洛谷P4170 [CQOI2007]涂色(区间dp)

    题意 题目链接 Sol 震惊,某知名竞赛网站竟照搬省选原题! 裸的区间dp,\(f[l][r]\)表示干掉\([l, r]\)的最小花费,昨天写的时候比较困于是就把能想到的转移都写了.. // luo ...

  8. AWS专线服务总结和疑问

    1.AWS专线服务的入口, 从介绍页上可以看到,有如下功能: (1)专线可以连接AWS云主机和传统的数据中心或者分支机构. (2)专线可以连接AWS云主机和托管区的主机. 连接要素: (1)需要使用V ...

  9. python安装小结

    一.python下载地址:http://www.activestate.com/activepython/downloads 二.1.没有安装request会出一下错误: 2.解决办法:pip ins ...

  10. Vue之axios请求数据

    引入文件 <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"> ...