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
// POJ 1681 为例题:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
//有equ个方程, var个变元。增广矩阵列数为var+1:0到var;
int equ, var;
int a[maxn][maxn]; // 增广矩阵
int x[maxn]; //解集
int free_x[maxn]; // 自由元
int free_num; //自由元个数 //返回-1无解, 为0 唯一解, 否则返回自由变元个数;
int Gauss()
{
int max_r, col, k;
free_num = ;
for(k = , col = ; k < equ&&col < var; k++, col++)
{
max_r = k;
for(int i = k+; i < equ; i++)
{
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
}
if(a[max_r][col] == )
{
k--;
free_x[free_num++] = col; // 因为只有0,1;当最大为0,则为自由元
continue;
}
if(max_r != k) // 交换
{
for(int j = col; j < var+; j++)
{
swap(a[k][j], a[max_r][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 ;
} int n;
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 = 0x3f3f3f3f;
int tot = ( << t);
for(int i =; i < tot; i++)
{
int 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 idx;
for(idx = j; idx < var; idx++)
if(a[j][idx])
break;
x[idx] = a[j][var];
for(int l = idx+; l < var; l++)
if(a[j][l])
x[idx] ^= x[l];
cnt += x[idx];
}
ans = min(ans , cnt);
}
printf("%d\n", ans);
}
} char str[][];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d", &n);
init();
for(int i = ; i < n; i++)
{
scanf("%s", str[i]);
for(int j = ; j < n; j++)
{
if(str[i][j] == 'y')
a[i*n+j][n*n] = ;
else a[i*n+j][n*n] = ;
}
}
solve();
}
return ;
}

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&#39;s Problem(高斯消元)

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

  4. POJ 1681 Painter's Problem 【高斯消元 二进制枚举】

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

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

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

  6. POJ - 1681: Painter's Problem (开关问题-高斯消元)

    pro:开关问题,同上一题. 不过只要求输出最小的操作步数,无法完成输出“inf” sol:高斯消元的解对应的一组合法的最小操作步数. #include<bits/stdc++.h> #d ...

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

    POJ   1681---Painter's Problem(高斯消元) Description There is a square wall which is made of n*n small s ...

  8. Problem A: Apple(高斯消元)

    可以发现具有非常多的方程, 然后高斯消元就能85分 然而我们发现这些方程组成了一些环, 我们仅仅设出一部分变量即可获得N个方程, 就可以A了 trick 合并方程 #include <cstdi ...

  9. HDU 4818 RP problem (高斯消元, 2013年长春区域赛F题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4818 深深地补一个坑~~~ 现场赛坑在这题了,TAT.... 今天把代码改了下,过掉了,TAT 很明显 ...

  10. 高斯消元 分析 && 模板 (转载)

    转载自:http://hi.baidu.com/czyuan_acm/item/dce4e6f8a8c45f13d7ff8cda czyuan 先上模板: /* 用于求整数解得方程组. */ #inc ...

随机推荐

  1. List接口的实现类(Vector)(与ArrayList相似,区别是Vector是重量级的组件,使用使消耗的资源比较多。)

      LinkedList提供以下方法:(ArrayList无此类方法) addFirst();    removeFirst();   addLast();   removeLast(); 在堆栈中, ...

  2. C#高阶与初心:(一)List.Add添加的到底是什么?

    前几日与同事讨论一个相对复杂的场景,需要先将中间过程存储在List中,稍后再用.同时程序类的许多线程共用了一个全局变量. 具体来说就是如下代码 ... _order = order1; _list.A ...

  3. 【App】不通过App Store实现ios应用分发下载安装(转)

    转自:https://www.cnblogs.com/star91/p/5018995.html   最近公司的项目准备着手宣传工作了,宣传手册上要印制App的下载地址二维码,但是客户端应用还未上线, ...

  4. 必问的Java集合框架面试题

    Arraylist 与 LinkedList 异同 是否保证线程安全: ArrayList 和 LinkedList 都是不同步的,也就是不保证线程安全: 底层数据结构: Arraylist 底层使用 ...

  5. 解决JS(Vue)input[type='file'] change事件无法上传相同文件的问题

    Html <input id="file" type="file" accept=".map" onchange="uplo ...

  6. windows 下面必备软件

    弹窗拦截软件 http://www.pc6.com/pc/tcguanggaolj/

  7. Mesos:数据库使用的持久化卷

    摘要: Mesos为很多不同的用户场景都提供了精妙的,考虑周全的API.持久化卷是由新的acceptOffers API引入的特性.持久化卷让用户可以为Mesos构建数据库框架,Mesos可以在任何不 ...

  8. php算法题

    一群猴子排成一圈,按1,2,…,n依次编号.然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去…,如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫做大 ...

  9. C#在WinForm下使用HttpWebRequest上传文件

    转自:http://blog.csdn.net/shihuan10430049/article/details/3734398 这段时间因项目需要,要实现WinForm下的文件上传,个人觉得采用FTP ...

  10. vue2.0 源码解读(一)

    又看完一遍中文社区的教程接下来开始做vue2.0的源码解读了! 注:解读源码时一定要配合vue2.0的生命周期和API文档一起看 vue2.0的生命周期分为4主要个过程 create. 创建---实例 ...