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

poj 3185 The Water Bowls(高斯消元)

时间:2014-08-24 18:07:14      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   ar   

The Water Bowls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4352   Accepted: 1721

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]

Source



高斯消元的简单题:


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;

int a[24][24],x[24];
int equ,var;//方程数和变元数

void Debug()
{
    for(int i=0;i<equ;i++)
    {
        for(int j=0;j<=var;j++)
            printf("%d ",a[i][j]);
        puts("");
    }
    puts("");
}

int gauss()
{
    int max_r,col=0;
    for(int i=0;i<equ&&col<var;i++,col++)
    {
        max_r=i;
        for(int j=i+1;j<equ;j++)
        {
            if(a[j][col]>a[max_r][col]) max_r=j;
        }
        if(max_r!=i)
        {
            for(int k=0;k<=var;k++)
            {
                swap(a[i][k],a[max_r][k]);
            }
        }
        if(a[i][col]==0)
        {
            i--;
            continue;
        }

        for(int j=i+1;j<equ;j++)
        if(i!=j&&a[j][col])
        {
            for(int k=col;k<=var;k++)
            a[j][k]=a[i][k]^a[j][k];
        }
    }

    //Debug();
    for(int i=equ-1;i<equ;i++)
    if(a[i][var]) return -1;

    int ans=100,sum=0;
    for(int k=0;k<=1;k++)
    {
       x[equ-1]=(k&1)>0?1:0;
       sum=0;
       for(int i=equ-2;i>=0;i--)
       {
        int temp=a[i][var];
        for(int j=i+1;j<var;j++)
        {
            temp-=a[i][j]*x[j];
        }
        x[i]=temp/a[i][i];
       }
       for(int i=0;i<var;i++)
       if(x[i]%2!=0)
       {
           sum++;
       }

       //printf("  sum:%d\n",sum);
       ans=min(ans,sum);
    }
    return ans;
}


void init()
{
    memset(a,0,sizeof(a));
    memset(x,0,sizeof(x));
    for(int i=0;i<20;i++)
    {
        if(i!=0) a[i][i-1]=1;
        if(i!=19) a[i][i+1]=1;
        a[i][i]=1;
    }
    //Debug();
}
int main()
{
    equ=var=20;
    int t;
    while(scanf("%d",&t)!=EOF)
    {
        init();
        a[0][var]=t;
        for(int i=1;i<=19;i++)
        {
            scanf("%d",&a[i][var]);
        }
        int ans=gauss();
        printf("%d\n",ans);
    }
    return 0;
}






poj 3185 The Water Bowls(高斯消元)

标签:des   style   http   color   os   io   strong   for   ar   

原文地址:http://blog.csdn.net/knight_kaka/article/details/38797135

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