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. Mac OS X下Sublime Text (V2.0.1)破解

    转自:http://blog.sina.com.cn/s/blog_559d66460101cab0.html 1. 在http://www.sublimetext.com/上根据操作系统选择对应版本 ...

  2. C# winForm 窗体闪烁问题

    在构造函数里加上以下代码: this.DoubleBuffered = true;//设置本窗体            SetStyle(ControlStyles.UserPaint, true); ...

  3. saiku执行速度优化二

    上一篇文章介绍了添加filter可以加快查询速度.下面继续分析: 下面这个MDX语句: WITH SET [~FILTER] AS {[create_date].[create_date].[--]} ...

  4. bootstrap fileinput 文件上传工具

    这是我上传的第二个plugin 首先第一点就是因为这个好看 符合bootstrap的界面风格 第二是可以拖拽(虽然我不常用这个功能 但是这样界面看起来就丰满了很多) 最后不得不吐槽这个的回发事件 我百 ...

  5. Why is processing a sorted array faster than an unsorted array?

    这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...

  6. BZOJ 1093 [ZJOI2007] 最大半连通子图(强联通缩点+DP)

    题目大意 题目是图片形式的,就简要说下题意算了 一个有向图 G=(V, E) 称为半连通的(Semi-Connected),如果满足图中任意两点 u v,存在一条从 u 到 v 的路径或者从 v 到 ...

  7. osgi: HttpService A null service reference is not allowed.

    最近在学习osgi,在练习HttpService的过程中,一直出现“A null service reference is not allowed”这样的报错,代码本身没有问题,在网上也搜了不少地方, ...

  8. ruby 中文字符to_json后乱码(unicode)

    今天遇到一个中文to_json问题 text = "第1章 青豆 不要被外表骗了" text.to_json => "\"\\u7b2c1\\u7ae0 ...

  9. 做mapx、ArcEngine的二次开发出现“没有注册类别 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)”

    转自:http://blog.sina.com.cn/s/blog_638e61a40100ynnc.html 出现这个问题主要是因为32位操作系统和64位操作系统存在兼容性问题. 解决方案: 1.鼠 ...

  10. Java知多少(下)

    Java知多少(78)Java向量(Vector)及其应用 Java知多少(79)哈希表及其应用 Java知多少(80)图形界面设计基础 Java知多少(81)框架窗口基础 Java知多少(82)标签 ...