任意门:http://poj.org/problem?id=1681

Painter's Problem
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7667   Accepted: 3624

Description

There is a square wall which is made of n*n small square bricks. Some bricks are white while some bricks are yellow. Bob is a painter and he wants to paint all the bricks yellow. But there is something wrong with Bob's brush. Once he uses this brush to paint brick (i, j), the bricks at (i, j), (i-1, j), (i+1, j), (i, j-1) and (i, j+1) all change their color. Your task is to find the minimum number of bricks Bob should paint in order to make all the bricks yellow. 

Input

The first line contains a single integer t (1 <= t <= 20) that indicates the number of test cases. Then follow the t cases. Each test case begins with a line contains an integer n (1 <= n <= 15), representing the size of wall. The next n lines represent the original wall. Each line contains n characters. The j-th character of the i-th line figures out the color of brick at position (i, j). We use a 'w' to express a white brick while a 'y' to express a yellow brick.

Output

For each case, output a line contains the minimum number of bricks Bob should paint. If Bob can't paint all the bricks yellow, print 'inf'.

Sample Input

2
3
yyy
yyy
yyy
5
wwwww
wwwww
wwwww
wwwww
wwwww

Sample Output

0
15

Source

题意概括:

一个二维矩阵, 输入每个格子的初始颜色,可以进行的操作是 粉刷一个格子则相邻的上下左右四个各自颜色都会取反(只有两种颜色);

问最后把全部各自涂成黄色的最小操作数;

解题思路:

根据题意,每个各自都是一个变元,根据各自之间的相邻关系构造增广矩阵;

高斯消元求出自由元个数 sum;

二进制枚举方案,找出最小的操作数;

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = ; int equ, var;
int a[MAXN][MAXN];
char str[MAXN][MAXN];
int x[MAXN];
int free_x[MAXN];
int free_num;
int N; int Gauss()
{
int maxRow, col, k;
free_num = ;
for(k = , col = ; k < equ && col < var; k++, col++){
maxRow = k;
for(int i = k+; i <equ; i++){
if(abs(a[i][col]) > abs(a[maxRow][col]))
maxRow = i;
}
if(a[maxRow][col] == ){ //最大的都为0说明该列下面全是 0
k--;
free_x[free_num++] = col; //说明col是自由元
continue;
}
if(maxRow != k){ //交换行
for(int j = col; j < var+; j++)
swap(a[k][j], a[maxRow][j]);
}
for(int i = k+; i < equ; i++){ //消元
if(a[i][col] != ){
for(int j = col; j < var+; j++){
a[i][j] ^= a[k][j];
}
}
}
}
for(int i = k; i< equ; i++){
if(a[i][col] != ) return -; //无解
}
if(k < var) return var-k; //返回自由元个数 for(int i = var-; i >= ; i--){ //唯一解,回代
x[i] = a[i][var];
for(int j = i+; j < var; j++){
x[i] ^= (a[i][j] && x[j]);
}
}
return ;
} void init()
{
memset(a, , sizeof(a));
memset(x, , sizeof(x));
equ = N*N;
var = N*N;
for(int i = ; i < N; i++){ //构造增广矩阵
for(int j = -; j < N; j++){
int t = i*N+j;
a[t][t] = ;
if(i > ) a[(i-)*N+j][t] = ;
if(i < N-) a[(i+)*N+j][t] = ;
if(j > ) a[i*N+j-][t] = ;
if(j < N-) a[i*N+j+][t] = ;
}
}
} void solve()
{
int t = Gauss();
if(t == -){ //无解
printf("inf\n");
return;
}
else if(t == ){ //唯一解
int ans = ;
for(int i = ; i < N*N; i++){
ans += x[i];
}
printf("%d\n", ans);
return;
}
else{ //多解,需要枚举自由元
int ans = INF;
int tot = <<t;
int cnt = ;
for(int i = ; i < tot; i++){
cnt = ;
for(int j = ; j < t; j++){
if(i&(<<j)){
x[free_x[j]] = ;
cnt++;
}
else x[free_x[j]] = ;
} for(int j = var-t-; j >= ; j--){
int index;
for(index = j; index < var; index++){
if(a[j][index]) break;
}
x[index] = a[j][var]; for(int s = index+; s < var; s++)
if(a[j][s]) x[index] ^= x[s]; cnt+=x[index];
}
ans = min(ans, cnt);
}
printf("%d\n", ans);
}
} int main()
{
int T_case;
int tpx, tpy;
scanf("%d", &T_case);
while(T_case--){
scanf("%d", &N);
init();
for(int i = ; i < N; i++){
scanf("%s", str[i]);
for(int j = ; j < N; j++){
tpx = i*N+j, tpy = N*N;
if(str[i][j] == 'y') a[tpx][tpy] = ;
else a[tpx][tpy] = ;
}
}
solve();
}
return ;
}

POJ 1681 Painter's Problem 【高斯消元 二进制枚举】的更多相关文章

  1. POJ 1681 Painter's Problem (高斯消元)

    题目链接 题意:有一面墙每个格子有黄白两种颜色,刷墙每次刷一格会将上下左右中五个格子变色,求最少的刷方法使得所有的格子都变成yellow. 题解:通过打表我们可以得知4*4的一共有4个自由变元,那么我 ...

  2. POJ 1681 Painter's Problem [高斯消元XOR]

    同上题 需要判断无解 需要求最小按几次,正确做法是枚举自由元的所有取值来遍历变量的所有取值取合法的最小值,然而听说数据太弱自由元全0就可以就水过去吧.... #include <iostream ...

  3. POJ 1222【异或高斯消元|二进制状态枚举】

    题目链接:[http://poj.org/problem?id=1222] 题意:Light Out,给出一个5 * 6的0,1矩阵,0表示灯熄灭,反之为灯亮.输出一种方案,使得所有的等都被熄灭. 题 ...

  4. POJ 1681 Painter's Problem(高斯消元+枚举自由变元)

    http://poj.org/problem?id=1681 题意:有一块只有黄白颜色的n*n的板子,每次刷一块格子时,上下左右都会改变颜色,求最少刷几次可以使得全部变成黄色. 思路: 这道题目也就是 ...

  5. poj 1681 Painter&#39;s Problem(高斯消元)

    id=1681">http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出自由变元.然后枚举自由变元,求出最优值. ...

  6. poj 1681 Painter's Problem

    Painter's Problem 题意:给一个n*n(1 <= n <= 15)具有初始颜色(颜色只有yellow&white两种,即01矩阵)的square染色,每次对一个方格 ...

  7. POJ 2947 Widget Factory(高斯消元)

    Description The widget factory produces several different kinds of widgets. Each widget is carefully ...

  8. POJ 1830 开关问题(高斯消元)题解

    思路:乍一看好像和线性代数没什么关系.我们用一个数组B表示第i个位置的灯变了没有,然后假设我用u[i] = 1表示动开关i,mp[i][j] = 1表示动了i之后j也会跟着动,那么第i个开关的最终状态 ...

  9. POJ 1830 开关问题(高斯消元求解的情况)

    开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8714   Accepted: 3424 Description ...

随机推荐

  1. Farey Sequence(欧拉函数板子题)

    题目链接:http://poj.org/problem?id=2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. Spring混合配置时,遇到配置文件路径NotFound,使用PathMatchingResourcePatternResolver解决

    在将spring的xml配置改为java配置的过程中,遇到了一些问题,block时间比较长的一个问题是资源(.xml, .properties)的路径找不到,最后是使用PathMatchingReso ...

  3. word 快捷键

    Ctrl+shift+F9  清除word文档中的超链接

  4. Coursera 机器学习 第5章 Neural Networks: Learning 学习笔记

    5.1节 Cost Function神经网络的代价函数. 上图回顾神经网络中的一些概念: L  神经网络的总层数. sl  第l层的单元数量(不包括偏差单元). 2类分类问题:二元分类和多元分类. 上 ...

  5. 移动端或APP禁止放大标识

    如果手机端或者APP的应用里面,有点击一下屏幕会自己放大,解决办法如下: 在头部添加一条meta标识 <meta name="viewport" content=" ...

  6. 无监督学习(Unsupervised Learning)

    无监督学习(Unsupervised Learning) 聚类无监督学习 特点 只给出了样本, 但是没有提供标签 通过无监督学习算法给出的样本分成几个族(cluster), 分出来的类别不是我们自己规 ...

  7. (六-1)Firefox插件安装

    1.安装火狐插件 ①旧版本firefox 火狐浏览器右上角-->附件-->获取更多附件-->搜索-->Firebug 安装 Firebug 扩展:https://addons. ...

  8. 【PPTP】windows & linux 安装pptp

    1.Windows下安装 PPTP 打开网络和共享中心 更改适配器配置 按一下alt,之后,点击左上角的文件---->新建传入连接 在新建传入连接这边点击添加用户 如输入用户名:Evelyn  ...

  9. SQLite 大小写敏感

    --转自mojianpo  http://mojianpo.iteye.com/blog/1496579 大部分数据库在进行字符串比较的时候,对大小写是不敏感的. 但是,在SQLite中,对大小写是敏 ...

  10. 浅谈------location

    今天在上班的时候碰到了要根据不同的页面随机添加栏目的问题,很简单的问题,想到了判断页面url是否含有某字符串来进行随机添加栏目...这就需要了解location对象. location 属性名 属性说 ...