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

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

 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = ; int equ,var;
int a[maxn][maxn];
int x[maxn]; // 解集.
int pos[maxn];
///int free_num; void init_a()///对称阵;
{
memset(a,,sizeof(a));
for(int i=;i<var*var;i++)
{
a[i][i]=;
int t=i%var;
if(t>) a[i][i-]=;
if(t<var-) a[i][i+]=;
t=i/var;
if(t>=) a[i][i-var]=;
if(t<var-) a[i][i+var]=;
}
} int Gauss()
{
int i, j, k;
int max_r; // 当前这列绝对值最大的行.
int t=;///记录自由元的个数;
int col = ; /// 当前处理的列. for (k = ; k < equ*equ && col < var*var; k++, col++)
{
max_r = k;
for (i = k + ; i < equ*equ; i++)
{
if (a[i][col] > a[max_r][col])
{
max_r=i;
break;
}
}
if (max_r != k)
{
for (j = k; j < var*var + ; j++) swap(a[k][j], a[max_r][j]);
}
if (a[k][col] == )
{
/// 说明该col列第k行以下全是0了,则处理当前行的下一列.
///并且应当记录这个自由元;
k--;
pos[t++]=col;
continue;
}
for (i = k + ; i < equ*equ; i++)
{
if (a[i][col] != )
{
for (j = col; j < var*var + ; j++)
{
a[i][j]^=a[k][j];
}
}
}
}
for (i = k; i < equ*equ; i++)
{
// 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.
if (a[i][col] != ) return -;
}
return var*var - k;
} int solve(int s)
{
int ans=;
int state=(<<s);
for(int i=;i<state;i++)
{
int cnt=;
memset(x,,sizeof(x));
for(int j=;j<s;j++)
{
if(i&(<<j)) x[pos[j]]=,cnt++;
}
for(int j=var*var-s-;j>=;j--)
{
int f=;
int ss;
int tmp=a[j][var*var];
for(int k=j;k<equ*equ;k++)
{
if(a[j][k]&&f)
{
ss=k;
f=;
}
if(a[j][k]) tmp^=x[k];
}
x[ss]=tmp;
cnt+=x[ss];
}
ans=min(ans,cnt);
}
return ans;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&equ);
var=equ;
init_a();
for(int i=;i<var*var;i++)
{
char x;
cin>>x;
if(x=='y')
a[i][var*var]=;
else a[i][var*var]=;
}
int v=Gauss();
if(v==-) printf("inf\n");
else cout<<solve(v)<<endl;
}
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 1681 Painter's Problem 【高斯消元 二进制枚举】

    任意门:http://poj.org/problem?id=1681 Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total ...

  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 1222【异或高斯消元|二进制状态枚举】

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

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

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

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

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

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

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

随机推荐

  1. sql server命令行

    http://www.cnblogs.com/bingcaihuang/archive/2011/01/31/1948222.html http://www.cnblogs.com/wontonJ/a ...

  2. Ubuntu 16.04 LTS更新

    Canonical今天正式发布了新版的Ubuntu系统,针对PC.笔记本.上网本.平板和智能手机各类设备.这次的Ubuntu 16.04代号为Xenial Xerus——这个代号是由Canonical ...

  3. bit操作 转

    http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/ Bit Hack #6. Turn off the ...

  4. PHP cURL应用实现模拟登录与采集使用方法详解

    对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程链接的数据,但是它的可控制性太差了,对于各种复杂情况的采集情景,file_get_co ...

  5. bower 新建.bowerrc文件

    Twitter工程师团队推出了Bower,这是一个针对Web开发的包管理器.该工具主要用来帮助用户轻松安装CSS.JavaScript.图像等相关包,并管理这些包之间的依赖. 随着网页功能变得越来越复 ...

  6. ops中set_sysclk set_clkdiv set_pll详解

    在看Alsa soc驱动的是时候,在snd_soc_dai_driver.ops中有3个字段 .set_sysclk .set_pll .set_clkdiv 开始的时候,总是晕头转向,感觉这3个回调 ...

  7. Android界面布局基本属性

    在 android 中我们常用的布局方式有这么几种:1.LinearLayout ( 线性布局 ) :(里面只可以有一个控件,并且不能设计这个控件的位置,控件会放到左上角)              ...

  8. JavaScript - 2个等号与3个等号的区别

    简言之,== equality 等同,=== identity 恒等. ==, 两边值类型不同的时候,要先进行类型转换,再比较. ===,不做类型转换,类型不同的一定不等. 或: = 赋值运算符 == ...

  9. BZOJ 1001 [BeiJing2006] 狼抓兔子(平面图最大流)

    题目大意 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的.而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: ...

  10. 【开源EFW框架】框架中自定义控件GridBoxCard使用实例说明

    回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://p ...