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. 架设laravel

    用laravel 架设的用户单点登录授权系统,git clone项目文件后,需要下面的方法初始化,纪录以供项目成员参考 错误信息:`Warning: require(/http/www.mywakav ...

  2. CSS技巧(二):CSS hack

    什么是CSS hack CSS hack由于不同的浏览器,比如IE6,IE7,Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到我们所需要的页面效果. 这个时候我们就需 ...

  3. LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium

    题目: Given a . For . 解题思路: 这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3.有这么几个方法: 方法一: 依赖字符串本身的一些特有函 ...

  4. js常见怪异

    1.隐式转换为布尔:"truthy"和"falsy" 当 JavaScript 需要一个布尔值时(例如:if 语句),任何值都可以被使用. 最终这些值将被转换为 ...

  5. js类(继承)(二)

    1. 定义js类 js并不是一种面向对向的语言, 没有提供对类的支持, 因此我们不能像在传统的语言里那样 用class来定义类, 但我们可以利用js的闭包封装机制来实现js类, 我们来封装一个简的Sh ...

  6. spring 启动流程

    AbstractApplicationContext 分析 启动流程 // Prepare this context for refreshing.prepareRefresh(); 1. // In ...

  7. 【Android】Handler、Looper源码分析

    一.前言 源码分析使用的版本是 4.4.2_r1. Handler和Looper的入门知识以及讲解可以参考我的另外一篇博客:Android Handler机制 简单而言:Handler和Looper是 ...

  8. Legolas工业自动化平台入门(一)搭建应用

    前两篇给大家介绍了TWaver家族的新面孔--Legolas工业自动化平台,通过两个应用案例钻井平台工程用车和水源地监控系统,相信大家对Legolas已经有了一定程度的了解.这几篇文章,我们会逐步介绍 ...

  9. ExtJs Column 显示文字内容过长 使用Tootip显示全部内容

    { text: 'Column Header Blah', dataIndex: 'blah', renderer: function(value, metaData, record, rowIdx, ...

  10. android 应用层性能优化方案

    1.避免创建不必要的类对象 2.如果方法用不到成员变量,可以把方法声明为static,新能会提升15%到20% 3.避免使用getter和setter存取Filed,可以吧Field声明为public ...