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. 关于Cocos的内存管理机制引发一些异常的解决方案

    错误:引发了异常: 读取访问权限冲突. this 是 0xDDDDDDDD.或者hero是 0xDDDDDDDD.hero是在GameController里创建的对象 这个的意思是this所指向的内存 ...

  2. C# SQLite数据库

    在客户端配置文件<configuration>节点下,添加: <connectionStrings> <add name="localdb" conn ...

  3. 解决ubuntu开机进入grub界面的问题

    开机显示GRUB界面显示如下字样,几秒后自动进入登录界面 *Ubuntu Advanced options for Ubuntu .... 解决方案: 1.编辑grub文件 sudo vim /etc ...

  4. Oracle同义词(synonym)

    oracle的同义词总结   从字面上理解就是别名的意思,和视图的功能类似.就是一种映射关系.   同义词拥有如下好处:   节省大量的数据库空间,对不同用户的操作同一张表没有多少差别;   扩展的数 ...

  5. Material Designer的低版本兼容实现(五)—— ActivityOptionsCompat

    extends:http://www.cnblogs.com/tianzhijiexian/p/4087917.html 本文是对API中的方法做了介绍,如果想要看如何让这些方法兼容4.x或2.x可以 ...

  6. rabbitmq - java client lib一二事

    由于不可抗因素, 需要给对接方撸一个client的demo.基于比较老的jdk. 所幸找到了这里:http://www.rabbitmq.com/releases/rabbitmq-java-clie ...

  7. nw.js---创建一个点击菜单

    使用nw.js创建一个可点击的菜单: <!doctype html> <html lang="en"> <head> <meta char ...

  8. I - 昂贵的聘礼

    来源poj1062 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求 ...

  9. div左右居中css

    l_btn{ font-size: 1.2rem; width: 190px; height: 50px; border: 1px solid #fff; border-radius: 25px; c ...

  10. 28. css样式中px转rem

    Vue3:脚手架配置 https://blog.csdn.net/weixin_41424247/article/details/80867351 与原来的vue-cli 2.x版本不同的是:如果使用 ...