zoukankan      html  css  js  c++  java
  • POJ 3185 The Water Bowls 高斯消元

    The Water Bowls
     

    Description

    The cows have a line of 20 water bowls from which they drink. The bowls can be either right-side-up (properly oriented to serve refreshing cool water) or upside-down (a position which holds no water). They want all 20 water bowls to be right-side-up and thus use their wide snouts to flip bowls. 

    Their snouts, though, are so wide that they flip not only one bowl but also the bowls on either side of that bowl (a total of three or -- in the case of either end bowl -- two bowls). 

    Given the initial state of the bowls (1=undrinkable, 0=drinkable -- it even looks like a bowl), what is the minimum number of bowl flips necessary to turn all the bowls right-side-up?

    Input

    Line 1: A single line with 20 space-separated integers

    Output

    Line 1: The minimum number of bowl flips necessary to flip all the bowls right-side-up (i.e., to 0). For the inputs given, it will always be possible to find some combination of flips that will manipulate the bowls to 20 0's.

    Sample Input

    0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0

    Sample Output

    3

    Hint

    Explanation of the sample: 

    Flip bowls 4, 9, and 11 to make them all drinkable: 
    0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [initial state] 
    0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 4] 
    0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 9] 
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [after flipping bowl 11]
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    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 = 1e2+10, M = 1e2+11, mod = 1e9+7, inf = 0x3fffffff;
    
    int a[N][N],x[N],free_x[N];
    int Gauss(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 -1;
        }
        int stat=1<<(var-k);
        int res=inf;
        for(i=0;i<stat;i++)
        {
            int cnt=0;
            int index=i;
            for(j=0;j<var-k;j++)
            {
                x[free_x[j]]=(index&1);
                if(x[free_x[j]]) cnt++;
                index>>=1;
            }
            for(j=k-1;j>=0;j--)
            {
                int tmp=a[j][var];
                for(int l=j+1;l<var;l++)
                  if(a[j][l]) tmp^=x[l];
                x[j]=tmp;
                if(x[j])cnt++;
            }
            if(cnt<res)res=cnt;
        }
        return res;
    }
    void init() {
            memset(a,0,sizeof(a));
            for(int i = 0; i < 20; ++i) a[i][i] = 1;
            for(int i = 0; i < 20; ++i) {if(i >= 1)a[i][i-1] = 1; if(i < 19) a[i][i+1] = 1;}
    }
    int main() {
            init();
            for(int i = 0; i < 20; ++i) scanf("%d",&a[i][20]);
            printf("%d
    ",Gauss(20,20));
            return 0;
    }
  • 相关阅读:
    Jmeter beanshell preprocessor随机添加任意多个请求参数
    Jmeter 场景设计
    jmeter 参数化
    .net 匿名方法
    jmeter 运行脚本报错 java.net.BindException: Address already in use
    Jmeter mysql性能测试
    ngcordova 监控网络制式改变
    建立apk定时自动打包系统第一篇——Ant多渠道打包并指定打包目录和打包日期
    Kafka架构
    Linux命令
  • 原文地址:https://www.cnblogs.com/zxhl/p/5876459.html
Copyright © 2011-2022 走看看