Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5378   Accepted: 2601

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<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int T,N,ANS;
int a[][];
bool gauss(){
int now=;
for(int i=;i<=N*N;i++){
int to=now;
while(to<=N*N&&a[to][i]==) to++;
if(to>N*N) continue;
if(to!=now){
for(int j=;j<=N*N+;j++) swap(a[to][j],a[now][j]);
}
for(int j=;j<=N*N;j++){
if(j!=now&&a[j][i]){
for(int k=;k<=N*N+;k++){
a[j][k]^=a[i][k];
}
}
}
now++;
}
for(int i=now;i<=N*N;i++)
if(a[i][N*N+]!=) return false;
return true;
} int v[],cnt;
void dfs(int x){
if(cnt>=ANS) return ;//已经比目前的答案大了,没有必要再搜
if(x==){
ANS=min(cnt,ANS);
return ;
}
if(a[x][x]!=){
int num=a[x][N*N+];//num表示第x块砖染色不染色
for(int i=x+;i<=N*N;i++){
if(a[x][i]!=) num=num^v[i];//已经枚举过的x+1~N*N中某块砖如果可以对x产生影响且已染色,就让num改变一次
}
v[x]=num;
if(num==) cnt++;
dfs(x-);
if(num==) cnt--;
}
else{//枚举按或不按两种情况
v[x]=; dfs(x-);
v[x]=; cnt++; dfs(x-); cnt--;
}
} int main(){
scanf("%d",&T);
while(T--){
memset(a,,sizeof(a));
scanf("%d",&N);
for(int i=;i<=N*N;i++){
a[i][i]=;
if(i%N!=) a[i][i-]=;
if(i%N!=) a[i][i+]=;
if(i>=N+) a[i][i-N]=;
if(i<=N*(N-)) a[i][i+N]=;
}
for(int i=;i<=N;i++){
char s[];
scanf("%s",s+);
for(int j=;j<=N;j++){
if(s[j]=='w') a[(i-)*N+j][N*N+]=;
}
}
if(gauss()==false){
puts("inf");
continue;
}
ANS=<<;
dfs(N*N);
printf("%d\n",ANS);
}
return ;
}

Painter's Problem的更多相关文章

  1. poj 1681 Painter's Problem

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

  2. Painter's Problem poj1681 高斯消元法

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

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

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

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

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

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

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

  6. Painter's Problem (高斯消元)

    There is a square wall which is made of n*n small square bricks. Some bricks are white while some br ...

  7. POJ 1681 Painter's Problem(高斯消元+枚举自由变元)

    http://poj.org/problem?id=1681 题意:有一块只有黄白颜色的n*n的板子,每次刷一块格子时,上下左右都会改变颜色,求最少刷几次可以使得全部变成黄色. 思路: 这道题目也就是 ...

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

    题目链接 题意:有一面墙每个格子有黄白两种颜色,刷墙每次刷一格会将上下左右中五个格子变色,求最少的刷方法使得所有的格子都变成yellow. 题解:通过打表我们可以得知4*4的一共有4个自由变元,那么我 ...

  9. POJ 1681 Painter's Problem (高斯消元 枚举自由变元求最小的步数)

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

随机推荐

  1. Hibernate的检索方式--查询数据的方式

    Hibernate 提供了以下几种检索对象的方式1导航对象图检索方式: 根据已经加载的对象导航到其他对象(根据已经加载的对象,导航到其他对象-例如一对多的查询)2OID 检索方式: 按照对象的 OID ...

  2. 解决存储过程中拼接的SQL字符串超长导致sql语句被截取的问题

    今天遇到了一个奇葩的问题:存储过程中的sql字符串拼接的太长,超出了分页存储过程执行sql参数的nvarchar(4000)的长度. 没办法,只能修改自己的存储过程,因为分页存储过程是不能动的. 开始 ...

  3. flume jetty 进程关系 flume jetty 跨域问题 jetty 源码分析

    flume  jetty  跨域问题 13481 httpSource的端口进程号 = flume 启动后的进程号 [root@c log]# netstat -atp Active Internet ...

  4. Yii框架2.0的小部件

    小部件是视图里的可重用单元. 小部件是在视图中使用的,但是可能需要使用控制器传给他的模型,比如在渲染表单的时候.比如一般的时间拾取器就可以直接砸视图里加入如下代码就可以: <?php use y ...

  5. d3.js 之增加感染力:使用转场效果

    转场/transition 图形比数据有感染力,动起来的图形比静态的图形更有感染力. 转场是一种过渡,提供两个稳定状态间的一种动态渐进的变化.转场的概念来源于电影. 电影中存在不同场景之间的切换,比如 ...

  6. Linux上安装MySQL及其基础配置

    本文主要介绍Linux下使用yum安装MySQL,以及启动.登录和远程访问MySQL数据库. 1.安装 查看有没有安装过: yum list installed mysql* rpm -qa | gr ...

  7. JDK动态代理实现源码分析

    JDK动态代理实现方式 在Spring框架中经典的AOP就是通过动态代理来实现的,Spring分别采用了JDK的动态代理和Cglib动态代理,本文就来分析一下JDK是如何实现动态代理的. 在分析源码之 ...

  8. bat命令运行java程序

    注意空格 本文主要介绍在window下bat批处理文件调用java的方法. @echo off echo 正在加密,请稍后....echo path:%~dp0 set base=%~dp0 set ...

  9. opencv:vs2015添加了包含目录依然无法打开‘opencv2/core/core.hpp’ 解决方法

    安装环境 win10 vs2015 出错和改错 按网上的教程,配置好opencv后,包括已经把以下内容添加到'包含目录'了: E:\openCV\opencv\build\include E:\ope ...

  10. 从iOS的图片圆角想到渲染

    圆角是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受.设置圆角会带来一定的性能损耗,如何提高性能是一个需要重点讨论的话题. 大家常见的圆角代码x.layer.cornerRadius = ...