There is a boy named God Wu in UESTC ACM team. One day he is asked to finish a task. The task is that he has to paint a wall as the given pattern. The wall can be represented as a 2×n grid and each grid will be painted only one color. You know God Wu is a God, so he has a brush which could paint a rectangular area of the wall at a single step. As we all know, the paint could be overlap and the newly painted color will replace the older one.

God Wu is so lazy that he always want to finish something in the least steps. At first, the wall was not painted until God Wu paint some colors on it. For a given pattern of the wall, God Wu has to find out the minimum possible number of painting steps to make the wall the same as the given pattern.

Input

In the input file, the first line contains the number of test cases.

For each test case, the first contains only one integer n(1≤n≤8) indicating the length of the wall.

Then follows two lines, denoting the expected pattern of the wall. Every grid is painted by a color which is represented by a single capital letter. You can see the Input Sample for more details.

Output

For each test case, output only one integer denoting the minimum number of steps.

Sample input and output

Sample Input Sample Output
3
3
ABA
CBC
3
BAA
CCB
3
BBB
BAB
Case #1: 3
Case #2: 3
Case #3: 2

Source

UESTC Training for Search Algorithm
 
解题报告
暴力枚举涂法,注意二进制的优先级。。TLE
改进:
我们设计一个A*算法,g(x) = 目前的涂色次数 , h(x) = 当前的不对颜色种类.
很容易得到 f(x) = g(x) + h(x)是满足单调性的
证明:
 设在某步时g(n) = x,h(n) = y;
 在下一步时必然有 g(n') = x + 1 , 且h(n') = y or y-1, 因为你每次只能消除一种颜色,甚至于等于没消
 故 c(n,n') + h(n') - h(n) = 1 + (y or y-1) -y -> min = 0 >=0
 满足相容性,得证

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std; char g[];
int len,all;
bool vis[];
int target;
int caculate[] = {,,,,,,,,,,,,,,,,};
typedef struct status
{
int st,step,h;
friend bool operator < (status a,status b)
{
if (a.step + a.h < b.step + b.h)
return false;
if (a.step + a.h == b.step + b.h && a.step < b.step)
return false;
return true;
} }; priority_queue<status>q; int A(status &x)
{
bool ll[];
int result = ;
memset(ll,false,sizeof(ll));
for(int i = ; i < all;++i)
if ( (!(x.st & ( << i))) && !ll[g[i]-'A'])
{
result ++;
ll[g[i]-'A'] = true;
}
return result;
} int bfs()
{
status start;
start.step = ,start.st = ;
start.h = A(start);
q.push(start);
vis[] = true;
while(!q.empty())
{
status ss = q.top();q.pop();
if (ss.st == target) return ss.step;
for(int i = ; i <len;++i)
for(int j = ;j <= len-i;++j)
{
char id;
for(int h = ;h< j;++h)
{
id = g[i+h];
int t = ss.st;
for(int v = ; v < j ;++ v)
if(g[i+v] != id)
t &= ~( << (i+v));
else
t |= ( << (i+v));
if (!vis[t])
{
status ns;
ns.step = ss.step + ;
ns.st = t;
ns.h = A(ns);
q.push(ns);
vis[t] = true;
}
} for(int h = ;h< j;++h)
{
id = g[i+h+len];
int t = ss.st;
for(int v = ; v < j ;++ v)
if(g[i+v+len] != id)
t &= ~( << (i+v+len));
else
t |= ( << (i+v+len));
if (!vis[t])
{
status ns;
ns.step = ss.step + ;
ns.h = A(ns);
ns.st = t;
q.push(ns);
vis[t] = true;
}
} for(int h = ; h < *j;++ h)
{
if (h >= j)
id = g[h-j+i+len];
else
id = g[i+h];
int t = ss.st; for(int v = ; v < j ; ++ v)
{
if (g[i+v] != id)
t &= ~( << (i+v));
else
t |= ( << (i+v)); if (g[i+v+len] != id)
t &= ~( << (i+v+len));
else
t |= ( << (i+v+len)); } if (!vis[t])
{
vis[t] = true;
status ns;
ns.h = A(ns);
ns.step = ss.step + ;
ns.st = t;
q.push(ns);
}
} } }
return -;
} int main(int argc, char * argv[])
{
int T,T2=;
memset(g,,sizeof(g));
scanf("%d",&T);
while (T--)
{
while(!q.empty())
q.pop();
scanf("%d",&len);
scanf("%s%s",g,&g[len]);
memset(vis,false,sizeof(vis));
target = caculate[len*]-;
all = len*;
cout << "Case #" << T2++ << ": " << bfs() << endl;
}
return ;
}

UESTC_Can You Help God Wu CDOJ 582的更多相关文章

  1. cdoj 1489 老司机采花

    地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others)     M ...

  2. 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10的100次幂。 输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。 输入样例: 1234567890987654321123456789 输出样例: yi san wu

    这是PAT中的一道练习题 刚开始的时候我想着直接定义正整数n,结果走了很大的弯路,因为题目中要求n小于10的100次幂,即最大的正整数n有100位,而C语言中整型数字最大占8个字节的存储空间,如果按无 ...

  3. Wu反走样算法绘制圆(C++/MFC实现)

    Wu反走样圆 原理:参考Bresenham算法,在主位移过程中计算出离理想圆最近的两个点,赋予不同的亮度值,绘制像素点即可! MFC 中CXXXView类中添加函数: //Wu算法画反走样圆 void ...

  4. Wu反走样算法绘制直线段

    Wu反走样算法 原理:在我看来,Wu反走样算法是在Bresenham算法基础上改进了一番,它给最靠近理想直线/曲线的两个点以不同的亮度值,以达到模糊锯齿的效果.因为人眼看到的是线附近亮度的平均值. M ...

  5. CodeForces - 1017D Round #502 D. The Wu(状压预处理)

    D. The Wu time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  6. CDOJ 1324 卿学姐与公主(分块)

    CDOJ 1324 卿学姐与公主(分块) 传送门: UESTC Online Judgehttp://acm.uestc.edu.cn/#/problem/show/1324 某日,百无聊赖的卿学姐打 ...

  7. CDOJ 1330 柱爷与远古法阵(高斯消元)

    CDOJ 1330 柱爷与远古法阵(高斯消元) 柱爷与远古法阵 Time Limit: 125/125MS (Java/Others)     Memory Limit: 240000/240000K ...

  8. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  9. Codeforces Round 582

    Codeforces Round 582 这次比赛看着是Div.3就打了,没想到还是被虐了,并再次orz各位AK的大神-- A. Chips Moving 签到题.(然而签到题我还调了20min--) ...

随机推荐

  1. ios获取权限

    ios获取权限 by 伍雪颖 -(void)requestRecord{ [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL ...

  2. [转]Laravel 4之请求

    Laravel 4之请求 http://dingjiannan.com/2013/laravel-request/ 获取请求数据 获取当前请求所包括的所有GET和POST数据 Route::get(' ...

  3. [RxJS] Using Observable.create for fine-grained control

    Sometimes, the helper methods that RxJS ships with such as fromEvent, fromPromise etc don't always p ...

  4. JSP九大内置对象和四种属性范围解读

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文首先主要解说了JSP中四种属性范围的概念.用法与实例. 然后在这个基础之上又引入了九 ...

  5. 数据存储: sqlite,coredata plist 归档

    sql 语句  结构化查询语言 通用数据库操作语言1.创建数据库create database 1407EDB2.删除数据库drop database 1407EDB3.备份use master ex ...

  6. OC——UIlabel text的常规应用

    UILabel *downloader = [[UILabel alloc]init]; NSString *downloadCount = [[LibraryArr objectAtIndex:in ...

  7. 高性能PHP日志插件--Seaslog

    日志系统作为记录系统运行的信息,包括 用户输入,安全日志等,日志系统是不能影响用户的使用. 为什么需要记录日志? 既然日志系统增加了整个系统的开销,为什么我还需要它,这是因为日志能帮我们记录运行的很多 ...

  8. JS多选日期

    项目需要一个可以选择多个日期的日期选择框,从网上找到一个单选的选择框源码 (http://blog.5d.cn/user2/samuel/200503/61881.html),修改成可以多选. 使用方 ...

  9. thinkphp3.2之模型(M层)

    最近学习了php框架thinkphp,回头总结了些学习心得知识,希望对大家有帮助 1.模型的概念: 模型(Model)是 ThinkPHP 中一个很重要的概念,粗略的理解模型就是与数据库交互信息进行c ...

  10. EcStore中的App是什么东西?

    Ecstore中的App不是手机上的App,它是一个Web应用包,集合了Web应用的后端PHP程序.Mysql数据表定义,以及前端HTML+CSS+JS展现,通常是实现某个业务功能,如购物车.促销.支 ...