Painter's Problem

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5875   Accepted: 2825

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

Source

 

很明显的异或版Gauss 消元,其中还要枚举出自由变元的取值来确定确定变元的值以查找最小的答案。
 #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define clr(x) memset(x,0,sizeof(x))
#define clrdown(x) memset(x,-1,sizeof(x))
using namespace std;
int A[][];
int x[];
int free_x[];
int mov[][]={,,,-,,,-,};
char s[];
void init(int n);
int Gauss(int n);
int main()
{
int T,n,p;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init(n);
if((p=Gauss(n))==-)
{
printf("inf\n");
}
else
{
printf("%d\n",p);
} }
return ;
}
//初始化以及读入操作
void init(int n)
{
clr(A);
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
A[i*n+j][i*n+j]=;
for(int k=;k<;k++)
if(i+mov[k][]>= && i+mov[k][]<n && j+mov[k][]>= && j+mov[k][]<n)
{
A[(i+mov[k][])*n+j+mov[k][]][i*n+j]=;
}
}
for(int i=;i<n;i++)
{
scanf("%s",s);
for(int j=;j<n;j++)
if(s[j]=='w')
A[i*n+j][n*n]=;
}
/* for(int i=0;i<n*n;i++)
{
for(int j=0;j<=n*n;j++)
printf("%d ",A[i][j]);
printf("\n");
}*/
clr(free_x);
return ;
}
//gauss消元部分
int Gauss(int n)
{
int num=;
int k,col;
//从第0行0列开始消元
for(k=,col=;k<n*n && col<n*n;col++,k++)
{
if(!A[k][col])
{
for(int i=k+;i<n*n;i++)
if(A[i][col])
{
for(int j=col;j<=n*n;j++)
swap(A[k][j],A[i][j]);
break;
}
}//找k列有最大值的行与之交换(即只要有1)。
if(!A[k][col])
{
k--;
free_x[num++]=col;//记录自由变元的位置
continue;
}//该行全是0,指向当前行下一列并记录自由变元的下标col
for(int i=k+;i<n*n;i++)
if(A[i][col])
for(int j=col;j<=n*n;j++)
A[i][j]=(A[i][j]+A[k][j])%;
}//消元部分
for(int i=;i<n*n;i++)
/* {
for(int j=0;j<=n*n;j++)
printf("%d ",A[i][j]);
printf("\n");
}
printf("%d %d\n",num,k);*/
for(int i=k;i<n*n;i++)
if(A[i][n*n])
return -;
//若k行及之后有(0,0,0,0,……,1)的行则无解,返回-1
int p=n*n-k;//p即为自由变元的数量
int c,temp,ans,minn=,index,ct;
for( index=;index<(<<p);index++)//index从0开始枚举自由变元至1<<p
{
clrdown(x);
ans=;
ct=index;
for(int i=;i<p;ct>>=,i++)
if(ct&)
{
ans++;
x[free_x[i]]=;
}
else
x[free_x[i]]=;
//给是自由变元的x[i]赋值
for(int i=k-;i>=;i--)
{
c=n*n-;
temp=A[i][col];
while(x[c]!=-)
{
if(x[c])
temp=(temp+A[i][c])%;
c--;
}
x[c]=temp;
if(x[c])
ans++;
}
if(ans<minn)
minn=ans;
// printf("%d %d\n",minn,ans);
}
return minn;
}
 
 
 
 

poj 1681(Gauss 消元)的更多相关文章

  1. POJ 1681 高斯消元 枚举自由变元

    题目和poj1222差不多,但是解法有一定区别,1222只要求出任意一解,而本题需要求出最少翻转次数.所以需要枚举自由变元,变元数量为n,则枚举的次数为1<<n次 #include < ...

  2. POJ 1830 开关问题(Gauss 消元)

    开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7726   Accepted: 3032 Description ...

  3. hdu 5755(Gauss 消元) &poj 2947

    Gambler Bo Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tota ...

  4. $Gauss$消元

    $Gauss$消元 今天金牌爷来问我一个高消的题目,我才想起来忘了学高消... 高斯消元用于解线性方程组,也就是形如: $\left\{\begin{matrix}a_{11}x_1+a_{12}x_ ...

  5. 求一个n元一次方程的解,Gauss消元

    求一个n元一次方程的解,Gauss消元 const Matrix=require('./Matrix.js') /*Gauss 消元 传入一个矩阵,传出结果 */ function Gauss(mat ...

  6. Gauss 消元(模板)

    /* title:Gauss消元整数解/小数解整数矩阵模板 author:lhk time: 2016.9.11 没学vim的菜鸡自己手打了 */ #include<cstdio> #in ...

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

  8. poj 2065 高斯消元(取模的方程组)

    SETI Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1735   Accepted: 1085 Description ...

  9. POJ1830开关问题——gauss消元

    题目链接 分析: 第一个高斯消元题目,操作是异或.奇偶能够用0.1来表示,也就表示成bool类型的方程,操作是异或.和加法没有差别 题目中有两个未知量:每一个开关被按下的次数(0.1).每一个开关的转 ...

随机推荐

  1. Achain 钱包部署

    官网 GIT: [ Achain_linux ] 基础环境 OS: CentOS, Ubuntu Achain: 官网 [ release 最新版本 ] 安装 Achain 钱包 下载 CentOS ...

  2. python学习笔记(二)之python简单实践

    1 安装python开发环境 Linux环境下自动安装好了python,可以通过以下命令更新到python最新版本. #echo "alias python=/usr/bin/python3 ...

  3. python keras YOLOv3实现目标检测

    1.连接 https://www.jianshu.com/p/3943be47fe84

  4. poj 2104 可持久化线段树

    我们先离散化,然后根据权值建立线段树,假设我们现在有一颗权值线段树,表示在区间1-n中每个数出现了几次,那么我们可以二分的求出来这个区间的k大值,类似sbt的select操作,那么因为点的权值插入是无 ...

  5. js中的for in 循环

    1.数组 使用for in 遍历数组时,其索引被视为对象的属性,从而直接输出数组的索引 var arr = ["a","b","c"]; f ...

  6. Linux 入门记录:二、Linux 文件系统基本结构

    一.树状目录结构 Linux 文件系统是一个倒置的单根树状结构.文件系统的根为"/":文件名严格区分大小写:路径使用"/"分割(Windows 中使用" ...

  7. python基础===装饰器@property 的扩展

    以下来自Python 3.6.0 Document: class property(fget=None, fset=None, fdel=None, doc=None) Return a proper ...

  8. 在LINUX平台上手动创建多个实例(oracle11g)

    在LINUX平台上手动创建多个实例(oracle11g) http://blog.csdn.net/sunchenglu7/article/details/39676659 ORACLE linux ...

  9. window.screen.height和window.screen.availHeight和document.body.clientHeight和document.documentElement.clientHeight

    说这几个属性前 我说一下我的设备 我的设备有两个,一个高度为1080的显示器,一个高度为800的电脑 第一种:window.screen.height 这个方法是获取用户电脑屏幕的高度,是不关浏览器或 ...

  10. Hadoop中文编码乱码相关问题

    mapreduce程序处理GBK编码数据并输出GBK编码数据, hadoop涉及输出文本的默认输出编码统一用没有BOM的UTF-8的形式,但是对于中文的输出window系统默认的是GBK,有些格式文件 ...