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. MIPI DSI转LVDS芯片方案TC358775XBG

    型号:TC358775XBG功能:MIPI转LVDS通信方式:IIC/MIPI Command mode分辨率:1920*1080电源:3.3/1.8/1.2封装形式:BGA64深圳长期现货 ,提供技 ...

  2. kindeditor上传图片时候,上传成功了,但是页面上却提示失败

    今天尝试着kindeditor做一个上传demo,碰到了一个日狗的问题,百度谷歌都没有答案,最后查看源码才发现问题所在,记录一下,福利大众. 碰到问题如下,图片后台明明上传成功了,返回信息也是正确的, ...

  3. vue父子组件通信

    一.父子组件间通信 vue.js 2.0提供了一个ref 的属性: 可以为子组件指定一个索引id 父组件: <template> <div id='user-login'> & ...

  4. crontab 各参数详解及如何查看日志记录

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt145 crontab各参数说明: crontab [-u user] [fi ...

  5. grunt之concat、cssmin、uglify

    周末有点懒,跑去看了<智取威虎山>,手撕鬼子的神剧情节被徐老怪一条回忆线就解释过去了,牛到极致尽是这种四两拨千斤的处理方式,手撕加分,四星推荐. --------------------- ...

  6. 团队作业9——展示博客(Beta版本)

    展示博客 1.团队成员的简介和个人博客地址,团队的源码仓库地址. 何琴琴(http://www.cnblogs.com/vviane/): 领导项目进行,协调各队员之间的矛盾合作,负责测试与需求分析. ...

  7. 团队作业8----第二次项目冲刺(beta阶段)5.20

    Day2--5.20 1.每日讨论 会议内容:1.新成员乔桦和周迪慢慢了解项目. 2.组内负责主要编程的益靖对代码进行了大概的说明. 3.对昨天的工作进行了几点总结. 4.组长对每个成员的任务完成了分 ...

  8. 第03周-Java作业评价

    1.作业存在的问题 几大扣分点: 思维导图敷衍了事 -1 代码格式混乱 -2 无码云提交记录 -2 无PTA实验总结 -2 部分题目未完成. 格式问题:到现在还搞不清楚的,主要是态度问题.相关的教程 ...

  9. 团队作业4——第一次项目冲刺(Alpha版本)5th day

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 计时模式已经大致完成了 接下来是记录成绩的部分 四.困难与问题 1.新语言的学习与适应较慢,整体的开发进展达不到预期效果, 2 ...

  10. 201521123034《Java程序设计》第六周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...