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]
设第i个bowl是否翻转为xi,xi为真表示翻转它一次,假表示不动。
原题是否某种状态转化为零向量,事实上等价于由零向量转化到该状态(y1 , y2 , …… , y20)
所以方程就是
x1 + x2 = y1
x1 + x2 + x3 = y2
x2 + x3 + x4 = y3
……
x18 + x19 + x20 = y19
x19 + x20 = y20
在将它建立方程组后,
它就转化为求解一个对2取模的01方程组,且找解中1的个数最少的一个。
在高斯消元的过程中将自由变元的个数k求出,再进行枚举自由变元(枚举 2 ^ k 次)求解高斯消元后的方程组,然后记录最少1的个数即可。
#include #include #include #include #include #include #include #include #define msc(X) memset(X,-1,sizeof(X))#define ms(X) memset(X,0,sizeof(X))typedeflonglong LL;
usingnamespacestd;
constint MAXN=25;
int g[MAXN][MAXN];
int x[MAXN];
int free_x[MAXN];
int free_num;
int Guass(void)
{
int max_r,col,k;
free_num=0;
for(k=col=0;k<20&&col<20;k++,col++)
{
max_r=k;
for(int i=k+1;i<20;i++)
if(abs(g[i][col])>abs(g[max_r][col]))
max_r=i;
if(g[max_r][col]==0){
k--;
free_x[free_num++]=col;
continue;
}
if(max_r!=k){
for(int j=col;j<21;j++)
swap(g[k][j],g[max_r][j]);
}
for(int i=k+1;i<20;i++)
if(g[i][col]){
for(int j=col;j<21;j++)
g[i][j]^=g[k][j];
}
}
for(int j=col;j<20;j++)
free_x[free_num++]=j;
for(int i=k;i<20;i++)
if(g[i][20]) return -1;
if(k<20) return20-k;
for(int i=19;i>=0;i--)
{
x[i]=g[i][20];
for(int j=i+1;j<20;j++)
x[i]^=(g[i][j]&&x[j]);
}
return0;
}
void solve(void)
{
int t=Guass();
if(t==0){
int ans=0;
for(int i=0;i<20;i++)
ans+=x[i];
printf("%d
",ans );
}
else {
int ans=0x3f3f3f3f;
int tot=(1<for(int i=0;iint cnt=0;
for(int j=0;jif(i&(1<1;
cnt++;
}
else x[free_x[j]]=0;
for(int j=19-t;j>=0;j--)
{
int idx;
for(idx=j;idx<20;idx++)
if(g[j][idx]) break;
x[idx]=g[j][20];
for(int l=idx+1;l<20;l++)
if(g[j][l])
x[idx]^=x[l];
cnt+=x[idx];
}
ans=min(cnt,ans);
}
printf("%d
",ans );
}
}
int main(int argc, charconst *argv[])
{
ms(g);
for(int i=0;i<20;i++)
{
g[i][i]=1;
if(i>0) g[i-1][i]=1;//注意不要写反if(i<19) g[i+1][i]=1;
}
for(int i=0;i<20;i++)
scanf("%d",&g[i][20]);
solve();
return0;
}