Paint on a Wall

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 830    Accepted Submission(s): 325

Problem Description
Annie
wants to paint her wall to an expected pattern. The wall can be
represented as a 2*n grid and each grid will be painted only one color.
Annie has a brush which could paint a rectangular area of the wall at a
single step. The paint could be overlap and the newly painted color will
replace the older one.
  For a given pattern of the wall, your task
is to help Annie find out the minimum possible number of painting steps.
You can assume that the wall remains unpainted until Annie paint some
colors on it.
 
Input
There are multiple test cases in the input. The first line contains the number of test cases.
  For each test case, the first line contains the only integer n indicating the length of the wall. (1 <= n <= 8)
  Two lines follow, denoting the expected pattern of the wall. The color is represented by a single capital letter.
  See the sample input for further details.
 
Output
For each test case, output only one integer denoting the minimum number of steps.
 
Sample Input
3
3
ABA
CBC
 
3
BAA
CCB
 
3
BBB
BAB
 
Sample Output
Case #1: 3
Case #2: 3
Case #3: 2
 
n <= 8 。。很经典的搜索。。
状压 + bfs , 用二进制表示是否到达目标颜色。
可以发现一个性质就是 没发现一个未达到目标颜色的点可以直接以它作为顶点向左,向右构造新的状态
写得比较烂, 跑了将近 3 s
 
#include <bits/stdc++.h>
using namespace std ;
typedef pair<int,int> pii ;
#define X first
#define Y second
const int N = <<;
int n ;
string s , s2 ;
struct node {
int w , st ;
node(){}
node( int w , int st ):w(w),st(st){}
bool operator < ( const node &a ) const {
return w > a.w ;
}
}; void update( int &st , int i , int j , bool tag ) {
if( ( st & ( <<j ) ) && s[j] != s[i] ) st ^= (<<j) ;
if( !( st & ( <<j ) ) && s[j] == s[i] ) st |= (<<j) ;
if( !tag ) return ;
if( ( st & ( <<((j+n)%(*n)) ) ) && s[(j+n)%(*n)] != s[i] ) st ^= (<<((j+n)%(*n)) );
if( !( st & ( <<(j+n)%(*n)) ) && s[(j+n)%(*n)] == s[i] ) st |= (<<((j+n)%(*n)) ) ;
// cout << j << ' ' << (j+n)%(2*n) << endl ;
} void show( int st ) {
for( int i = ; i < n ; ++i ) if( st&(<<i) ) cout << '' ; else cout << '' ; cout << endl ;
for( int i = n ; i < *n ; ++i ) if( st&(<<i) ) cout << '' ; else cout << '' ; cout << endl ;
} int dis[N] , all ;
void bfs( ) {
priority_queue<node>que;
que.push( node(,) ) ;
memset( dis , 0x3f , sizeof dis );
dis[] = ;
// cout << all << endl ;
while( !que.empty() ) {
int uw = que.top().w , ust = que.top().st ;
que.pop();
if( dis[ust] < uw ) continue ;
// cout << uw << endl ; show( ust ); cout << endl ;
if( ust == all ) return ;
int vw = uw + , vst , vst2 ; for( int i = ; i < n ; ++i ) if( !( ust&(<<i)) ) {
vst = ust , vst2 = ust ;
for( int j = i ; j < n ; ++j ) { // single line
update( vst , i , j , false );
update( vst2 , i , j , true );
if( vw < dis[vst] ) {
// cout << i << ' ' << vst << endl ;
// show( vst ); cout << endl ;
dis[vst] = vw ;
que.push( node( vw , vst ) ) ;
}
if( vw < dis[vst2] ) {
// show( vst2 ); cout << endl ;
dis[vst2] = vw ;
que.push( node( vw , vst2 ));
}
}
vst = ust , vst2 = ust ;
for( int j = i ; j >= ; --j ) {
update( vst , i , j , false );
update( vst2 , i , j , true );
if( vw < dis[vst] ) {
// show( vst ); cout << endl ;
dis[vst] = vw ;
que.push( node( vw , vst ) ) ;
}
if( vw < dis[vst2] ) {
// show( vst2 ); cout << endl ;
dis[vst2] = vw ;
que.push( node( vw , vst2 ));
}
}
} for( int i = n ; i < * n ; ++i ) if( !( ust&(<<i)) ) {
vst = ust , vst2 = ust ;
for( int j = i ; j < * n ; ++j ) { // single line
update( vst , i , j , false );
update( vst2 , i , j , true );
if( vw < dis[vst] ) {
// show( vst ); cout << endl ;
dis[vst] = vw ;
que.push( node( vw , vst ) ) ;
}
if( vw < dis[vst2] ) {
// show( vst2 ); cout << endl ;
dis[vst2] = vw ;
que.push( node( vw , vst2 ));
}
}
vst = ust , vst2 = ust ;
for( int j = i ; j >= n ; --j ) {
update( vst , i , j , false );
update( vst2 , i , j , true );
if( vw < dis[vst] ) {
// show( vst ); cout << endl ;
dis[vst] = vw ;
que.push( node( vw , vst ) ) ;
}
if( vw < dis[vst2] ) {
// show( vst2 ); cout << endl ;
dis[vst2] = vw ;
que.push( node( vw , vst2 ));
}
}
}
}
} int Run() {
int _ , cas = ; cin >> _ ;
while( _-- ) {
cout << "Case #" << cas++ <<": " ;
cin >> n >> s >> s2 ;
s += s2 ;
all = (<<(n*))-;
bfs();
// cout << dis[45] << endl ;
cout << dis[all] << endl ;
}
return ;
}
int main() {
// freopen("in.txt","r",stdin);
ios::sync_with_stdio();
return Run();
}

HDU 4012 Paint on a Wall(状压+bfs)的更多相关文章

  1. 拯救大兵瑞恩 HDU - 4845(状压bfs || 分层最短路)

    1.状压bfs 这个状压体现在key上  我i们用把key状压一下  就能记录到一个点时 已经拥有的key的种类 ban[x1][y1][x2][y1]记录两个点之间的状态 是门 还是墙 还是啥都没有 ...

  2. HDU Stealing Harry Potter's Precious(状压BFS)

    状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...

  3. POJ 1324 Holedox Moving (状压BFS)

    POJ 1324 Holedox Moving (状压BFS) Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18091 Acc ...

  4. P2622 关灯问题II(状压bfs)

    P2622 关灯问题II 题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯——按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j] ...

  5. 状压BFS

    ​题意:1个机器人找几个垃圾,求出最短路径. 状压BFS,这道题不能用普通BFS二维vis标记数组去标记走过的路径,因为这题是可以往回走的,而且你也不能只记录垃圾的数量就可以了,因为它有可能重复走同一 ...

  6. hdu 5094 状压bfs+深坑

    http://acm.hdu.edu.cn/showproblem.php?pid=5094 给出n*m矩阵 给出k个障碍,两坐标之间存在墙或门,门最多10种,状压可搞 给出s个钥匙位置及编号,相应的 ...

  7. HDU 5067 Harry And Dig Machine(状压DP)(TSP问题)

    题目地址:pid=5067">HDU 5067 经典的TSP旅行商问题模型. 状压DP. 先分别预处理出来每两个石子堆的距离.然后将题目转化成10个城市每一个城市至少经过一次的最短时间 ...

  8. HDU 1565 - 方格取数(1) - [状压DP][网络流 - 最大点权独立集和最小点权覆盖集]

    题目链接:https://cn.vjudge.net/problem/HDU-1565 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32 ...

  9. HDU 4026 Unlock the Cell Phone 状压dp(类似TSP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4026 Unlock the Cell Phone Time Limit: 6000/3000 MS ...

随机推荐

  1. 浅谈GC

    关于Java中的GC,简单来说就是垃圾收集器自动回收生命周期结束的对象,释放内存. 那么怎样确定对象是否存活呢? 可达性分析算法 现在主流的Java虚拟机大多使用这种可达性分析算法来判断对象是否需要进 ...

  2. linux 调整内核优化

    所谓内核优化,主要是在 linux 中针对业务服务应用而进行的系统内核参数优化,优化并无特殊的 标准,下面以常见生产环境 linux 的内核优化为例讲解,仅供大家参考: 内核调优 #vi /etc/s ...

  3. Linux性能优化从入门到实战:12 内存篇:Swap 基础

    内存资源紧张时,可能导致的结果 (1)OOM 杀死大内存CPU利用率又低的进程(系统内存耗尽的情况下才生效:OOM 触发的时机是基于虚拟内存,即进程在申请内存时,如果申请的虚拟内存加上服务器实际已用的 ...

  4. 编译安装github上的kafka_exporter项目

    本文介绍的kafka_exporter是prometheus监控系统中针对kafka的一款监控插件,要使用这个监控插件,kafka的版本需要满足 0.10.1.0 及以上. 项目的github地址:h ...

  5. GetExtendedTcpTable

    https://blog.csdn.net/sky101010ws/article/details/55511501 AdjustTokenPrivileges function Library Ad ...

  6. B1001. 害死人不偿命的(3n + 1)猜想

    题目描述 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n + 1)砍掉一半.这样一直反复砍下去,最后一定在某一步得到n=1.卡拉兹在19 ...

  7. OC 类的load方法

    +(void)load 方法 不需要主动调用,类加载时会走这个方法

  8. OC + RAC(一) RACSignal 基本使用

    -(void)_test1{ //测试RAC流程 发送next类型事件 以completed结束时: //至于有无 sendCompleted 的区别主要是用在需要知道信号状态 NSLog(@&quo ...

  9. Windows无法启动MapGIS DataStorage Service服务

    但是启动又启动不了,查看属性 发现计算机服务器确实少了该文件目录.. 可能是不小心删除了? 之前确实有删过一些文件 下次直接把.net禁止就可以了,不用删除,不然不小心删除了其它服务.. 参考文献:h ...

  10. Hash树——数据结构