poj 3185 The Water Bowls(高斯消元 开关问题)

Language:
The Water Bowls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4836   Accepted: 1898

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]



20个碗排成一排 有0 1两种状态 一个碗翻转 它旁边的两个碗的状态也会变化 

现在需要通过最少的翻转次数 使得所有碗的状态都变为0

一维的情况 处理更简单了。。。

<span style="font-family:KaiTi_GB2312;font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 30
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

int equ,var;//equ个方程 var个变元 增广矩阵行数为equ 列数为var+1 分别为0到var
int a[MAXN][MAXN];//增广矩阵
int x[MAXN];//解集
int free_x[MAXN];//存储自由变元(多解枚举自由变元可以使用)
int free_num;//自由变元的个数
int n;

void init()
{
    MEM(a,0); MEM(x,0);
    equ=n; var=n;
    for(int i=0;i<n;i++)
    {
        a[i][i]=1;
        if(i>0) a[i-1][i]=1;
        if(i<n-1) a[i+1][i]=1;
    }
}
//返回值为-1表示无解 为0是唯一解 否则返回自由变元个数
int Gauss()
{
    int max_r,col,k;
    free_num=0;
    for(k=0,col=0;k<equ&&col<var;k++,col++)
    {
        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(a[max_r][col]==0)
        {
            k--;
            free_x[free_num++]=col;//这个是自由变元
            continue;
        }
        if(max_r!=k)
        {
            for(int j=col;j<var+1;j++)
                swap(a[k][j],a[max_r][j]);
        }
        for(int i=k+1;i<equ;i++)
        {
            if(a[i][col]!=0)
            {
                for(int j=col;j<var+1;j++)
                    a[i][j]^=a[k][j];
            }
        }
    }
    for(int i=k;i<equ;i++)
        if(a[i][col]!=0)
            return -1;//无解
    if(k<var) return var-k;//自由变元个数
    //唯一解  回带
    for(int i=var-1;i>=0;i--)
    {
        x[i]=a[i][var];
        for(int j=i+1;j<var;j++)
            x[i]^=(a[i][j]&&x[j]);
    }
    return 0;
}

int solve()
{
    int t=Gauss();
    if(t==-1)
    {
        return INF;
    }
    else if(t==0)
    {
        int ans=0;
        for(int i=0;i<n*n;i++)
            ans+=x[i];
       return ans;
    }
    else
    {
        int ans=INF;
        int tot=(1<<t);
        for(int i=0;i<tot;i++)
        {
            int cnt=0;
            for(int j=0;j<t;j++)
            {
                if(i&(1<<j))
                {
                    x[free_x[j]]=1;
                    cnt++;
                }
                else x[free_x[j]]=0;
            }
            for(int j=var-t-1;j>=0;j--)
            {
                int idx;
                for(idx=j;idx<var;idx++)
                    if(a[j][idx])
                        break;
                x[idx]=a[j][var];
                for(int l=idx+1;l<var;l++)
                    if(a[j][l])
                        x[idx]^=x[l];
                cnt+=x[idx];
            }
            ans=min(cnt,ans);
        }
        return ans;
    }
}

int c[25];

int main()
{
//    fread;
    while(scanf("%d",&c[0])!=EOF)
    {
        for(int i=1;i<20;i++)
            scanf("%d",&c[i]);
        n=20;
        init();
        for(int i=0;i<20;i++)
        {
            if(c[i])
                a[i][20]=1;
            else a[i][20]=0;
        }
        int ans=solve();
        printf("%d\n",ans);
    }
    return 0;
}

</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值