poj3185 The Water Bowls 高斯消元

来源:互联网 发布:迅捷网络fwr200 编辑:程序博客网 时间:2024/06/06 03:11

Language:
The Water Bowls
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4261 Accepted: 1671

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


思路:和poj1681一样,高斯消元,对于无穷解的情况,dfs得最优解。

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int MAXN=25;const int inf=0x3fffffff;int ans;int a[MAXN][MAXN],x[MAXN],flag[MAXN];void init(){    memset(a,0,sizeof(a));    memset(x,0,sizeof(x));    memset(flag,0,sizeof(flag));}void dfs(int k,int pos,int cnt,int var) //k是从下往上第k个阶梯行,pos为当前处理的变元,cnt为总数,var为变量数{    if(k<=-1)    {        if(cnt<ans) ans=cnt;        return ;    }    if(flag[pos])    {        x[pos]=a[k][var];        for(int j=pos+1;j<var;j++)            if(a[k][j]) x[pos]^=x[j];        if(x[pos])            dfs(k-1,pos-1,cnt+1,var);        else            dfs(k-1,pos-1,cnt,var);        return ;    }    x[pos]=0;  //枚举自由元    dfs(k,pos-1,cnt,var);    x[pos]=1;    dfs(k,pos-1,cnt+1,var);}int Gauss(int n,int m){    int i,col=0;    for(i=0;i<n && col<m;i++,col++)    {        int r=i;        for(int j=i+1;j<n;j++)            if(abs(a[j][col])>abs(a[r][col])) r=j;        if(r!=i)            for(int j=col;j<=m;j++) swap(a[i][j],a[r][j]);        if(!a[i][col])        {            i--;            continue;        }        for(int k=i+1;k<n;k++)            if(a[k][col])                for(int j=col;j<=m;j++)                    a[k][j]^=a[i][j];    }    for(int k=0;k<i;k++)        for(int j=k;j<m;j++)            if(a[k][j])            {                flag[j]=1;                break;            }    ans=inf;    dfs(i-1,m-1,0,m);    return ans;}int main(){    //freopen("text.txt","r",stdin);    init();    int n=20;    for(int i=0;i<n;i++)    {        a[i][i]=1;        if(i>0)            a[i][i-1]=1;        if(i+1<n)            a[i][i+1]=1;        scanf("%d",&a[i][n]);    }    ans=Gauss(n,n);    printf("%d\n",ans);    return 0;}

0 0
原创粉丝点击