码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3185 The Water Bowls 高斯消元

时间:2016-09-16 15:23:08      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

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\n",Gauss(20,20));
        return 0;
}

 

POJ 3185 The Water Bowls 高斯消元

标签:

原文地址:http://www.cnblogs.com/zxhl/p/5876459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!