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. distri.lua的web运维工具

    我的新手游项目很快就要进入到寻找发行商的环节,最近几天相对较空闲,逐将工作重心转移到服务器组运维工具的制作上. 回想一年之前经历的那个不算成功的端游项目,因为运维工具设计得不合理,使用十分不方便,游戏 ...

  2. CISA 信息系统审计知识点 [第一章. 信息系统审计过程 ]

    对有志成为审计师或者IT管理者de朋友, 第一章. 信息系统审计过程 1. IS 审计和保障标准.指南.工具.职业道德规范 信息技术保证框架(ITAF,Information Technology A ...

  3. boost 1.56.0 编译及使用

    boost的编译和使用,经过搜集资料和总结,记录成文.感谢文后所列参考资料的作者. 1 下载 地址:http://sourceforge.net/projects/boost/files/boost/ ...

  4. Unitils集成DBUnit、Spring-单元测试

    Unitils集成DBUnit.Spring-单元测试 1.maven-pom文件中引入相关jar包 <!-- Unitils -dbunit.Spring --> <depende ...

  5. Centos用yum升级mysql到(5.5.37)

    原文:http://www.if-not-true-then-false.com/2010/install-mysql-on-fedora-centos-red-hat-rhel/ 1. Change ...

  6. 在 Ubuntu 配置 PPTP Server

    本文在 Ubuntu 12.4 或 14 亲测有效. 建立 PPTP 服务器 首先安装 pptp 服务器. # apt-get install pptpd 然后配置 pptpd. # sudo vi ...

  7. Windows上使用clang编译

    - 先自己从源代码 (http://llvm.org/releases/) 编译llvm和clang,或者直接安装clang for Windows - 测试过使用cygwin (https://cy ...

  8. java 时间转换

    public static int timestrtosec(String time) {        if (Strings.isNullOrEmpty(time)) {            r ...

  9. 2014 网选 上海赛区 hdu 5047 Sawtooth

    题意:求n个'M'型的折线将一个平面分成的最多的面数! 思路:我们都知道n条直线将一个平面分成的最多平面数是 An = An-1 + n+1 也就是f(n) = (n*n + n +2)/2 对于一个 ...

  10. bower的使用

    一.bower的安装 安装nodejs的最新版本: 安装npm. 由于npm是nodejs的包管理器,所以在将nodejs安装完成后,npm也就自动安装完成. 安装git. 安装bower. 使用 n ...