poj 3185 The Water Bowls (高斯消元法)

    xiaoxiao2021-03-25  122

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

    题意:枚举+高斯消元法;

     与poj1753相似;

    代码:

    #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; const int N=30; int a[N][N]; int equ=20,var=20; int map[N]; int index_x[N],x[N]; void init() { for(int i=0;i<20;i++) { a[i][i]=1; if(i>0) a[i-1][i]=1; if(i<19) a[i+1][i]=1; } } int Gauss() { int col,k; memset(index_x,0,sizeof(index_x)); memset(x,0,sizeof(x)); int num=0; for(k=0,col=0;k<equ&&col<var;k++,col++) { int 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(k!=max_r) for(int j=col;j<=var;j++) swap(a[k][j],a[max_r][j]); if(a[k][col]==0) { index_x[num++]=col; k--;continue; } for(int i=k+1;i<equ;i++) { if(a[i][col]!=0) for(int j=col;j<=var;j++) a[i][j]^=a[k][j]; } } int maxn=1<<(var-k); int ans=0x3f3f3f3f; for(int i=0;i<maxn;i++) { int cnt=0; int temp=i; for(int j=0;j<var-k;j++) { x[index_x[j]]=temp&1; if(x[index_x[j]]) cnt++; temp>>=1; } for(int j=k-1;j>=0;j--) { int l=0; while(a[j][l]==0) l++; x[l]=a[j][var]; for(int t=l+1;t<var;t++) x[l]^=(a[j][t]&&x[t]); if(x[l]) cnt++; } if(cnt<ans) ans=cnt; } return ans; } int main() { while(~scanf("%d",&map[0])) { for(int i=1;i<20;i++) scanf("%d",&map[i]); memset(a,0,sizeof(a)); for(int i=0;i<20;i++) a[i][20]=map[i]; init(); int ans=Gauss(); printf("%d\n",ans); } }

    转载请注明原文地址: https://ju.6miu.com/read-6964.html

    最新回复(0)