The Water Bowls

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 7402 Accepted: 2927

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. 有一行的碗,有的碗朝上有的碗朝下(1为上,0为下)。每次可以反转一只碗,如果反转一只碗,那么和这个碗相邻的碗也会反转,问最少反转多少次让所有的碗朝下。
  2. 想了很多种方法,最后还是枚举,总共二十只碗,有2^20次方中可能性,勉强可以接受。然后在枚举的过程中可以剪一下枝。

#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <climits>
using namespace std;
const int maxn = 25; int num[maxn]; int get(int x) {//较快的获得x的二进制中有多少个1
int cnt = 0;
while(x) {
x &= (x-1);
cnt++;
}
return cnt;
} bool check(int x) {//检查是否完全反转向下
int num2[maxn];
memcpy(num2,num,sizeof(num));
for(int i=0;i<20;i++) {
if(1&(x>>i)) {
num2[i]++;
if(i-1>=0)
num2[i-1]++;
num2[i+1]++;
}
}
for(int i=0;i<20;i++) {
if(num2[i]%2)
return true;
}
return false;
} int main() {
for(int i=0;i<20;i++)
scanf("%d",&num[i]);
int ans = INT_MAX;
for(int i=0;i<(1<<20);i++) {
int num_1 = get(i);
if(num_1 > ans)//如果现在枚举的数二进制中1的个数比当前答案还多可以直接剪去
continue;
if(check(i))
continue;
else
ans = min(ans,get(i));
}
printf("%d\n",ans);
return 0;
}

POJ:3185-The Water Bowls(枚举反转)的更多相关文章

  1. 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 ...

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

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

  3. poj 3185 The Water Bowls

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

  4. POJ 3185 The Water Bowls(高斯消元-枚举变元个数)

    题目链接:http://poj.org/problem?id=3185 题意:20盏灯排成一排.操作第i盏灯的时候,i-1和i+1盏灯的状态均会改变.给定初始状态,问最少操作多少盏灯使得所有灯的状态最 ...

  5. poj 3185 The Water Bowls 高斯消元枚举变元

    题目链接 给一行0 1 的数, 翻转一个就会使他以及它左右两边的都变, 求最少多少次可以变成全0. 模板题. #include <iostream> #include <vector ...

  6. POJ 3185 The Water Bowls (高斯消元)

    题目链接 题意:翻译过来就是20个0或1的开关,每次可以改变相邻三个的状态,问最小改变多少次使得所有开关都置为0,题目保证此题有解. 题解:因为一定有解,所以我们可以正序逆序遍历两次求出较小值即可.当 ...

  7. POJ 3185 The Water Bowls (高斯消元 求最小步数)

    题目链接 题意:有20个数字,0或1.如果改变一个数的状态,它左右两边的两个数的状态也会变反.问从目标状态到全0,至少需要多少次操作. 分析: 和上一题差不多,但是比上一题还简单,不多说了,但是在做题 ...

  8. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  9. POJ3185 The Water Bowls 反转(开关)

    Description The cows have a line of 20 water bowls from which they drink. The bowls can be either ri ...

随机推荐

  1. Spring文件下载

    package com.smbea.demo.controller; import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  2. #include stdio.h(4)

    #include <stdio.h> int main() { //****************1.数组*************** //什么是数组:专门用来存放数据的 /* 格式 ...

  3. 多路复用select poll epoll

    I/O 多路复用之select.poll.epoll详解 select,poll,epoll都是IO多路复用的机制.I/O多路复用就是通过一种机制,一个进程可以监视多个描述符,一旦某个描述符就绪(一般 ...

  4. Java实习问题记录

    1. $(window).height() 获取屏幕高度2. $("#chartbottomdiv").width() 某个控件的属性 用"."3. // 保留 ...

  5. C++指针、引用、const

    ; int *p = &a; //定义指针p指向变量a *p = ; //*p代表a的值 ; p = &b; //p指向变量b *p = ; //此时*p代表b的值 ] = {,,}; ...

  6. April 29 2017 Week 17 Saturday

    Every man is a poet when he is in love. 每个恋爱中的人都是诗人. It is said this saying was from Plato, the famo ...

  7. Javascript作业—取字符串的第一个只出现一次的字母

    js作业 取字符串第一个只出现一次的字母 <script type='text/javascript'> //取a-z字符串中第一个只出现一次的字母 function firstUniqu ...

  8. 微信小程序加载本地图片方法

    目录结构如下,只要图片按正确的方式放入小程序的开发工具的项目中,即可在wxml文件中用内联样式或者image标签都可以引用本地的图片. 步骤一:微信开发工具 打开项目 步骤二:新建个文件夹(放项目的一 ...

  9. 【CCPC-Wannafly Winter Camp Day4 (Div1) G】置置置换(动态规划)

    点此看题面 大致题意: 求出有多少个长度为\(n\)的排列满足对于奇数位\(a_{i-1}<a_i\),对于偶数位\(a_{i-1}>a_i\). 考虑打表? 考虑每次只有一个数\(n\) ...

  10. Java 截屏工具类

    PrintScreenUtils.java package javax.utils; import java.awt.AWTException; import java.awt.Dimension; ...