poj 3185

问题描述

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?

输入

Line 1: A single line with 20 space-separated integers

输出

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.

样例输入

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0

样例输出

3

提示

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的时候,你肯定要翻了,那么问题来了,你是翻前一个?还是这一个?还是后一个呢?
答案当然是后一个,因为你翻前一个或者这一个,你的前一个都会变。然后正着翻一次,倒着翻一次取最小值,因为要避免1 1 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0这种情况。
#include <iostream>
#include <cstdio>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;

int ma[21];
int a[21];

int main()
{
    for(int i=0;i<20;i++)
    {
        scanf("%d",&ma[i]);
    a[i]=ma[i];
    }
    int ans1=0,ans2=0;
        for(int i=0;i<19;i++)
        {
            if(a[i])
            {
                a[i+1]=!a[i+1];
                if(i!=18)
                a[i+2]=!a[i+2];
                ans1++;
            }
        }
        for(int i=19;i>=1;i--)
        {
            if(ma[i])
            {
                ma[i-1]=!ma[i-1];
                if(i!=1)
                    ma[i-2]=!ma[i-2];
                ans2++;
            }
        }
        printf("%d\n",min(ans1,ans2));

}


你可能感兴趣的:(poj 3185)