Painter's Problem
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5352   Accepted: 2588

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

题意:

一个n*n 的木板,每个格子都可以染成白色和黄色,一旦我们对某个格子染色,其上下左右都将改变颜色

求将所有的格子染成黄色最少需要几次,若不能则输出inf。

当有多个自由变元时,需要进行枚举求出最小值

 /*
poj1681
类似于一元开关问题。把二维矩阵转换成一维矩阵来看。
在求出答案之后,对于有很多解的考虑枚举自由变元来求解
最小值
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld; using namespace std;
const int maxn = ; int equ,var;
int a[maxn][maxn];
int x[maxn];
int free_x[maxn];
int free_num; int Gauss()
{
int max_r,col,k;
free_num = ;
for(k = ,col = ; k < equ && col < var; k++,col++)
{
max_r = k;
for(int i = k+; i < equ; i++)
{
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
}
if(a[max_r][col] == )
{
k --;
free_x[free_num++] = col;
continue;
}
if(max_r != k)
{
for(int j = col; j < var+; j++)
swap(a[k][j],a[max_r][j]); }
for(int i = k + ; i < equ; i++)
{
if(a[i][col] != )
{
for(int j = col; j < var+; j++)
a[i][j] ^= a[k][j];
}
} }
for(int i = k; i < equ; i++)
if(a[i][col] != )
return -;
if(k < var) return var-k; for(int i = var-; i >= ; i--)
{
x[i] = a[i][var];
for(int j = i +; j < var; j++)
x[i] ^= (a[i][j] && x[j]); }
return ; } int n;
void ini()
{
memset(a,,sizeof(a));
memset(x,,sizeof(x));
equ = n*n;
var = n*n;
for(int i = ;i < n;i++)
{
for(int j = ;j < n;j++)
{
int tt = i*n+ j;
a[tt][tt] =;
if(i > ) a[(i-)*n+j][tt] = ;
if(i < n-) a[(i+)*n+j][tt] = ;
if(j > ) a[tt-][tt] = ;
if(j < n-) a[tt+][tt] =;
}
}
} char str[][];
/*
2
3
yyy
yyy
yyy
5
wwwww
wwwww
wwwww
wwwww
wwwww
*/
int main()
{
int T;
char color;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
ini();
for(int i = ; i < n; i++)
{
scanf("%s",str[i]);
for(int j = ; j < n; j++)
{
if(str[i][j] == 'y')
a[i*n+j][n*n] = ;
else
a[i*n+j][n*n] = ;
}
}
int t = Gauss();
if(t == -)
{
printf("inf\n");
}
else if(t == )
{
int ans = ;
for(int i = ; i < n*n; i++)
ans += x[i];
printf("%d\n",ans);
}
else
{
int ans = 0x3f3f3f3f;
int tot = ( << t);
for(int i = ; i < tot; i++)
{
int cnt = ;
for(int j = ; j < t; j++)
{
if(i & ( << j))
{
cnt ++;
x[free_x[j]]= ;
}
else x[free_x[j]]= ;
} for(int j = var-t-; j >= ; j--)
{
int dex;
for(dex = j; dex < var; dex++)
if(a[j][dex])
break;
x[dex] = a[j][var];
for(int l = dex +; l <var ; l++)
{
if(a[j][l])
x[dex] ^= x[l];
}
cnt += x[dex];
}
ans = min(ans,cnt);
}
printf("%d\n",ans);
}
}
return ;
}

poj1681 高斯消元的更多相关文章

  1. 高斯消元几道入门题总结POJ1222&&POJ1681&&POJ1830&&POJ2065&&POJ3185

    最近在搞高斯消元,反正这些题要么是我击败了它们,要么就是这些题把我给击败了.现在高斯消元专题部分还有很多题,先把几道很简单的入门题总结一下吧. 专题:http://acm.hust.edu.cn/vj ...

  2. 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:// ...

  3. 【BZOJ-3143】游走 高斯消元 + 概率期望

    3143: [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2264  Solved: 987[Submit][Status] ...

  4. 【BZOJ-3270】博物馆 高斯消元 + 概率期望

    3270: 博物馆 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 292  Solved: 158[Submit][Status][Discuss] ...

  5. *POJ 1222 高斯消元

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9612   Accepted: 62 ...

  6. [bzoj1013][JSOI2008][球形空间产生器sphere] (高斯消元)

    Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧 ...

  7. hihoCoder 1196 高斯消元·二

    Description 一个黑白网格,点一次会改变这个以及与其连通的其他方格的颜色,求最少点击次数使得所有全部变成黑色. Sol 高斯消元解异或方程组. 先建立一个方程组. \(x_i\) 表示这个点 ...

  8. BZOJ 2844 albus就是要第一个出场 ——高斯消元 线性基

    [题目分析] 高斯消元求线性基. 题目本身不难,但是两种维护线性基的方法引起了我的思考. void gauss(){ k=n; F(i,1,n){ F(j,i+1,n) if (a[j]>a[i ...

  9. SPOJ HIGH Highways ——Matrix-Tree定理 高斯消元

    [题目分析] Matrix-Tree定理+高斯消元 求矩阵行列式的值,就可以得到生成树的个数. 至于证明,可以去看Vflea King(炸树狂魔)的博客 [代码] #include <cmath ...

随机推荐

  1. Step by Step 真正从零开始,TensorFlow详细安装入门图文教程!帮你完成那个最难的从0到1

    摘要: Step by Step 真正从零开始,TensorFlow详细安装入门图文教程!帮你完成那个最难的从0到1 安装遇到问题请文末留言. 悦动智能公众号:aibbtcom AI这个概念好像突然就 ...

  2. SpringCloud的服务消费者 (二):(rest+feign/ribbon)声明式访问注册的微服务

    采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,Feign底层调用Ribbon2.注册在EurekaServer中的微服务api,不 ...

  3. React中路由传参及接收参数的方式

    注意:  路由表改变后要重启服务      方式 一:          通过params         1.路由表中                     <Route path=' /s ...

  4. 搭建ssm框架,可实现登录和数据展示以及增删改查

    需求: 后台使用ssm(spring-springMVC-mybatis)进行整合 前台使用bootstrap框架 前后台交互使用Ajax进行发送 表结构: 登录页面后显示所有用户信息,可对每条进行增 ...

  5. css回顾之左侧宽度自适应布局

    目标: <!DOCTYPE html> <meta charset=utf-8> <html> <head> <title>alibaba& ...

  6. 这次彻底理解了Object这个属性

    1.实例化Object对象 实例化Object对象的方式有两种:使用Object构造器和使用对象的字面量.例如: var person1 = { name: '李四' }; var person2 = ...

  7. pthon/零起点(一、集合)

    pthon/零起点(一.集合) set( )集合,集合是无序的,集合是可变的,集合是可迭代的 set()强型转成集合数据类型 set()集合本身就是去掉重复的元素 集合更新操作案列: j={1,2,3 ...

  8. Vue框架axios请求(类似于ajax请求)

    Vue框架axios get请求(类似于ajax请求) 首先介绍下,这个axios请求最明显的地方,通过这个请求进行提交的时候页面不会刷新 <!DOCTYPE html> <html ...

  9. Spring Cloud学习笔记-001

    Spring Boot快速入门 1. Eclipse新建maven工程,骨架选择quickstart: 2. 加入springboot的父工程,和web依赖: 3. 编写一个简单的RESTful接口, ...

  10. drupal 8 查看数据库用户名密码

    一.查看网站数据库名.密码 在已安装网站下 例如:我安装了一个d8radix的网站,此网站/home/jx/www/d8radix/sites/default/setting.php文件里面 数据库名 ...