HDU 4012 Paint on a Wall(状压+bfs)
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
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.
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.
#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)的更多相关文章
- 拯救大兵瑞恩 HDU - 4845(状压bfs || 分层最短路)
1.状压bfs 这个状压体现在key上 我i们用把key状压一下 就能记录到一个点时 已经拥有的key的种类 ban[x1][y1][x2][y1]记录两个点之间的状态 是门 还是墙 还是啥都没有 ...
- HDU Stealing Harry Potter's Precious(状压BFS)
状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...
- POJ 1324 Holedox Moving (状压BFS)
POJ 1324 Holedox Moving (状压BFS) Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18091 Acc ...
- P2622 关灯问题II(状压bfs)
P2622 关灯问题II 题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯——按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j] ...
- 状压BFS
题意:1个机器人找几个垃圾,求出最短路径. 状压BFS,这道题不能用普通BFS二维vis标记数组去标记走过的路径,因为这题是可以往回走的,而且你也不能只记录垃圾的数量就可以了,因为它有可能重复走同一 ...
- hdu 5094 状压bfs+深坑
http://acm.hdu.edu.cn/showproblem.php?pid=5094 给出n*m矩阵 给出k个障碍,两坐标之间存在墙或门,门最多10种,状压可搞 给出s个钥匙位置及编号,相应的 ...
- HDU 5067 Harry And Dig Machine(状压DP)(TSP问题)
题目地址:pid=5067">HDU 5067 经典的TSP旅行商问题模型. 状压DP. 先分别预处理出来每两个石子堆的距离.然后将题目转化成10个城市每一个城市至少经过一次的最短时间 ...
- HDU 1565 - 方格取数(1) - [状压DP][网络流 - 最大点权独立集和最小点权覆盖集]
题目链接:https://cn.vjudge.net/problem/HDU-1565 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32 ...
- 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 ...
随机推荐
- [转]Oracle 11g 基于CentOS7静默安装教程(无图形界面,远程安装) --有部份地方有问题
Oracle 11g 基于CentOS7静默安装教程(无图形界面,远程安装) [转载]原文地址:http://canonind.blog.51cto.com/8239025/1883066 一.安装前 ...
- Simple Vedio Intercom System
I. Deployment / Architecture Block Diagram II. Resources Used sip proxy server + sip user agent 1. ...
- Linux性能优化从入门到实战:03 CPU篇:CPU上下文切换
linux操作系统是将CPU轮流分配给任务,分时执行的.而每次执行任务时,CPU需要知道CPU寄存器(CPU内置的内存)和程序计数器PC(CPU正在执行指令和下一条指令的位置)值,这些值是CPU执 ...
- [Poj2349]Arctic Network(二分,最小生成树)
[Poj2349]Arctic Network Description 国防部(DND)要用无线网络连接北部几个哨所.两种不同的通信技术被用于建立网络:每一个哨所有一个无线电收发器,一些哨所将有一个卫 ...
- cx_Oracle python模块安装
1. 需要从oracle网站下载一下两个包 instantclient-basic-linux.x64-11.2.0.4.0.zip instantclient-sdk-linux.x64-11.2. ...
- bzoj1969 [Ahoi2005]LANE 航线规划 树链剖分
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1969 题解 如果我们把整个图边双联通地缩点,那么最终会形成一棵树的样子. 那么在这棵树上,\( ...
- flask之注册功能
一:注册功能 1:前端准备表单 # 前端代码 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- python开发之virtualenv与virtualenvwrapper
在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...
- 一个能极大提高生产率的Chrome新建标签页扩展
我是一个对开发生产率有着BT需求的程序员,总是追求将自己的单位时间生产率最大化. 通过分析,我发现自己一天会反反复复使用Chrome的新建标签,然后访问常用的网站.因此,我期望新建一个默认的Chrom ...
- php接受post传值的方法
这段时间在研究php的接口,利用jmeter模拟发送数据给php服务器,看php如何接收post传输过去的数据,遇到了几个问题,经过一番度娘之后终于有所理解,记录一下: 这里记录常用的两种post方式 ...