zoukankan      html  css  js  c++  java
  • POJ 1681---Painter's Problem(高斯消元)

    POJ   1681---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

     
    代码如下:
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    using namespace std;
    const int maxn = 240;
    
    int equ,var;
    int a[maxn][maxn];
    int x[maxn]; // 解集.
    int pos[maxn];
    ///int free_num;
    
    void init_a()///对称阵;
    {
        memset(a,0,sizeof(a));
        for(int i=0;i<var*var;i++)
        {
            a[i][i]=1;
            int t=i%var;
            if(t>0) a[i][i-1]=1;
            if(t<var-1) a[i][i+1]=1;
            t=i/var;
            if(t>=1) a[i][i-var]=1;
            if(t<var-1) a[i][i+var]=1;
        }
    }
    
    int Gauss()
    {
        int i, j, k;
        int max_r; // 当前这列绝对值最大的行.
        int t=0;///记录自由元的个数;
        int col = 0; /// 当前处理的列.
    
        for (k = 0; k < equ*equ && col < var*var; k++, col++)
        {
            max_r = k;
            for (i = k + 1; i < equ*equ; i++)
            {
                if (a[i][col] > a[max_r][col])
                {
                    max_r=i;
                    break;
                }
            }
            if (max_r != k)
            {
                for (j = k; j < var*var + 1; j++) swap(a[k][j], a[max_r][j]);
            }
            if (a[k][col] == 0)
            {
                /// 说明该col列第k行以下全是0了,则处理当前行的下一列.
                ///并且应当记录这个自由元;
                k--;
                pos[t++]=col;
                continue;
            }
            for (i = k + 1; i < equ*equ; i++)
            {
                if (a[i][col] != 0)
                {
                    for (j = col; j < var*var + 1; j++)
                    {
                        a[i][j]^=a[k][j];
                    }
                }
            }
        }
        for (i = k; i < equ*equ; i++)
        {
            // 对于无穷解来说,如果要判断哪些是自由变元,那么初等行变换中的交换就会影响,则要记录交换.
            if (a[i][col] != 0) return -1;
        }
        return var*var - k;
    }
    
    int solve(int s)
    {
        int ans=9999999;
        int state=(1<<s);
        for(int i=0;i<state;i++)
        {
            int cnt=0;
            memset(x,0,sizeof(x));
            for(int j=0;j<s;j++)
            {
                if(i&(1<<j)) x[pos[j]]=1,cnt++;
            }
            for(int j=var*var-s-1;j>=0;j--)
            {
                int f=1;
                int ss;
                int tmp=a[j][var*var];
                for(int k=j;k<equ*equ;k++)
                {
                    if(a[j][k]&&f)
                    {
                        ss=k;
                        f=0;
                    }
                    if(a[j][k]) tmp^=x[k];
                }
                x[ss]=tmp;
               cnt+=x[ss];
            }
            ans=min(ans,cnt);
        }
        return ans;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&equ);
            var=equ;
            init_a();
            for(int i=0;i<var*var;i++)
            {
                char x;
                cin>>x;
                if(x=='y')
                    a[i][var*var]=0;
                else a[i][var*var]=1;
            }
            int v=Gauss();
            if(v==-1)  printf("inf
    ");
            else cout<<solve(v)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    做一天业务员的感觉:辛苦!
    我的乒乓生涯之三浑浑噩噩的中学六年
    名菊照片(二)
    今天,同事的裤子破了
    昨晚,再一次兵败滑铁卢
    今天去世纪公园看名菊展,拍了好多照片发上来大家一起欣赏:)
    寻找上海市乒友喜欢打乒乓的朋友都进来看看
    同学给我两张f1照片:)
    又搞到几张f1照片,发上来给大家养眼
    我的乒乓生涯之四乒乓姻缘
  • 原文地址:https://www.cnblogs.com/chen9510/p/5591881.html
Copyright © 2011-2022 走看看