Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4839   Accepted: 2350

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

/**
题意:根据给出的图,问有多少种方法使得变为全‘y’
做法:高斯消元 建一个n*n的矩阵
**/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#define maxn 250
using namespace std;
int mmap[maxn][maxn];
int x[maxn];
int equ,val;
char ch[][];
int free_x[maxn];
int gcd(int a,int b)
{
if(b == ) return a;
return gcd(b,a%b);
}
int Lcm(int a,int b)
{
return a/gcd(a,b)*b;
}
int Guess()
{
int lcm;
int ta;
int tb;
int max_r;
int k;
int col;
col = ;
for(k = ; k<equ&&col < val; k++,col++)
{
max_r = k;
for(int i=k+; i<equ; i++)
{
if(abs(mmap[i][col]) > abs(mmap[max_r][col]))
{
max_r = i;
}
}
if(mmap[max_r][col] == )
{
k--;
continue;
}
if(max_r != k)
{
for(int i=col; i<val+; i++)
{
swap(mmap[max_r][i],mmap[k][i]);
}
}
for(int i=k+; i<equ; i++)
{
if(mmap[i][col] != )
{
for(int j=col; j<val+; j++)
{
mmap[i][j] ^= mmap[k][j];
}
}
}
}
for(int i=k; i<equ; i++)
{
if(mmap[i][col] != ) return -;
}
for(int i=val-; i>=; i--)
{
x[i] = mmap[i][val];
for(int j=i+; j<val; j++)
{
x[i] ^= (mmap[i][j] & x[j]);
}
}
return ;
}
void init(int n)
{
memset(x,,sizeof(x));
memset(mmap,,sizeof(mmap));
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
int tt = i * n +j;
mmap[tt][tt] = ;
if(i > ) mmap[(i-)*n+j][tt] = ;
if(i < n-) mmap[(i+)*n+j][tt] = ;
if(j > ) mmap[i*n + j - ][tt] = ;
if(j < n-) mmap[i*n + j + ][tt] = ;
}
}
}
void solve(int tt)
{
int res = Guess();
if(res == -) printf("inf\n");
else if(res == )
{
int ans = ;
for(int i=; i<=tt; i++)
{
ans += x[i];
}
printf("%d\n",ans);
return;
}
else
{
int ans = 0x3f3f3f3f;
int tot = (<<res);
for(int i=; i<tot; i++)
{
int cnt = ;
for(int j=; j<res; j++)
{
if(i &(<<j))
{
x[free_x[j]] = ;
cnt++;
}
else x[free_x[j]] = ;
}
for(int j=val-tt-; j>=; j--)
{
int k;
for( k=j; k<val; k++)
if(mmap[j][k]) break;
x[k] = mmap[j][val];
for(int l=k+; l < val; l++)
if(mmap[j][l]) x[k] ^= x[l];
cnt += x[k]; }
ans = min(ans,cnt);
}
printf("%d\n",ans);
}
return;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
char c[];
init(n);
int tt = n*n;
equ = val = tt;
for(int i=; i<n; i++)
{
scanf("%s",c);
for(int j=; j<n; j++)
{
if(c[j] == 'y') mmap[i*n+j][tt] = ;
else mmap[i*n+j][tt] = ; }
}
solve(tt);
}
return ;
}

POJ-1681的更多相关文章

  1. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  2. POJ 1681 (开关问题+高斯消元法)

    题目链接: http://poj.org/problem?id=1681 题目大意:一堆格子,或白或黄.每次可以把一个改变一个格子颜色,其上下左右四个格子颜色也改变.问最后使格子全部变黄,最少需要改变 ...

  3. OpenJudge 2813 画家问题 / Poj 1681 Painter's Problem

    1.链接地址: http://bailian.openjudge.cn/practice/2813 http://poj.org/problem?id=1681 2.题目: 总时间限制: 1000ms ...

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

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

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

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

  6. poj 1681 Painter&#39;s Problem(高斯消元)

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

  7. poj 1681 Painter's Problem

    Painter's Problem 题意:给一个n*n(1 <= n <= 15)具有初始颜色(颜色只有yellow&white两种,即01矩阵)的square染色,每次对一个方格 ...

  8. poj 1681(Gauss 消元)

    Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5875   Accepted: 2825 ...

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

    题目链接 题意:有一面墙每个格子有黄白两种颜色,刷墙每次刷一格会将上下左右中五个格子变色,求最少的刷方法使得所有的格子都变成yellow. 题解:通过打表我们可以得知4*4的一共有4个自由变元,那么我 ...

  10. POJ 1681 Painter's Problem (高斯消元 枚举自由变元求最小的步数)

    题目链接 题意: 一个n*n 的木板 ,每个格子 都 可以 染成 白色和黄色,( 一旦我们对也个格子染色 ,他的上下左右 都将改变颜色): 给定一个初始状态 , 求将 所有的 格子 染成黄色 最少需要 ...

随机推荐

  1. 在linux服务器上搭建相对安全的FTP服务器

    一.如何在Linux服务器上安装vsftp不在多说,直接介绍如何进行安全性配置: 二.编辑vsftp.conf文件 关键配置项如下: anonymous_enable=NO /禁止匿名用户登录 loc ...

  2. Vue报错

    Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 8.x Found ...

  3. PHP实现---汉字简体繁体转换

    這是以class包裝好的簡繁體轉換的class, 另外需要兩個table檔: 1. http://www.jerry.com.tw/php/big5.map 2. http://www.jerry.c ...

  4. 【单调队列】【P1714】 切蛋糕

    传送门 Description 今天是小Z的生日,同学们为他带来了一块蛋糕.这块蛋糕是一个长方体,被用不同色彩分成了N个相同的小块,每小块都有对应的幸运值. 小Z作为寿星,自然希望吃到的第一块蛋糕的幸 ...

  5. 什么是static?什么是final?

    由static修饰的变量.常量.和方法被称为是静态变量.常量和 方法. 静态数据和静态方法的作用通常是为了提供共享数据或方法,如数学计算公式等,以static声明并且实现,这样当需要使用时,直接使用类 ...

  6. 二叉树中和为某一值得路径 java实现

    本题来自<剑指offer> 路径为从根节点到叶节点一条路径,路径经过的各节点数值之和等于某一给定数值,则打印路径上的节点 因为需要打印满足条件的路径节点信息和各节点之和,需要栈记录经过的节 ...

  7. [samba]samba设置指定用户权限

    步骤: 1.在系统中添加用户 批量添加用户和密码的方法(因为samba用户要求必须在系统中存在): for name in a b c d;do useradd $name ; echo " ...

  8. 使用html5的Geolocation API实现定位

    公司有个需求,需要获取用户的位置,所以看了下html5的Geolocation 这个新东西,发现挺好用的. <!DOCTYPE html> <html> <body> ...

  9. Ajax请求回调函数没有被调用

    $.ajax({        type:"post",        url:"http://172.16.41.91:8080/FcsServletSSM/users ...

  10. ZooKeeper观察者(十三)

    观察者:扩展ZooKeeper而不影响写性能 尽管ZK运行地很好通过客户端直接连接来投票集群的成员,这个结构使它很难扩展出很多客户端.问题是当我们加入更多的投票成员时,写性能就会下降.这是因为一个写操 ...