POJ 3185 The Water Bowls

来源:互联网 发布:旅游拼车软件 编辑:程序博客网 时间:2024/05/21 14:04
The Water Bowls
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3979 Accepted: 1564

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

 

【题目大意】:

        奶牛有20只碗摆成一排,用鼻子顶某只碗的话,包括左右两只在内的一共三只碗会反向,现在给出碗的初始状态,问至少要用鼻子顶多少次才能使所有碗都朝上。

【分析】:

        解亦或方程组的高斯消元法

【代码】:

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>#include<vector>#include<stack>#include<queue>using namespace std;#define MAXN 22#define IMAX 21474836int N,a[MAXN],ratio[MAXN][MAXN+1],ans[MAXN],ansreal=IMAX;void pre_mat(){      for(int i=1;i<=N;i++)      {            if(i!=1)   ratio[i][i-1]=1;            if(i!=N)   ratio[i][i+1]=1;            ratio[i][i]=1;      }}void swap_mat(int A,int B){      for(int i=1;i<=N+1;i++)            swap(ratio[A][i],ratio[B][i]);}void guess(int now){      if(now==N+1)            return;      for(int i=now+1;i<=N;i++)            if(ratio[i][now] && !ratio[now][now])            {                  swap_mat(now,i);                  break;            }      for(int i=now+1;i<=N;i++)            if(ratio[i][now])                  for(int j=now;j<=N+1;j++)                        ratio[i][j]^=ratio[now][j];      guess(now+1);}void solve(int now){      if(now==0)      {            int ansnow=0;            for(int i=1;i<=N;i++)                  ansnow+=ans[i];            ansreal=min(ansreal,ansnow);            return;      }      if(!ratio[now][now])      {            for(int i=0;i<=1;i++)            {                  ans[now]=i;                  for(int j=N;j>now;j--)                        ans[now]^=ratio[now][j]*ans[j];                  solve(now-1);            }      }      else       {            ans[now]=ratio[now][N+1];            for(int j=N;j>now;j--)                  ans[now]^=ratio[now][j]*ans[j];            solve(now-1);      }}int main(){      //freopen("input.in","r",stdin);  //freopen("output.out","w",stdout);      N=20;  for(int i=1;i<=N;i++)  {        scanf("%d",&a[i]);        ratio[i][N+1]=a[i];      }      pre_mat();      guess(1);      solve(N);      printf("%d\n",ansreal);  //system("pause");      return 0;}


 

转载注明出处:http://blog.csdn.net/u011400953

 

0 0