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. (一)JavaScript之[数据类型]与[对象]

    1].数据类型字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空(Null).未定义 (Undefined). //极大或极小的数字可以 ...

  2. scss的使用方式(环境搭建)

    我用的是Koala. IDE是intellij_idea(其他IDE也可) 下载Koala:http://koala-app.com/ 2.安装(选好位置,下一步即可) 3.打开Koala,创建项目 ...

  3. Android开发基础

    一.Android开发环境搭建 1.下载安卓SDK 官方下载地址:http://developer.android.com/sdk/index.html 2.下载安装JDK 官方下载地址:JDK6 h ...

  4. 《深入理解Java 7核心技术与最佳实践》读书笔记(2) Java语言动态性引言

    Java语言是一种静态类型的编程语言.静态类型的含义是指在编译时进行类型检查.Java源代码中的每个变量的类型都要显式地进行声明.所有变量.方法的参数和方法返回值的类型在程序运行之前就必须是已知的.J ...

  5. UI自动化录制工具----UI Recorder

    1.系统和工具包 windows 7 64位 jdk,nodejs,webdriver,浏览器都放在工具包目录内.(晚上回去把文件上传到云盘,在分享给大家) 2.安装JDK和node.js 2.1 J ...

  6. 用户管理的设计--4.jquery的ajax实现登录名的校验

    页面效果 鼠标失去焦点时,不需要刷新页面进行校验,判断登录名是否重复. 实现步骤 1.引入struts2-json-plugin-2.5.10.1插件包 2.页面使用jquery的ajax实现后台校验 ...

  7. Html : 将submit变成像文字一样的按钮

    直接上代码: <html> <head> <title>像文字一样的按钮</title> <style> body{ background- ...

  8. 修改Linux中发送邮件中附件大小的限制

    方法一: 在命令中设定postfix的message_size_limit值 (但系统重启后会失效) postconf -e "message_size_limit = 20480000&q ...

  9. ubuntu16.4安装 VirtualBox

    1) 从oracle官网下载virtual box安装包 2) 安装支持包 sudo apt-get install libqt5x11extras5 libsdl1.2debian 3) sudo ...

  10. 同步软件UltraCompare 64位 软件及注册机

    软件及注册机下载: https://share.weiyun.com/f09e6243887e374ead1b3a3ab8f611a9 软件官方下载地址:  https://www.ultraedit ...