Painter's Problem
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 |
[Submit] [Go Back] [Status] [Discuss]
题解:高斯消元
与poj 1222类似,但是这道题需要考虑无解和自由元的情况。
如果一个方程左边的未知数的系数全是0,但是最后的结果不为0,那么一定无解。
如果一个方程左边的未知数的系数全是0,但是最后的结果为0,因为第i个方程是用来就第i个未知数的,所以就说第i个未知数是自由元(与该位置的取值无关),对这些自由元我们进行爆搜。然后将爆搜出的结果代入每个方程验证,看是否可行。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 300
#define inf 1000000000
using namespace std;
int n,m;
int a[N][N],b[N],b1[N],mark[N][N],num[N][N],tot,ans[N],vis[N];
int dx[10]={0,1,0,-1},dy[10]={1,0,-1,0};
void dfs(int x)
{
if (x==m+1) {
bool pd=true;
for (int i=1;i<=m;i++) {
int k=0;
for (int j=1;j<=m;j++)
k^=a[i][j]*vis[j];
if (k!=b1[i]) pd=false;
}
if (pd) {
int size=0;
for (int i=1;i<=m;i++)
if (vis[i]) size++;
tot=min(tot,size);
}
return;
}
if (ans[x]!=-1) vis[x]=ans[x],dfs(x+1);
else {
vis[x]=0; dfs(x+1);
vis[x]=1; dfs(x+1);
}
}
void gauss()
{
for (int i=1;i<=m;i++){
int num=i;
for (int j=i;j<=m;j++)
if (a[j][i]) {
num=j;
break;
}
if (!a[num][i]) continue;
for (int j=1;j<=m;j++) swap(a[num][j],a[i][j]);
swap(b[i],b[num]);
for (int j=1;j<=m;j++)
if (j!=i&&a[j][i]){
for (int k=1;k<=m;k++)
a[j][k]^=a[i][k];
b[j]^=b[i];
}
}
for (int i=1;i<=m;i++) b1[i]=b[i];
int size=0;
for (int i=m;i>=1;i--){
if (a[i][i]==0&&b[i]!=0) {
tot=inf;
return;
}
if (a[i][i]==0&&b[i]==0) {
ans[i]=-1;
continue; size++;
}
ans[i]=b[i]; if (ans[i]) tot++;
for (int j=i-1;j>=1;j--)
if (a[j][i]) b[j]^=ans[i];
}
if (size) {
tot=inf;
dfs(1);
}
}
int main()
{
freopen("a.in","r",stdin);
int T;
scanf("%d",&T);
for (int t=1;t<=T;t++){
scanf("%d",&n);
int cnt=0; m=n*n;
memset(num,0,sizeof(num));
memset(mark,0,sizeof(mark));
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
num[i][j]=++cnt;
for (int i=1;i<=n;i++){
char s[20]; scanf("%s",s+1);
for (int j=1;j<=n;j++)
if (s[j]=='y') mark[i][j]=0;
else mark[i][j]=1;
}
memset(b,0,sizeof(b));
memset(a,0,sizeof(a));
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++){
for (int k=0;k<=3;k++){
int nowx=i+dx[k];
int nowy=j+dy[k];
if (nowx<0||nowy<0||nowx>n||nowy>n) continue;
a[num[i][j]][num[nowx][nowy]]=1;
}
a[num[i][j]][num[i][j]]=1;
b[num[i][j]]=mark[i][j];
}
tot=0;
gauss();
if (tot!=inf) printf("%d\n",tot);
else printf("inf\n");
}
}