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 ;
}

最新文章

  1. Java中一些常用的方法
  2. SegmentFault创始人高阳:辍学后带着500元北漂,4年建成国内最大开发者
  3. 0729pm命名空间
  4. 介绍UDF,以及完成大小写的转换
  5. C# 启动关闭.exe进程(转)
  6. yii框架基本操作
  7. PHP自学之路---报表及绘图技术
  8. html5 notifications通知
  9. c++ primer plus 习题答案(1)
  10. 配置php网页显示错误
  11. 关于 vue.js 运行环境的搭建(mac)
  12. java 中的重载与重写 抽象类与接口的区别
  13. 图像处理:卷积模块FPGA 硬件加速
  14. 洛谷P1330封锁阳光大学题解
  15. C# 字符串大写转小写,小写转大写,数字保留,其他除外
  16. CSS之设置滚动条样式
  17. canvas放射粒子效果
  18. Promise &amp; Deferred Objects in JavaScript Pt.2: in Practice
  19. 解题:BZOJ 2818 GCD
  20. python(24)下载文件

热门文章

  1. 40个UI设计工具
  2. ECharts 使用实例
  3. soj 1698 Hungry Cow_三角函数
  4. c++ windows下declspec
  5. Android --- 字符串\n的换行问题
  6. Socket通信原理和实践
  7. docker 数据管理
  8. jquery文本框验证字符长度和只能输入数字
  9. Sql server 数据库 单用户切换为多用户
  10. Install cv2.so for Anaconda