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. windows server 2012 + sql server 2008 r2安装

    windows server 2012 r2  里面安装 sql server 2008 r2 问题总结 前提是 windows server 2012 r2 已经安装完成  ,(仅仅是安装完成 啥服 ...

  2. jmeter后置处理器 JSON Extractor取多个变量值

    1.需要获取响应数据的请求右键添加-后置处理器-JSON Extractor 2.如果要获取json响应数据多个值时,设置的Variable names (后续引用变量值的变量名设置)与JSON Pa ...

  3. Personal Learning Path of Java——初识Java

    初识Java 在我个人看来,Java是一门高大上的面向编程语言,这也是Java吸引我的地方.在自学Java之前,我在学校大概学过了一些C语言的知识,在学校学的那点C语言纯属是拿来打基础用的,大概了解了 ...

  4. Android Studio开发常见问题

    Compilation failed; see the compiler error output for details 错误描述 解决方法 原因:文件编码问题.进入项目根目录,在命令提示符下执行以 ...

  5. 开源的API集成测试工具 v0.1.2 - 增强体验

    Hitchhiker 是一款开源的 Restful Api 集成测试工具,你可以在轻松部署到本地,和你的team成员一起管理Api. 详细介绍请看: http://www.cnblogs.com/br ...

  6. JS常用方法总结

    1.javascript删除元素节点 IE中有这样一个方法:removeNode(),这个方法在IE下是好使的,但是在Firefox等标准浏览器中就会报错了 removeNode is not def ...

  7. 文件系统的几种类型:ext3, s…

    分类: 架构设计与优化 1.  ext3 在异常断电或系统崩溃(不洁关机, unclean system shutdown  ).每个已挂载ext2文件系统计算机必须使用e2fsck程序来检查其一致性 ...

  8. java课程设计——博客作业教学数据分析系统(201521123083 戴志斌)

    目录 一.团队课程设计博客链接 二.个人负责模块或任务说明 三.自己的代码提交记录截图 四.自己负责模块或任务详细说明 五.课程设计感想 (题外话,终于可以用markdown建目录) 一.团队课程设计 ...

  9. JTable用法-实例

    前几篇文章介绍了JTable的基本用法,本文实现一个简单的JTable,算是前文的一个总结,并造福供拷贝党们. Swing-JTable用法-入门 Swing-JTable的渲染器与编辑器使用demo ...

  10. 201521123026 《Java程序设计》第一周学习总结

    1. 本章学习总结 1.简要了解JAVA的发展史以及其特点(面向对象.跨平台性,健壮性,安全性,可移植性,多线程性,动态性等) 2.认识JAVA三大平台(Java SE,Java EE,JavaME) ...