The Water Bowls POJ - 3185(开关问题+暴力)

这篇博客探讨了一种数学问题,即如何通过最少的翻转次数将一排20个水碗全部翻成正面朝上的状态。每个操作可以翻转一个碗及其两侧的两个碗(对于边缘的碗则只翻转自身和一个相邻的碗)。文章提供了样例输入和输出,并指出了解决方案可能涉及的算法,如暴力搜索和构造增广矩阵进行高斯消元。给出的AC代码采用了一种从两端同时进行翻转的简单策略来求解最小翻转次数。
摘要由CSDN通过智能技术生成

题意:

给20个水碗。朝上为‘0’或朝下为‘1’,每次操作使三个碗翻转,问使所有20个水碗都朝上,至少翻多少次?

题目:

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]

分析:

这个题直接暴力过得,直接正反遍历,找最小值,还有一种构造增广矩阵类似于根据开关的关系构造有向图的邻接矩阵;构造增广矩阵,高斯消元,枚举自由元(二进制枚举状态),寻找最小值的方法,因为时间关系就没有去钻研,在这提供下思路。

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[30],b[30];
int ans,num;
int main(){
    for(int i=1;i<=20;i++){
        scanf("%d",&a[i]);
        b[i]=a[i];
    }
    num=ans=0;
    for(int i=1;i<=20;i++){
        if(a[i]==1){
            ans++;
            a[i+1]=!a[i+1];
            a[i+2]=!a[i+2];
        }
    }
     for(int i=20;i>=1;i--){
        if(b[i]==1){
            num++;
            b[i-1]=!b[i-1];
            b[i-2]=!b[i-2];
        }
    }
    ans=min(ans,num);
    printf("%d\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值