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

    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
    

    题意:

      给你一个只含'Y‘  或 'W'颜色的图

      每次你可以选择图上一个点 (i,j)

      那么 (i,j)及四周的 点的颜色都会改变

      给出原始图,问你最少次数的选择使得 最终图的颜色全部为 'Y';

    题解:

      高斯消元n*n个方程

      只有自己和四周的点的改变会使其变化

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18;
    const double Pi = acos(-1.0);
    const int N = 600+10, M = 1e2+11, mod = 1e9+7, inf = 0x3fffffff;
    
    int a[N][N],x[N],free_x[N],n;
    int ss[4][2] = {-1,0,0,-1,1,0,0,1};
    int Guass(int equ,int var) {
        int i,j,k;
        int max_r;
        int col;
        int ta,tb;
        int LCM;
        int temp;
        int free_index;
        int num=0;
        for(int i=0;i<=var;i++)
        {
            x[i]=0;
            free_x[i]=0;
        }
        col=0;
        for(k = 0;k < equ && col < var;k++,col++)
        {
            max_r=k;
            for(i=k+1;i<equ;i++)
            {
                if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;
            }
            if(max_r!=k)
            {
                for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);
            }
            if(a[k][col]==0)
            {
                k--;
                free_x[num++]=col;
                continue;
            }
            for(i=k+1;i<equ;i++)
            {
                if(a[i][col]!=0)
                {
                    for(j=col;j<var+1;j++)
                    {
                        a[i][j] ^= a[k][j];
                    }
                }
            }
        }
        for (i = k; i < equ; i++)
        {
            if (a[i][col] != 0) return inf;
        }
         for(i=var-1;i>=0;i--)
        {
            x[i]=a[i][var];
            for(j=i+1;j<var;j++)
              x[i]^=(a[i][j]&&x[j]);
        }
        int cnt = 0;
        for(int i = 0; i < n*n; ++i) if(x[i]) cnt++;
        return cnt;
    }
    char ch[N][N];
    int main() {
            int T;
            scanf("%d",&T);
            while(T--) {
                scanf("%d",&n);
                memset(a,0,sizeof(a));
                for(int i = 0; i < n; ++i) scanf("%s",ch[i]);
                for(int i = 0; i < n; ++i) {
                    for(int j = 0; j < n; ++j) {
                        a[i*n+j][n*n] = 1^(ch[i][j] == 'y');
                    }
                }
                for(int i = 0; i < n; ++i) {
                    for(int j = 0; j < n; ++j) {
                        a[i*n+j][i*n+j] = 1;
                        for(int k = 0; k < 4; ++k) {
                            int ii = i + ss[k][0];
                            int jj = j + ss[k][1];
                            if(ii < 0 || jj < 0 || ii >= n ||jj >= n) continue;
                            a[i*n+j][ii*n+jj] = 1;
                        }
                    }
                }
                int ans = Guass(n*n,n*n);
                if(ans == inf) puts("inf");
                else
                printf("%d
    ",ans);
            }
            return 0;
    }
  • 相关阅读:
    Android 主题theme说明 摘记
    Android开发 去掉标题栏方法 摘记
    安卓项目五子棋代码详解(二)
    关于 ake sure class name exists, is public, and has an empty constructor that is public
    百度地图3.0实现图文并茂的覆盖物
    android onSaveInstanceState()及其配对方法。
    关于集成科大讯飞语音识别的 一个问题总结
    android 关于 webview 控制其它view的显示 以及更改view数据失败的问题总结
    C# 解析 json Newtonsoft果然强大,代码写的真好
    c#数据类型 与sql的对应关系 以及 取值范围
  • 原文地址:https://www.cnblogs.com/zxhl/p/5885930.html
Copyright © 2011-2022 走看看