1. 题目描述
有个#字型的条带,可以从横线或竖线进行循环移动,求通过各种移动最终使中心的8个字符全等的长度最短并相同长度字典序最小的操作序列。

2. 基本思路
24个数据,8种移动方式,数据量很小了,所以基本怎么玩儿都可以。
需要注意的是因为横线竖线间有交点,所以每个条带的数据可能都是变化的。
采用IDA*算法可解。
所谓IDA*,就是不断对所求操作需要长度进行增加,然后不断当前长度是否存在可行的操作序列。
判断是否存在可行操作序列的方法是深搜,这里需要注意去除先移动A紧接着移动F的类似情况。
H函数的基本想法很简单,即判断中心8个字符的最少改变几个就可以满足条件。

3. 代码

 /* 1667 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int INF = 0x3f3f3f3f;
const int maxl = ;
int op[maxl];
int a[];
int pos[][] = {
{, , , , , , },
{, , , , , , },
{, , , , , , },
{, , , , , , }
};
int eight[] = {
, , , , , , ,
};
int mirror[] = {
, , , , , , ,
};
bool flag;
int v; void Init() {
rep(i, , ) {
int j = mirror[i];
memcpy(pos[j], pos[i], sizeof(pos[j]));
reverse(pos[j], pos[j]+);
}
} void Rotate(int *a, int d) {
int tmp = a[pos[d][]];
rep(i, , )
a[pos[d][i-]] = a[pos[d][i]];
a[pos[d][]] = tmp;
} int H(int *a) {
int c[]; c[] = c[] = c[] = ;
rep(i, , )
++c[a[eight[i]]]; return - max(c[], max(c[], c[]));
} void dfs(int *a, int dep, int fa) {
int mn = H(a); if (dep < mn)
return ; if (dep == ) {
if (mn == ) {
flag = true;
v = a[eight[]];
}
return ;
} int b[]; rep(i, , ) {
if (mirror[i] == fa)
continue;
op[dep] = i;
memcpy(b, a, sizeof(b));
Rotate(b, i);
dfs(b, dep-, i);
if (flag) return ;
}
} void solve() {
int mn = H(a); if (mn == ) {
printf("No moves needed\n%d\n", a[eight[]]);
return ;
} flag = false;
for (int l=mn; ; ++l) {
dfs(a, l, -);
if (flag) {
per(i, , l+) {
putchar('A'+op[i]);
}
printf("\n%d\n", v);
break;
}
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif Init();
while (scanf("%d", &a[])!=EOF && a[]) {
rep(i, , )
scanf("%d", &a[i]);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

4. 数据生成器。

/*
2 3 2 2 1 1 3 1 3 2 2 1 2 2 2 3 3 2 3 1 2 2 2 2
2 3 2 1 2 1 1 2 1 2 2 3 2 2 2 2 2 2 1 3 3 2 2 2
1 1 3 1 3 3 1 3 3 3 1 3 2 1 2 2 1 3 3 3 1 3 3 3
1 2 1 1 1 2 1 2 1 3 1 2 1 2 2 1 1 1 1 1 2 1 3 1
3 2 3 3 3 2 3 3 1 2 3 3 3 1 3 1 2 1 3 3 2 3 2 3
2 2 3 2 3 3 2 1 3 2 2 3 1 2 2 2 2 2 1 2 1 2 3 2
3 2 1 1 3 3 3 1 2 3 3 3 2 3 1 3 3 3 3 3 1 1 3 3
3 2 3 3 3 2 2 1 1 3 1 3 3 3 2 3 3 3 1 3 3 3 3 2
1 3 2 1 1 3 1 1 1 3 3 1 1 1 3 3 3 2 1 1 2 1 1 2
1 1 3 3 2 3 1 3 2 3 2 1 3 1 2 2 2 1 1 2 2 2 3 2
0
*/ /*
AABDDHA
2
BFFF
2
BBGGH
3
AGHBH
1
GBGF
3
ABBHHF
2
BCCEC
3
BHA
3
EGG
1
ADHBBH
2
*/
 from copy import deepcopy
from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 10
bound = 10**5
# fout.write("%d\n" % (t))
for tt in xrange(t):
n1 = randint(10, 15)
token = randint(1, 3)
L = [token for i in xrange(n1)]
op = range(1, 4)
op.remove(token)
for i in xrange(24-n1):
idx = randint(0, 1)
x = op[idx]
L.append(x)
shuffle(L)
fout.write(" ".join(map(str, L)) + "\n")
fout.write("0\n") def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()

【HDOJ】1667 The Rotation Game的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  3. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  4. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  5. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  6. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  7. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  8. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. HTML邮件制作规范

    以下内容有些是别人总结的,有些是自己在工作中总结的. 模板最佳尺寸:显示宽度550px-750px,模板高度控制在一屏以内. 1. 用table+css方式构建模板 Div+css布局不完全被邮件客户 ...

  2. Building microservices with Spring Cloud and Netflix OSS, part 2

    In Part 1 we used core components in Spring Cloud and Netflix OSS, i.e. Eureka, Ribbon and Zuul, to ...

  3. 排序,求几个最值问题,输入n个整数,输出其中最小的k个元素。

    看完两个求最大值算法之后的一些感想. 如果想直接看算法的可以跳过.但是我觉得我这些想法还是比较有用的,至少对我将来的算法设计是这样的. 算法的功能越强大,必然意味着速度慢,因为根据丛林法则,那种慢又功 ...

  4. webpack减少打包后文件体积的几种方法

    webpack 把我们所有的文件都打包成一个 JS 文件,这样即使你是小项目,打包后的文件也会非常大.下面就来讲下如何从多个方面进行优化. 去除不必要的插件 刚开始用 webpack 的时候,开发环境 ...

  5. 在后台对GameObject进行"创建"||"删除"动作

    在后台对GameObject进行"创建"||"删除"动作 建立 public GameObject Pre;//在编辑器中用来绑定的Prefabs public ...

  6. 自动化运维——一键安装MySQL

    根据项目需要,前段时间在搞EMM系统各种安装包的自动化部署工作,主要包括一键安装和一键启动\停止功能.总结记录下来,以供后用. 本文主要是自动安装MySQL5.7.11版,Linux版脚本在CentO ...

  7. Java中的异常处理(一)

    package second; public class C { public static void main(String[] args){ String name = null;//定义一个nu ...

  8. Mapped Statements collection does not contain value for ResearcherMapper.方法名

    搞了半天, 原来是映射文件没加, 新手常遇到的问题. Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原 ...

  9. 基于Ogre的DeferredShading(延迟渲染)的实现以及应用

    http://blog.sina.com.cn/s/blog_458f871201017i06.html 这篇文章写的不错,从比较宏观的角度说了下作者在OGRE中实现的延迟渲染

  10. 深层解析:构建facebook应用商店推荐引擎

    Under the Hood: Building the App Center recommendation engine   As more apps on Facebook Platform ha ...