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 ...
随机推荐
- 未能加载文件或程序集“WebApi”或它的某一个依赖项。试图加载格式不正确的程序。
将项目的平台目标改为“Any CPU” 在项目上右击选择属性——>生成——>平台目标 选择Any CPU
- readlink 查看符号链接的文件的内容
1. 命令功能 readlink 查看软链接文件里的真实内容. 2. 语法格式 readlink [option] file 参数 参数说明 -f 后跟软链接文件 3. 使用范例 范例1 查看文件链 ...
- 将postgresql中的数据实时同步到kafka中
参考地址:https://blog.csdn.net/weixin_33985507/article/details/92460419 参考地址:https://mp.weixin.qq.com/s/ ...
- JS基础入门篇(七)—运算符
1.算术运算符 1.算术运算符 算术运算符:+ ,- ,* ,/ ,%(取余) ,++ ,-- . 重点:++和--前置和后置的区别. 1.1 前置 ++ 和 后置 ++ 前置++:先自增值,再使用值 ...
- django之mysql数据库的配置和orm交互
一:django默认数据库的配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path. ...
- windows2008R2-AD域控组策略设置与其它相关设置
防火墙设置 修改>计算机配置>策略>安全设置>高级安全windows防火墙>高级安全windows防火墙 修改入站规则 1.组名-文件和打印机共享(SMB-In)> ...
- Spring Boot Shiro
Shiro 核心 API Subject:用户主体(每次请求都会创建Subject). principal:代表身份.可以是用户名.邮件.手机号码等等,用来标识一个登录主体的身份. credentia ...
- python生成HTMl报告(unittest)
Python3 使用HTMLTestRunner.py 报错ImportError: No module named 'StringIO'处理方法 HTMLTestRunner.py文件是基于Py ...
- Python全栈开发,Day3
一.基本数据类型 基础数据类型,有7种类型,存在即合理. 1.int 整数 主要是做运算的.比如加减乘除,幂,取余 + - * / ** % ... 2.bool 布尔值 判断真假以及作为条件变量 ...
- BZOJ 2726: [SDOI2012]任务安排 斜率优化 + 凸壳二分 + 卡精
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...