Painter's Problem
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4420   Accepted: 2143

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
 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <set>
using namespace std;
const int MAXN=;
int a[MAXN][MAXN];
int x[MAXN];
bool free_x[MAXN];
inline int gcd(int a,int b)
{
int t;
while(b!=)
{
t=b;
b=a%b;
a=t;
}
return a;
}
inline int lcm(int a,int b)
{
return a/gcd(a,b)*b;
}
int Gauss(int equ,int var)
{
int i,j,k;
int max_r;
int col;
col=;
for(k = ; k < equ && col < var; k++,col++)
{
max_r=k;
for(i=k; i<equ; i++)
{
if(a[i][col])
{
max_r=i;
break;
}
}
if(max_r!=k)
{
for(j=k; j<var+; j++) swap(a[k][j],a[max_r][j]);
}
if(a[k][col]==)
{
k--;
continue;
}
for(i=; i<equ; i++)
{
if(i!=k&&a[i][col]!=)
{
for(j=; j<var+; j++)
{
a[i][j]^= a[k][j];
}
}
}
}
for (i = k; i < equ; i++)
{
if (a[i][col] != ) return ;
}
return ;
}
int main()
{
int n,m,t,i,j;
cin>>t;
char x;
while(t--)
{
memset(a,,sizeof(a));
cin>>n;
m=n*n;
for(i=; i<m; i++)
{
if(i%n==)getchar();
x=getchar();
if(x!='y')a[i][m]=;
}
for(i=; i<m; i++)
{
a[i][i]=;
if(i-n>=)
a[i][i-n]=;
if(i+n<m)
a[i][i+n]=;
if(i%n)
a[i][i-]=;
if((i+)%n)
a[i][i+]=;
}
if(!Gauss(m,m))cout<<"inf"<<endl;
else
{
int ans=;
for(i=; i<m; i++)ans+=a[i][m]&;
cout<<ans<<endl;
}
}
}

Painter's Problem poj1681 高斯消元法的更多相关文章

  1. poj1681 Painter's Problem(高斯消元法,染色问题)

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

  2. (模板)poj1681 高斯消元法求异或方程组(无解、唯一解、多解)

    题目链接:https://vjudge.net/problem/POJ-1681 题意:类似于poj1222,有n×n的01矩阵,翻转一个点会翻转其上下左右包括自己的点,求最少翻转多少点能使得矩阵全0 ...

  3. poj 1681 Painter's Problem

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

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

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

  5. [POJ1681]Painter's Problem(高斯消元,异或方程组,状压枚举)

    题目链接:http://poj.org/problem?id=1681 题意:还是翻格子的题,但是这里有可能出现自由变元,这时候枚举一下就行..(其实这题直接状压枚举就行) /* ━━━━━┒ギリギリ ...

  6. [Gauss]POJ1681 Painter's Problem

    和POJ1222(分析)完全相同 题意也类似, 可以涂自己以及上下左右五个位置的颜色 问几次能全部涂色 不能输出inf 01方程组 用异或来求解就好了 ][]; // 增广矩阵 ]; // 解 ]; ...

  7. poj1681 Painter's Problem

    题目描述: 和那道关灯差不多,求最少涂几次. 题解: 高消,然后深搜枚举自由元更新答案. 貌似这道题没卡贪心但是其他题基本都卡了. 比如$Usaco09Nov$的$lights$ 代码: #inclu ...

  8. POJ1681 Painter's Problem(高斯消元)

    题目看似与线性方程组无关,但可以通过建模转化为线性方程组的问题. 对于一块砖,刷两次是没有必要的,我们令x=1表示刷了一次,x=0没有刷,一共有n*n个,所以相当于有n*n个未知量x. 定义aij表示 ...

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

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

随机推荐

  1. SVG基本形状及样式设置

    前面的话 图形分为位图和矢量图.位图是基于颜色的描述,是由像素点组成的图像:而矢量图是基于数学矢量的描述,是由几何图元组成的图像,与分辨率无关.可缩放矢量图形,即SVG,是W3C XML的分支语言之一 ...

  2. docfx (一)

    什么是docFX? DocFX 是一个基于.NET的API文档生成器,当前支持 C# 和 VB.它可以通过你的代码中的三斜杠注释生成 API 参考文档.同样也支持你使用 Markdown 文件创建一些 ...

  3. Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(2)

    上一篇:Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(1) 服务器版本 Ubuntu 16.04 LTS. 本 ...

  4. HDU 6184 Counting Stars 经典三元环计数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6184 题意: n个点m条边的无向图,问有多少个A-structure 其中A-structure满足V ...

  5. 基于大数据的电影网站项目开发之CentOS的安装(一)

    一.下载VMware并安装,至于安装教程从网上搜索进行参考 二.下载系统镜像文件,这里使用的是CentOS-6.5-x86_64-bin-DVD1.iso 三.安装镜像文件 运行VMware work ...

  6. Windows环境下最新OpenCV和Contribute代码的联合编译

    解决这个问题,目的在于获得并使用最新的完全版本的代码,主要方法是对CMake能够熟练使用,并且对编译等基础支持有所了解. 一.工具的准备 1 tortoisegit www.tortoisegit.o ...

  7. visual studio xamarin 离线安装文件以及 android 模拟器

    介绍 为了使用vs开发android我也是煞费苦心,先是从网上各种搜刮文章,然后找各种各样的离线包(因为国内网络是下载不了C#/Xamain)的包的,还有各种各样的安装包,都已快接近奔溃的边缘.每次不 ...

  8. 利用MySQL触发器实现check和assertion

    MySQL虽然输入check语句不会报错,但是实际上并没有check的功能.但是MySQL 依然可以利用触发器来实现相应功能. 本文将根据两个例子简要阐述MySQL实现check和assertion的 ...

  9. 编程零基础应当如何开始学习 Python?

    提前说一下,这篇福利多多,别的不说,直接让你玩回最有手感的怀旧游戏,参数贴图很方便自己可以根据喜好修改哦. 本篇通过以下四块展开,提供大量资源对应. 选一个好版本 有没有看过<在下坂本,有何贵干 ...

  10. Httprequest 获取url 常用方法

    HttpServletRequest常用获取URL的方法         1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路 ...