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. vs 2015 项目筛选器没了,.h头文件和.cpp文件混在一起了

    场景: git 拉取 VS 2015 项目,打开之后,.h头文件和.cpp文件混在一起了. 解决方案: 需要XXX..vcxproj.filters 文件.

  2. 再整合ssh时,关于Spring IOC注入问题

    No matching editors or conversion strategy found IOC问题: EmpService bean 实现了 Iemp接口,就不能直接作为参数传入Action ...

  3. vue+node+mongodb前后端分离博客系统

    感悟 历时两个多月,终于利用工作之余完成了这个项目的1.0版本,为什么要写这个项目?其实基于vuejs+nodejs构建的开源博客系统有很多,但是大多数不支持服务端渲染,也不支持动态标题,只是做到了前 ...

  4. Spring框架介绍及使用

    Spring框架—控制反转(IOC)1 Spring框架概述1.1 什么是Spring1.2 Spring的优点1.3 Spring的体系结构2 入门案例:(IoC)2.1导入jar包2.2目标类2. ...

  5. VS2017 编译Assimp

    1. 下载Assimp:http://assimp.sourceforge.net/ 2. 要下载和安装DirectX SDK 安装出现错误,错误代码s1023,解决方法:https://blog.c ...

  6. 在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”的解决方案

    eclipse在其POM文件的一处提示出错如下: Plugin execution not covered by lifecycle configuration: org.apache.maven.p ...

  7. 我的C语言编程风格

    前几天看别人的代码,真是的看的头昏脑涨,基本没有注释.乱起的变量名字,还要费尽心思去解读作者的意思.突然感觉高效的程序注释说明和良好的编程风格是多么的重要. 为了不让别人在看到我的代码时在背后骂我,也 ...

  8. [转]常见的JavaScript内存泄露

    什么是内存泄露 内存泄漏指由于疏忽或错误造成程序未能释放已经不再使用的内存.内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,导致在释放该段内存之前就失去了对该段内存的控制, ...

  9. mybatis04--Mapper动态代理实现

    通过之前的操作,我们发现dao的实现类其实并没有做什么实质性的工作,仅仅是通过sqlSession的相关API定位到StudentMapper映射文件 中的ID中的sql语句,其实真正操作DB的是ma ...

  10. vim创建程序文件自动添加头部注释/自动文件头注释与模板定义

    Vim 自动文件头注释与模板定义 在vim的配置文件.vimrc添加一些配置可以实现创建新文件时自动添加文件头注释,输入特定命令可以生成模板. 使用方法 插入模式输入模式输入seqlogic[Ente ...