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. Vue项目搭建过程

    环境搭建:mac+nodejs+npm #安装node.js : $ brew install node #安装vue-cil: $ npm install -g vue-cli 注:官网下载安装no ...

  2. bzoj2588: Spoj 10628. Count on a tree(树上第k大)(主席树)

    每个节点继承父节点的树,则答案为query(root[x]+root[y]-root[lca(x,y)]-root[fa[lca(x,y)]]) #include<iostream> #i ...

  3. 【HASH】【UVA 10125】 Sumset

    传送门 Description 给定一个整数集合S,求一个最大的d,满足a+b+c=d,其中a,b,c,d∈S Input 多组数据,每组数据包括: 第一行一个整数n,代表元素个数 下面n行每行一个整 ...

  4. 【DP】【Uva437】UVA437 The Tower of Babylon

    传送门 Description Input Output Sample Input Sample Output Case : maximum height = Case : maximum heigh ...

  5. iOS开发ARC机制下的内存管理技术要点

    转载一篇: iOS开发ARC内存管理技术要点.ARC内存管理原则总结.iOS ARC内存管理总结 ARC内存管理机制 (一)ARC的判断准则: 只要没有任何一个强指针指向该对象,该对象就会被释放. ( ...

  6. stout代码分析之八:cache类

    stout中实现了LRU cache.cache的成员如下: public: typedef std::list<Key> list; typedef std::tr1::unordere ...

  7. zigbee ------ JN5169低功耗设置

    低功耗睡眠设置Power Manager (PWRM) PWRM_vInit() 如果进入睡眠模式,设置芯片进入何种睡眠模式 PWRM_eScheduleActivity()设置进入睡眠多长时间(时钟 ...

  8. FreeRTOS ------ 栈、堆、任务栈

    1.任务的栈资源(创建任务分配的资源,单位是4字节)来自 configTOTAL_HEAP_SIZE 定义的堆,如果任务栈总量超过 configTOTAL_HEAP_SIZE,任务创建失败: 2.如果 ...

  9. RotateAnimation 详解

    RotateAnimation 详解 看看新闻网>看引擎>开源产品 其他构造器的旋转也可参考这副图. RotateAnimation旋转坐标系为以旋转点为坐标系(0,0)点.x轴为0度,顺 ...

  10. Java设计模式の观察者模式(推拉模型)

    目录: 一.观察者定义 二.观察者模式的结构(推模式实现) 三.推模型和拉模型(拉模式实现) 四.JAVA提供的对观察者模式的支持 五.使用JAVA对观察者模式的支持(自带推模式实现实例) 一.观察者 ...