POJ 3185 The Water Bowls(反转)

本文介绍了一种解决翻转水碗问题的算法,旨在通过最少次数的操作将所有水碗翻转为正确方向。该问题设定在一个包含20个水碗的线上,每个水碗可以处于两种状态之一:适合饮用或不适合饮用。算法的目标是最小化翻转次数,确保所有水碗都适合饮用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Water Bowls
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions:7270 Accepted: 2880

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]


当b[0]确定时,最后结果便唯一确定 。枚举b[0]的
两种情况,找出最小值。
代码

#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
int PP(int *a,int *b)
{
    int ans=0,c[22];
    c[0]=a[0]+b[0];
    for(int i=1;i<20;i++)
    {
        if(c[i-1]&1)b[i]++,ans++;
        c[i]=a[i]+b[i]+b[i-1];
    }
    if(c[19]&1)return 1e9+7;
    return ans;
}
int main()
{
    int i,j,a[22],b[22];
    for(i=0;i<20;i++)
        scanf("%d",&a[i]);
    memset(b,0,sizeof(b));
    int ans=PP(a,b);
    memset(b,0,sizeof(b));
    b[0]=1;
    ans=min(ans,PP(a,b)+1);//b[0]变为1算是一次操作
    printf("%d\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值