poj1681 Painter's Problem 高斯消元

Language:
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4398 Accepted: 2134

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



题意:和poj1222类似,只不过是灯的亮暗变成染色,但是会有无解和无穷解的情况。

思路:和poj1222一样,每一个方块的颜色都是由其所在十字内5个方块内操作共同影响的,所以当方块本身为黄色,Xi ^ Xi-1 ^ Xi+1 ^ Xi-6 ^ Xi+6 = 0 ,不然Xi ^ Xi-1 ^ Xi+1 ^ Xi-6 ^ Xi+6 = 1。接下来高斯消元,特判是否有无解情况,之后dfs枚举自由元的值,更新最小的Xi=1的个数,详见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=250;
const int inf=0x3fffffff;
int ans;
int a[MAXN][MAXN],x[MAXN],flag[MAXN]; // x数组是变元的值,flag数组记录该变元是否为某行第一个非零数
char str[25];
void init()
{
    memset(a,0,sizeof(a));
    memset(x,0,sizeof(x));
    memset(flag,0,sizeof(flag));
}
void dfs(int k,int pos,int cnt,int var) //k是从下往上第k个阶梯行,pos为当前处理的变元,cnt为总数,var为变量数
{
    if(k<=-1)
    {
        if(cnt<ans) ans=cnt;
        return ;
    }
    if(flag[pos])
    {
        x[pos]=a[k][var];
        for(int j=pos+1;j<var;j++)
            if(a[k][j]) x[pos]^=x[j];
        if(x[pos])
            dfs(k-1,pos-1,cnt+1,var);
        else
            dfs(k-1,pos-1,cnt,var);
        return ;
    }
    x[pos]=0;  //枚举自由元
    dfs(k,pos-1,cnt,var);
    x[pos]=1;
    dfs(k,pos-1,cnt+1,var);
}
int Gauss(int n,int m)
{
    int i,col=0;
    for(i=0;i<n && col<m;i++,col++)
    {
        int r=i;
        for(int j=i+1;j<n;j++)
            if(abs(a[j][col])>abs(a[r][col]))  r=j;
        if(r!=i)
            for(int j=i;j<=m;j++) swap(a[r][j],a[i][j]);
        if(!a[i][col])
        {
            i--;
            continue;
        }
        for(int k=i+1;k<n;k++)
            if(a[k][col])
                for(int j=col;j<=m;j++)
                    a[k][j]^=a[i][j];
    }
    for(int k=i;k<n;k++)
        if(a[k][m])
        return -1;
    for(int k=0;k<i;k++)
        for(int j=k;j<m;j++)
            if(a[k][j])
            {
                flag[j]=1;
                break;
            }
  
    ans=inf;
    dfs(i-1,m-1,0,m);
    return ans;
}
int main()
{
    //freopen("text.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        init();
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",str);
            for(int j=0;j<n;j++)
                a[i*n+j][n*n]= str[j]=='y'? 0 : 1;
        }
        for(int i=0;i<n*n;i++)
        {
            a[i][i]=1;
            if(i%n!=0)
                a[i][i-1]=1;
            if(i%n!=n-1)
                a[i][i+1]=1;
            if(i>=n)
                a[i][i-n]=1;
            if(i<n*(n-1))
                a[i][i+n]=1;
        }
        ans=Gauss(n*n,n*n);
        if (ans==-1) printf("inf\n");
        else printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值