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]

        这题属于反转(开关)类型的题,典型的特征就是:
1,反转的先后顺序是不重要的。
2。主动对一个开关进行2次或2次以上的反转是多余的。

        这题。假设条件是每次必须反转3个碗的话,那么就非常easy,先考虑最左端的碗,假设碗朝下,那么这个碗必须反转,同一时候带动后面两个碗一起反转,这种话问题的规模就降低了一个,然后反复此方法推断。
        可是条件是在两端能够出现同一时候仅仅反转两个碗的情况,这时候仅仅要先枚举一下两端反转两个碗的全部情况,然后就能够把它当成每次必须反转3个碗进行处理就能够了。
#include <stdio.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <iostream>
#include <queue>
#include <list>
#include <algorithm>
#include <stack>
#include <map> using namespace std; int main()
{
#ifdef _DEBUG
freopen("e:\\in.txt", "r", stdin);
#endif
int side[21];
int side1[21];
for (int i = 0; i < 20; i ++)
{
scanf("%d", &side[i]);
}
int minCount = 50;
for (int i = 0; i < 4;i++)
{
int count1 = 0;
memcpy(side1, side, sizeof(side));
if (i == 1)
{
side1[0]++;
side1[1]++;
count1++;
}
else if (i == 2)
{
side1[18]++;
side1[19]++;
count1++;
}
else if (i == 3)
{
side1[0]++;
side1[1]++;
side1[18]++;
side1[19]++;
count1++;
count1++;
}
for (int i = 0; i <= 17; i++)
{
if (side1[i] & 1)
{
side1[i] ++;
side1[i + 1]++;
side1[i + 2]++;
count1++;
}
}
if (!(side1[1] & 1 || side1[18] & 1 || side1[19] & 1))
{
if (minCount > count1)
{
minCount = count1;
}
}
}
printf("%d\n", minCount);
return 1;
}


POJ3185 The Water Bowls 反转(开关)的更多相关文章

  1. POJ3185 The Water Bowls(反转法or dfs 爆搜)

    POJ3185 The Water Bowls 题目大意: 奶牛有20只碗摆成一排,用鼻子顶某只碗的话,包括左右两只在内的一共三只碗会反向,现在给出碗的初始状态,问至少要用鼻子顶多少次才能使所有碗都朝 ...

  2. [Gauss]POJ3185 The Water Bowls

    题意:反正就是要给的一串01的变成全0 能影响自己和左右 最少需要几步 01方程组 异或解 ][]; // 增广矩阵 ]; // 解 ]; // 标记是否为自由未知量 int n; void debu ...

  3. poj 3185 The Water Bowls(反转)

    Description The cows have a line of water bowls water bowls to be right-side-up and thus use their w ...

  4. POJ 3185 The Water Bowls 【一维开关问题 高斯消元】

    任意门:http://poj.org/problem?id=3185 The Water Bowls Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. POJ:3185-The Water Bowls(枚举反转)

    The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7402 Accepted: 2927 Descr ...

  6. poj 3185 The Water Bowls

    The Water Bowls 题意:给定20个01串(最终的状态),每个点变化时会影响左右点,问最终是20个0所需最少操作数? 水题..直接修改增广矩阵即可:看来最优解不是用高斯消元(若是有Gaus ...

  7. The Water Bowls [POJ3185] [开关问题]

    题意 一串长度为20的0,1数列,每次翻转i,会影响i-1,i+1,也被翻转,最少翻转成0的步骤数是多少? Sample Input 0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 ...

  8. Greedy:The Water Bowls(POJ 3185)

    水池 题目大意:给定一个20的数组,全都是0和1,可以翻一个数改变成另一个数(0或者1),但是其左右两边的数都会跟着变为原来的相反数,问你怎么用最小的操作数使全部数变成0 这一题的:满足 1:翻转次序 ...

  9. 挑战程序竞赛 反转开关 poj3276

    这个我其实也没有看太懂它的证明过程. 1.若某一个位置被翻转了n次,则其实际上被翻转了n%2次. 2.分析易知翻转的顺序并不影响最终结果. 3.现在我们着眼于第1个位置,可知若要将第1个位置进行翻转只 ...

随机推荐

  1. nodejs __dirname 与 process.cwd()的区别

    var cwd = process.cwd(); console.log(cwd); console.log(__dirname); 1 2 3 cwd() 是当前执行node命令时候的文件夹地址 _ ...

  2. Redis学习笔记(三)-数据类型之string类型

    string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象.从内部实现来看其实string可以看作byte ...

  3. AI:狄拉克之海上的涟漪

    延陵季子2011年  8月27日 19:02   借鉴英文原文:Ripples in the Dirac Sea 当他试着用一种轻松的口吻诉说一些事情时,我会明白,其实我们都明白,在他的心里绝对不是平 ...

  4. SLAM:飞行机器人的参数解析-分类

    在水电站存在的山区,公路运输效率极低,盘山公路绕行消耗大量时间,使用飞行机器人进行运输是合适的选择. 实现一位长辈在山区飞行的愿望,任重而道远 常见飞行机器人的参数解析:解读飞行机器人的基本类型及技术 ...

  5. Maya API编程快速入门

    一.Maya API编程简介 Autodesk® Maya® is an open product. This means that anyone outside of Autodesk can ch ...

  6. LOJ #6041. 「雅礼集训 2017 Day7」事情的相似度 LCT+SAM+线段树

    Code: #include<bits/stdc++.h> #define maxn 200003 using namespace std; void setIO(string s) { ...

  7. scrapy获取重定向之前的url

    通过 response.request.meta['redirect_urls'] 来获取跳转之前的链接

  8. HDU 2268 How To Use The Car (数学题)

    题目 //做起来很艰辛,总结起来很简单... //注意步行速度可能比车的速度快.... //推公式要仔细,,,, //一道对我来说很搞脑子的数学题,,,,, //车先送第一个人上路,第二个人步行:中途 ...

  9. POJ3253 Fence Repair【贪心】

    我们的小伙伴Bingo真的很调皮,他在上课的路上看到树上有个鸟窝,他就想去把他捅下来,但是鸟窝很高他够不到,于是他就到处找木棍,想把这些木棍接在一起,然后去捅鸟窝.他一共找了N跟木棍 (1 ≤ N ≤ ...

  10. PAT 1075. PAT Judge

    The ranklist of PAT is generated from the status list, which shows the scores of the submittions. Th ...