Painter's Problem
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5378 | Accepted: 2601 |
Description

Input
Output
Sample Input
2
3
yyy
yyy
yyy
5
wwwww
wwwww
wwwww
wwwww
wwwww
Sample Output
0
15
题解:
构造矩阵高斯消元后可以得到一组解,但是题目中要求的是求出最小染色次数。所以要对其中不确定的方案进行枚举。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int T,N,ANS;
int a[][];
bool gauss(){
int now=;
for(int i=;i<=N*N;i++){
int to=now;
while(to<=N*N&&a[to][i]==) to++;
if(to>N*N) continue;
if(to!=now){
for(int j=;j<=N*N+;j++) swap(a[to][j],a[now][j]);
}
for(int j=;j<=N*N;j++){
if(j!=now&&a[j][i]){
for(int k=;k<=N*N+;k++){
a[j][k]^=a[i][k];
}
}
}
now++;
}
for(int i=now;i<=N*N;i++)
if(a[i][N*N+]!=) return false;
return true;
} int v[],cnt;
void dfs(int x){
if(cnt>=ANS) return ;//已经比目前的答案大了,没有必要再搜
if(x==){
ANS=min(cnt,ANS);
return ;
}
if(a[x][x]!=){
int num=a[x][N*N+];//num表示第x块砖染色不染色
for(int i=x+;i<=N*N;i++){
if(a[x][i]!=) num=num^v[i];//已经枚举过的x+1~N*N中某块砖如果可以对x产生影响且已染色,就让num改变一次
}
v[x]=num;
if(num==) cnt++;
dfs(x-);
if(num==) cnt--;
}
else{//枚举按或不按两种情况
v[x]=; dfs(x-);
v[x]=; cnt++; dfs(x-); cnt--;
}
} int main(){
scanf("%d",&T);
while(T--){
memset(a,,sizeof(a));
scanf("%d",&N);
for(int i=;i<=N*N;i++){
a[i][i]=;
if(i%N!=) a[i][i-]=;
if(i%N!=) a[i][i+]=;
if(i>=N+) a[i][i-N]=;
if(i<=N*(N-)) a[i][i+N]=;
}
for(int i=;i<=N;i++){
char s[];
scanf("%s",s+);
for(int j=;j<=N;j++){
if(s[j]=='w') a[(i-)*N+j][N*N+]=;
}
}
if(gauss()==false){
puts("inf");
continue;
}
ANS=<<;
dfs(N*N);
printf("%d\n",ANS);
}
return ;
}
Painter's Problem的更多相关文章
- poj 1681 Painter's Problem
Painter's Problem 题意:给一个n*n(1 <= n <= 15)具有初始颜色(颜色只有yellow&white两种,即01矩阵)的square染色,每次对一个方格 ...
- Painter's Problem poj1681 高斯消元法
Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4420 Accepted: 2143 ...
- POJ 1681 Painter's Problem 【高斯消元 二进制枚举】
任意门:http://poj.org/problem?id=1681 Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total ...
- [POJ1681]Painter's Problem(高斯消元,异或方程组,状压枚举)
题目链接:http://poj.org/problem?id=1681 题意:还是翻格子的题,但是这里有可能出现自由变元,这时候枚举一下就行..(其实这题直接状压枚举就行) /* ━━━━━┒ギリギリ ...
- OpenJudge 2813 画家问题 / Poj 1681 Painter's Problem
1.链接地址: http://bailian.openjudge.cn/practice/2813 http://poj.org/problem?id=1681 2.题目: 总时间限制: 1000ms ...
- Painter's Problem (高斯消元)
There is a square wall which is made of n*n small square bricks. Some bricks are white while some br ...
- POJ 1681 Painter's Problem(高斯消元+枚举自由变元)
http://poj.org/problem?id=1681 题意:有一块只有黄白颜色的n*n的板子,每次刷一块格子时,上下左右都会改变颜色,求最少刷几次可以使得全部变成黄色. 思路: 这道题目也就是 ...
- POJ 1681 Painter's Problem (高斯消元)
题目链接 题意:有一面墙每个格子有黄白两种颜色,刷墙每次刷一格会将上下左右中五个格子变色,求最少的刷方法使得所有的格子都变成yellow. 题解:通过打表我们可以得知4*4的一共有4个自由变元,那么我 ...
- POJ 1681 Painter's Problem (高斯消元 枚举自由变元求最小的步数)
题目链接 题意: 一个n*n 的木板 ,每个格子 都 可以 染成 白色和黄色,( 一旦我们对也个格子染色 ,他的上下左右 都将改变颜色): 给定一个初始状态 , 求将 所有的 格子 染成黄色 最少需要 ...
随机推荐
- Oracle涂抹oracle学习笔记第10章Data Guard说,我就是备份
DG 是备份恢复工具,但是更加严格的意义它是灾难恢复 Data Guard是一个集合,由一个Primary数据库及一个或者多个Standby数据库组成,分两类逻辑Standby和物理Standby 1 ...
- Spoken English Practice(1、This is between you and me, Don't let it out. 2、Don't let your dreams be dreams, no matter how hard it gets, say to yourself, I'm going to make it.)
绿色:连读: 红色:略读: 蓝色:浊化: 橙色:弱读 下划线_为浊化 口语蜕变(2017/7/12) ...
- varints
Protocol Buffer技术详解(数据编码) - Stephen_Liu - 博客园 https://www.cnblogs.com/stephen-liu74/archive/2013/01/ ...
- Yii框架2.0的过滤器
过滤器是 控制器 动作 执行之前或之后执行的对象. 例如访问控制过滤器可在动作执行之前来控制特殊终端用户是否有权限执行动作, 内容压缩过滤器可在动作执行之后发给终端用户之前压缩响应内容. 过滤器可包含 ...
- 6.2 - BBS + BLOG系统
一.简介 博客系统开发: 1.注册,登录,首页 2.个人站点,分组:(分类,标签,归档)3.文章详细页4.点赞,踩灭5.评论楼,评论树6.后台管理,发布文章,文件上传7.BeautifulSoup8. ...
- 前端开发 - CSS - 上
CSS: 1.css的引入方式 2.基础选择器 3.高级选择器 4.选择器的优先级 5.伪类选择器 6.字体样式 7.文本样式 8.背景 9.盒模型border 10.margin 11.paddin ...
- The Highest Mark---hdu5501(问题转化 01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5501 二维数组: #include<stdio.h> #include<iostre ...
- 雨痕 的《Python学习笔记》--附脑图(转)
原文:http://www.pythoner.com/148.html 近日,在某微博上看到有人推荐了 雨痕 的<Python学习笔记>,从github上下载下来看了下,确实很不错. 注意 ...
- MySQL存储引擎与事务
1.作用 和磁盘的数据打交道 2.简介 MySQL 基于存储引擎管理 表空间数据数据文件 3.种类 Innodb存储引擎ibd:存储表的数据行和索引frm:表基本结构信息Myisam存储引擎frmm ...
- Linux(5)- MariaDB、mysql主从复制、初识redis
一.MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL ...