POJ3185 The water bowls - 模拟

假设一个方块是1, 为了使其变为0而不使已经变白的序列受到影响,只能反转其后面的方块
所以这个序列的第一个数是1还是0就决定了整个序列的反转,但是从左边开始翻和从右边开始翻是不一样的(感觉是这样就行,不需要证明),所以我们要扫两遍这个序列

#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = 25;
int a[MAXN],b[MAXN],ans,ans1;
int main() {
    for(int i=1; i<=20; i++)
        cin >> a[i], b[i] = a[i];
    for(int i=2; i<=20; i++) {
        if(a[i-1]) {
            a[i-1] = !a[i-1];
            a[i] = !a[i];
            a[i+1] = !a[i+1];
            ans++;
        }
    }
    for(int i=19; i>=1; i--) {
        if(b[i+1]) {
            b[i] = !b[i];
            b[i-1] = !b[i-1];
            ans1++;
        }
    }
    cout << min(ans, ans1);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/fantasy_world/article/details/79589380