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;
}
 
 
 
 

最新文章

  1. UITableView全面解析
  2. rfc2616 HTTP Protocl Analysis
  3. android数据存储之Sqlite(二)
  4. CMOS and BIOS
  5. Core Data系列文章(一)Core Data基础
  6. 记一次 Newtonsoft.Json 巧妙的用法(C#)
  7. shell的含义
  8. Linux 获得机器的IP和网卡信息
  9. sql视图显示数据不对应
  10. bootstrap简单使用
  11. 通过Cookie统计上次网页访问时间
  12. Java基础之多线程篇(线程创建与终止、互斥、通信、本地变量)
  13. 多线程Java Socket编程
  14. Java - 阅读与查找
  15. 深入理解磁盘文件系统之inode
  16. JVM、Java编译器和Java解释器
  17. 求交错序列前N项和(15 分)
  18. BZOJ5305 HAOI2018苹果树(概率期望+动态规划)
  19. org.springframework.web.HttpMediaTypeNotSupportedException: Content type &#39;multipart/form-data;boundary=----WebKitFormBoundaryRAYPKeHKTYSNdzc1;charset=UTF-8&#39; not supported
  20. Java学习--使用 Math 类操作数据

热门文章

  1. HDU 1070 Milk (模拟)
  2. Angular2.0 基础: 环境搭建
  3. python基础===理解Class的一道题
  4. sql server查看创建表的代码,表定义
  5. 【UOJ#9】vfk的数据
  6. bzoj 1191 超级英雄Hero
  7. Centos7 IP地址配置方法
  8. DevExpress.XtraTreeList 小结
  9. Sql 中取小数点后面两位小数
  10. [水煮 ASP.NET Web API2 方法论](1-1)在MVC 应用程序中添加 ASP.NET Web API