zoukankan      html  css  js  c++  java
  • poj3185 高斯消元

    The Water Bowls
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5329   Accepted: 2081

     

    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]


    题意:

    给你一排碗,当翻动其中一个时,它和周围两个都翻转,多变元枚举最小值


    /*
    poj3185
    给你20个碗排成一排,当翻动其中一个时,它和周围两个都翻转
    
    */
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    
    using namespace std;
    const int maxn = 40;
    
    int equ,var;
    int a[maxn][maxn];
    int b[maxn][maxn];
    int x[maxn];
    int free_x[maxn];
    int free_num;
    
    int Gauss()
    {
        int max_r,col,k;
        free_num = 0;
        for(k = 0,col = 0; k < equ && col < var; k++,col++)
        {
            max_r = k;
            for(int i = k+1; i < equ; i++)
            {
                if(abs(a[i][col]) > abs(a[max_r][col]))
                    max_r = i;
            }
            if(a[max_r][col] == 0)
            {
                k --;
                free_x[free_num++] = col;
                continue;
            }
            if(max_r != k)
            {
                for(int j = col; j < var+1; j++)
                    swap(a[k][j],a[max_r][j]);
    
            }
            for(int i = k + 1; i < equ; i++)
            {
                if(a[i][col] != 0)
                {
                    for(int j = col; j < var+1; j++)
                        a[i][j] ^= a[k][j];
                }
            }
    
        }
        for(int i = k; i < equ; i++)
            if(a[i][col] != 0)
                return -1;
        if(k < var) return var-k;
    
        for(int i = var-1; i >= 0; i--)
        {
            x[i] = a[i][var];
            for(int j = i +1; j < var; j++)
                x[i] ^= (a[i][j] && x[j]);
    
        }
        return 0;
    
    }
    
    int n;
    void ini()
    {
        memset(a,0,sizeof(a));
        memset(x,0,sizeof(x));
        equ = 20;
        var = 20;
        for(int i = 0;i < 20;i++)
        {
            a[i][i] = 1;
            if(i > 0) a[i-1][i] = 1;
            if(i < 20-1) a[i+1][i]= 1;
        }
    }
    
    int solve()
    {
        int t = Gauss();
        if(t == -1)
        {
            return t;
        }
        else if(t == 0)
        {
            int ans = 0;
            for(int i = 0; i < n*n; i++)
                ans += x[i];
            return ans;
        }
        else
        {
            int ans = 0x3f3f3f3f;
            int tot = (1 << t);
            for(int i = 0; i < tot; i++)
            {
                int cnt = 0;
                for(int j = 0; j < t; j++)
                {
                    if(i & (1 << j))
                    {
                        cnt ++;
                        x[free_x[j]]= 1;
                    }
                    else x[free_x[j]]= 0;
                }
    
                for(int j = var-t-1; j >= 0; j--)
                {
                    int dex;
                    for(dex = j; dex < var; dex++)
                        if(a[j][dex])
                            break;
                    x[dex] = a[j][var];
                    for(int l = dex +1; l <var ; l++)
                    {
                        if(a[j][l])
                            x[dex] ^= x[l];
                    }
                    cnt += x[dex];
                }
                ans = min(ans,cnt);
            }
            return ans;
        }
    }
    
    int main()
    {
        int tx;
        while(scanf("%d",&tx) != EOF)
        {
            ini();
            if(tx == 1)
                a[0][20] = 1;
            else
                a[0][20] = 0;
            for(int i=  1; i < 20; i ++)
            {
                scanf("%d",&tx);
                if(tx == 1)
                    a[i][20] = 1;
                else
                    a[i][20] = 0;
            }
    
            int t = solve();
            printf("%d
    ",t);
        }
        return 0;
    }
    

      





  • 相关阅读:
    面试官:重写 equals 时为什么一定要重写 hashCode?
    MyBatis 中为什么不建议使用 where 1=1?
    面试官:方法重写时需要注意哪些问题?
    Java中List排序的3种方法
    面试官:this和super有什么区别?this能调用到父类吗?
    面试官:int和Integer有什么区别?为什么要有包装类?
    HashMap 中的一个“坑”!
    Java 中接口和抽象类的 7 大区别!
    List 去重的 6 种方法,这个方法最完美!
    面试官:如何实现 List 集合去重?
  • 原文地址:https://www.cnblogs.com/Przz/p/5409642.html
Copyright © 2011-2022 走看看