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 ...
随机推荐
- a标签实现下载canvas图片
令 a 的 href = canvas.toDataURL("image/png");
- Altium Designer 19 导出光绘文件
一.点击 文件--制造输出--Gerber Files 第一次设置如下 绘制层点击进去全选 钻孔光圈 符号大小50mil 生成文件 关闭不用保存 蚀刻图 二.点击 文件--制造输出--Gerber F ...
- es6 扩展运算符 三个点...
es6中引入扩展运算符…,它用于把一个数组转化为用逗号分隔的参数序列,它常用在不定参数个数时的函数调用,数组合并等情形.因为typeScript是es6的超集,所以typeScript也支持扩展运算符 ...
- SQL Server 中用While循环替代游标Cursor的解决方案
在编写SQL批处理或存储过程代码的过程中,经常会碰到有些业务逻辑的处理,需要对满足条件的数据记录逐行进行处理,这个时候,大家首先想到的方案大部分是用“游标”进行处理. 举个例子,在订单管理系统中,客服 ...
- php中substr_compare()区分大小写吗
PHP substr_compare() 函数 定义和用法 substr_compare() 函数从指定的开始位置比较两个字符串. 提示:该函数是二进制安全且选择性地对大小写敏感(区分大小写). 语法 ...
- Python_006(is和==,小数据池)
一.is和==的区别 1.id()方法,可以查看一个变量的内存地址 lst = ["周杰伦", "燃哥"] lst1 = ["周杰伦", & ...
- .Net服务组件(ServicedComponent)简介及其使用
.NET Enterprise Services 为企业应用程序提供重要的基础结构.COM+ 为企业环境中部署的组件编程模型提供服务结构.System.EnterpriseServices命名空间向 ...
- 添加对象到 HashSet 里的规则是
下面的解释取自百度知道的一位网友的回答,链接如下: java HashSet类添加元素的问题_百度知道http://zhidao.baidu.com/link?url=9bcAnolev1EBeFI_ ...
- JAVA一切皆对象之包装类、自动装箱、自动拆箱
首先封装一个基本数据类型int, class P{ private int number; //封装一个私有的->基本数据类型 public P(){} public P(int number) ...
- jsc2019_qualD Classified
题目大意 给你一个有n个点的完全图 求一种方案是的给边染色后任何一点不能沿一种颜色的边走奇数条边回到这个点 要求颜色数最少 分析 考场上输出格式打错见祖宗... 我们每次找一个最大二分图将其染一个新颜 ...