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. props设置state误区

    class Component extends React.Component { constructor(props) { super(props); this.state = { value: t ...

  2. C++分离字符串中的数字和字符 转

    #include <iostream> #include <string> #include <vector> using namespace std; void ...

  3. mysql 集群+主从同步

    SQL节点: 给上层应用层提供sql访问. 管理节点(MGM):  管理整个集群. 启动,关闭集群. 通过ndb_mgmd命令启动集群 存储/数据节点: 保存cluster中的数据.  数据节点,可以 ...

  4. sublime Text 块编辑方法

    比如我们要把SQL语句中的多表查询结果封装成pojo SQL: SELECT a.id, a.title, a.sell_point, a.price, a.image, b.`name` categ ...

  5. 淘淘相关DTO

    result 用于Controller层返回值或Controller于service层之间返回值 package com.taotao.common.pojo; import java.util.Li ...

  6. Leetcode 703. 数据流中的第K大元素

    1.题目要求 设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器, ...

  7. last-child 选择器

    <!DOCTYPE html> <html> <head> <style> p:last-child //p的父类 的子类下最后一个,就是p兄弟层的最后 ...

  8. uboot 的命令体系

    1.代码位置 (1)uboot命令体系的实现代码在uboot/common/cmd_xxx.c中.有若干个.c文件和命令体系有关.(还有command.c  main.c也是和命令有关的) 2.传参方 ...

  9. SqlDataAdapter 用法详解

    SqlCommand是sql命令,执行后通过sqlDataAdapter返回填入DataSet SqlDataAdapter   有不同的构造函数, SqlDataAdapter(SqlCommand ...

  10. 使用 html2canvas 实现浏览器截图

    基于上一篇<h5 本地上传图片预览 源码下载>,今天分享一个图片上传后, 根据所上传的图片颜值随机生成一个答案, 并且可以生成一张专属于自己的名片. 首先上传预览我们已经实现了, 所以接下 ...