UESTC_Can You Help God Wu CDOJ 582
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 |
Case #1: 3 |
Source
#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的更多相关文章
- cdoj 1489 老司机采花
地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others) M ...
- 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10的100次幂。 输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。 输入样例: 1234567890987654321123456789 输出样例: yi san wu
这是PAT中的一道练习题 刚开始的时候我想着直接定义正整数n,结果走了很大的弯路,因为题目中要求n小于10的100次幂,即最大的正整数n有100位,而C语言中整型数字最大占8个字节的存储空间,如果按无 ...
- Wu反走样算法绘制圆(C++/MFC实现)
Wu反走样圆 原理:参考Bresenham算法,在主位移过程中计算出离理想圆最近的两个点,赋予不同的亮度值,绘制像素点即可! MFC 中CXXXView类中添加函数: //Wu算法画反走样圆 void ...
- Wu反走样算法绘制直线段
Wu反走样算法 原理:在我看来,Wu反走样算法是在Bresenham算法基础上改进了一番,它给最靠近理想直线/曲线的两个点以不同的亮度值,以达到模糊锯齿的效果.因为人眼看到的是线附近亮度的平均值. M ...
- 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 ...
- CDOJ 1324 卿学姐与公主(分块)
CDOJ 1324 卿学姐与公主(分块) 传送门: UESTC Online Judgehttp://acm.uestc.edu.cn/#/problem/show/1324 某日,百无聊赖的卿学姐打 ...
- CDOJ 1330 柱爷与远古法阵(高斯消元)
CDOJ 1330 柱爷与远古法阵(高斯消元) 柱爷与远古法阵 Time Limit: 125/125MS (Java/Others) Memory Limit: 240000/240000K ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- Codeforces Round 582
Codeforces Round 582 这次比赛看着是Div.3就打了,没想到还是被虐了,并再次orz各位AK的大神-- A. Chips Moving 签到题.(然而签到题我还调了20min--) ...
随机推荐
- cf446A DZY Loves Sequences
A. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- jQuery 动画之 添加商品到购物车
前台页面 <link href="MyCar.css" rel="stylesheet" /> <script src="../jq ...
- IOS UIImage 模糊
#import <UIKit/UIKit.h> #import <Accelerate/Accelerate.h> #import <QuartzCore/QuartzC ...
- mycat实例(1)
2016二月 22 置原 MyCat - 使用篇(1) 分类:数据库分库分表(Mycat等) (1126) (1) 数据库路由中间件MyCat - 使用篇(1) 基本概念 直接介绍概念太枯燥了,还是拿 ...
- VBA清除Excelpassword保护,2003/2007/2010均适用
Sub Macro1() ' ' Breaks worksheet and workbook structure passwords. Jason S ' probably originator of ...
- [置顶] 正则表达式应用:匹配IP地址
都知道iP地址有四个数值,三个点号组成.三个数值的具体范围为0到255,为了使用正则表达式匹配就必须分析IP地址的组成 1先分析数值,2再组合数值和点号 1先分析数值 IP地址的数字范围从0到255, ...
- SQL内连接-外连接join,left join,right join,full join
1.创建测试表test1及test2 SQL)); 表已创建. SQL)); 表已创建. ,'name1'); ,'name2'); ,'name3'); ,'name4'); ,'name5'); ...
- C#中string.Empty和""、null的区别
string.Empty是string类的一个静态常量,而""则表示一个空字符串. string是一种特殊的引用类型,它的null值则表示没有分配内存. 使用ILSpy反编译Str ...
- Mob短信验证的配置的解释
原文地址:http://www.jb51.net/article/84946.htm 关于mob短信验证的解释: mob官方是这样写的: repositories{ flatDir{ dirs 'li ...
- C语言实现界面(不通过MFC\避免遗忘)
感觉MFC不属于程序员细究的东西,今实现基本界面避免日后遗忘. 源代码: #include<windows.h>#include<stdio.h>char str[] = {' ...