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. bzoj 2588 Count on a tree 解题报告

    Count on a tree 题目描述 给定一棵\(N\)个节点的树,每个点有一个权值,对于\(M\)个询问\((u,v,k)\),你需要回答\(u\) \(xor\) \(lastans\)和\( ...

  2. Mac添加锁屏快捷键

    Mac要想添加锁屏快捷键,必须使用Automator. 1. 打开Automator,创建一个新的服务. 2. 在左侧栏中找到 启动屏幕保护 ,将其拖曳到右侧窗口内,并且修改 服务收到改为" ...

  3. java 实现多个接口 方法重名的解决办法——内部类

    package com.kk.innerClass; /** * 通过内部类实现接口 * 解决多个接口中方法重名问题 * */interface Machine { void run();} clas ...

  4. 直通BAT面试算法精讲课1

    1.有一棵二叉树,请设计一个算法,按照层次打印这棵二叉树. 给定二叉树的根结点root,请返回打印结果,结果按照每一层一个数组进行储存,所有数组的顺序按照层数从上往下,且每一层的数组内元素按照从左往右 ...

  5. angularJS 条件查询 品优购条件查询品牌(条件查询和列表展示公用方法解决思路 及 post请求混合参数提交方式)

    Brand.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...

  6. CursorAdapter中getView newView bindView异同

    Adapter的作用是界面与数据之间的桥梁,通过设置适配器至ListView控件后(如调用ListView的 setAdapter(ListAdapter adapter)               ...

  7. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  8. windows版本redis下载安装

    官方网站:http://redis.io/ 官方下载:http://redis.io/download 可以根据需要下载不同版本 在官方下载网页中有一个window版本的说明,说redis官方没有wi ...

  9. javascript拖拽原理与简单实现方法[demo]

    美国人有一句常用的俗语—“Re-inventing the Wheel”,从字面上来解释就是“重新发明轮子”.可是轮子早已问世,再要去发明岂非劳而无功? 产品经理发下需求,实施者再到网上搜索代码,也许 ...

  10. OPENId是什么, OAUTH 是什么

    what is openId open id is said to be a protocol which uses url as username, so if a website supports ...