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. Windows10下virtualenv配置

    1.安装virtualenv pip install virtualenv 2.选定一个目录,作为存储不同环境的总目录 3.安装virtualenvwrapper-powershell(只适用于Pyt ...

  2. 在Linux下使用gcc编译mesa文件报undefined reference to symbol 'sin@@GLIBC_2.2.5和DSO missing from command line两个错误的解决方案

    一.概述 在Linux系统下使用gcc编译用C语言写的mesa的示例程序. 环境:Ubuntu Server 18.04.1 二.问题的出现 在Ubuntu下安装好mesa所需的库文件,将目标文件从g ...

  3. 使用git命令push到自己的仓库,显示Unknown且没有贡献记录的解决方案

    一.问题的起因 今天用公司电脑在github上push时出现了以下问题: 用户名为unknown: 贡献记录为0: 二.解决方案 1,检查一遍自己的账号密码是否正确,如果正确,执行第二步骤操作: 2, ...

  4. UITableView 自定义多选

    前言 在上一篇文章中介绍了UITableView的多选操作,有提到将 return UITableViewCellEditingStyleDelete | UITableViewCellEditing ...

  5. 【MySQL】随机获取数据的方法,支持大数据量

    在mysql中带了随机取数据的函数,在mysql中我们会有rand()函数,很多朋友都会直接使用,如果几百条数据肯定没事,如果几万或百万时你会发现,直接使用是错误的.下面我来介绍随机取数据一些优化方法 ...

  6. JS创建对象的几种方式整理

    javascript是一种“基于prototype的面向对象语言“,与java有非常大的区别,无法通过类来创建对象.那么,既然是面象对象的,如何来创建对象呢? 一:通过“字面量”方式创建对象 方法:将 ...

  7. mobx 添加 isEmpty 装饰器

    避免 obj.xxx && obj.xxx.length 这样的写法 store import * as u from "lodash"; function isE ...

  8. CString比较不区分大小写

    第一种:都变为大写 或者都变成小写. str1.MakeUpper();str2.MakeUpper();or:str1.MakeLower();str2.MakeLower(); if(str1== ...

  9. js判断PC端还是移动端

    function goPAGE() { if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobi ...

  10. Java中的日期格式转化

    package lianxi; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util ...