状态压缩+BFS。

 /* 3088 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; #define MAXN 60000
#define MAXL 11 typedef struct {
char s[MAXL];
int t;
} node_t; node_t bn;
bool visit[MAXN];
int end[];
int len; int cal(char *s) {
int i, ret = ; for (i=; i<len; ++i)
ret = *ret + s[i];
return ret;
} int bfs() {
int i, j, k, v;
int pre, cur;
queue<node_t> Q;
node_t nd; v = cal(bn.s);
if (v == end[] || v==end[] || v==end[])
return ;
memset(visit, false, sizeof(visit));
visit[v] = true;
Q.push(bn); while (!Q.empty()) {
nd = Q.front();
Q.pop();
for (i=; i<len; ++i) {
if (nd.s[i] == nd.s[i-])
continue;
pre = nd.s[i-];
cur = nd.s[i];
nd.s[i] = nd.s[i-] = -(pre+cur);
v = cal(nd.s);
if (visit[v] == false) {
if (v==end[] || v==end[] || v==end[])
return nd.t + ;
++nd.t;
visit[v] = true;
Q.push(nd);
--nd.t;
}
nd.s[i-] = pre;
nd.s[i] = cur;
}
} return -;
} void init() {
int i, j, k; end[] = ;
for (i=; i<; ++i) {
k = ;
for (j=; j<len; ++j)
k = *k + i;
end[i] = k;
}
} int main() {
int t;
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d", &t);
bn.t = ;
while (t--) {
scanf("%s", bn.s);
len = strlen(bn.s);
init();
for (i=; i<len; ++i) {
if (bn.s[i] == 'r') bn.s[i] = ;
if (bn.s[i] == 'g') bn.s[i] = ;
if (bn.s[i] == 'b') bn.s[i] = ;
}
k = bfs();
if (k < )
puts("No solution!");
else
printf("%d\n", k);
} return ;
}

【HDOJ】3088 WORM的更多相关文章

  1. 【HDOJ】2782 The Worm Turns

    DFS. /* 2782 */ #include <iostream> #include <queue> #include <cstdio> #include &l ...

  2. 【HDOJ】4729 An Easy Problem for Elfness

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

  3. 【HDOJ】【3506】Monkey Party

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

  4. 【HDOJ】【3516】Tree Construction

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

  5. 【HDOJ】【3480】Division

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

  6. 【HDOJ】【2829】Lawrence

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

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

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

  8. 【HDOJ】【3530】Subsequence

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

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

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

随机推荐

  1. Linux命令之dot - 绘制DOT语言脚本描述的图形

    本文链接:http://codingstandards.iteye.com/blog/840055 用途说明 Graphviz (Graph Visualization Software的缩写)是一个 ...

  2. 初步掌握MapReduce的架构及原理

    目录 1.MapReduce定义 2.MapReduce来源 3.MapReduce特点 4.MapReduce实例 5.MapReduce编程模型 6.MapReduce 内部逻辑 7.MapRed ...

  3. Driving the Activity Lifecycle

    Before Robolectric 2.2, most tests created Activities by calling constructors directly, (new MyActiv ...

  4. Java 原始数据类型转换

    在开发中经常遇到数据类型转换的问题,大多数都是拿来强制转换,强制转换可能会出现你意想不到的问题: int a = -1; 我们经过多重转换之后:int b = (int)(char)(byte) a ...

  5. excel中VBA对多个文件的操作

    添加引用 "Scripting.FileSystemObject" (Microsoft Scripting Runtime) '用于操作文件.目录 Sub 数据整理部分() ' ...

  6. 【转】iOS开发UI篇—程序启动原理和UIApplication

    原文 http://www.cnblogs.com/wendingding/p/3766347.html   一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程 ...

  7. 远程推送,集成极光的SDK,证书制造

    由于iOS操作系统限制,我们APP在后台不能做操作,也不能接收任何数据,所以需要用推送来接收消息. APNs服务,苹果官方网址:https://developer.apple.com/library/ ...

  8. iOS 获取通讯录里边的电话号码AddressBook

    1  首先导入库 <AddressBook/AddressBook.h> 2 然后在导入#import <AddressBook/AddressBook.h>文件 3 声明   ...

  9. Codeforces Round #287 D.The Maths Lecture

    The Maths Lecture 题意:求存在后缀Si mod k =0,的n位数的数目.(n <=1000,k<=100); 用f[i][j]代表 长为i位,模k等于j的数的个数. 可 ...

  10. fish code

    <embed width="272" height="180" type="application/x-shockwave-flash" ...