POJ 3185 The Water Bowls

來源:互聯網
上載者:User

標籤:des   style   http   color   os   strong   


高斯消元+枚舉自由變元


The Water Bowls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4276   Accepted: 1678

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

USACO 2006 January Bronze

[Submit]   [Go Back]   [Status]   [Discuss]



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int maxn=30;int a[maxn][maxn],x[maxn],ta[maxn];int val0,mim;void dfs(int v){    if(v==20)    {        for(int i=0;i<20;i++)            x[i]=ta[i];        for(int i=val0-1;i>=0;i--)        {            x[i]=a[i][20];            for(int j=i+1;j<20;j++)                x[i]^=(a[i][j]&&x[j]);        }        int temp=0;        for(int i=0;i<20;i++)            if(x[i]) temp++;        mim=min(mim,temp);        return ;    }    ta[v]=0;    dfs(v+1);    ta[v]=1;    dfs(v+1);}void Guass(){    int i,j;    for(i=0,j=0;i<20&&j<20;j++)    {        int k=i;        for(;k<20;k++)            if(a[k][j]) break;        if(a[k][j])        {            if(k!=i)            {                for(int r=0;r<=20;r++)                    swap(a[k][r],a[i][r]);            }            for(int r=0;r<20;r++)            {                if(r!=i&&a[r][j])                {                    for(int c=j;c<=20;c++)                        a[r][c]^=a[i][c];                }            }            i++;        }    }    if(i==20)///無自由變元    {        int ans=0;        for(int k=0;k<20;k++)            if(a[k][20]) ans++;        printf("%d\n",ans);    }    else    {        val0=i; mim=100;        dfs(val0);        printf("%d\n",mim);    }}int main(){    for(int i=0;i<20;i++)    {        int x;        scanf("%d",&x);        a[i][20]=x;        a[i][i]=1;        if(i-1>=0) a[i-1][i]=1;        if(i+1<20) a[i+1][i]=1;    }    Guass();    return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.