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. AJAX跨域POST发送json时,会先发送一个OPTIONS预请求

    我们会发现,在很多post,put,delete等请求之前,会有一次options请求. 根本原因就是,W3C规范这样要求了!在跨域请求中,分为简单请求(get和部分post,post时content ...

  2. Unable to copy a file from obj\Debug to bin\Debug

    1. Exit the VS2012, and then re-open the solution. 2. Clean the solution and build.

  3. linux下使用iperf测试服务器带宽

    准备工具 1.2台Linux服务器(要求其中至少1台主机为腾讯云主机,另外一台任意主机均可,确保2台主机可以互相访问即可)2.Iperf软件为专业网络性能测试工具. 测试目标 上海地区主机外网带宽是否 ...

  4. ASP.NET Core - 初期准备

    微软在前不久发布了.NET Core1.0(以下简称Core),由于项目需要开始对其进行研究,希望将自己踩过的坑和见解分享给大家. Core和Framework4.6是属于并行产品,前者侧重于跨平台的 ...

  5. S/4HANA for Customer Management里的搜索分页处理

    这篇文章的英文版我发在了SAP Community上:Paging Implementation in S/4HANA for Customer Management https://blogs.sa ...

  6. [转载]Memcached缓存服务的简单安装

    1.Linux下的安装方法 下载:wget http://memcached.org/latest tar -zxvf memcached-1.x.x.tar.gz cd memcached-1.x. ...

  7. C# windows 计划任务 程序编写

    编写windows 计划任务只需要在普通的类里面使用main方法就好了,因为任务计划在创建后走的是程序的主方法,代码如下: using System; using System.Collections ...

  8. POJ-3190 Stall Reservations---优先队列+贪心

    题目链接: https://vjudge.net/problem/POJ-3190 题目大意: 有N头奶牛,每头奶牛都会在[1,1000000]的时间区间内的子区间进行挤奶.挤奶的时候奶牛一定要单独放 ...

  9. python_4_interaction

    #1(方法1)尽量不用这种拼接法,效率低下,占用内存多 name=input("name:") age=input('age:') job=input('job:') salary ...

  10. R 语言学习日志 1

      1. CSV文件的的读取与写出 2. 数据集筛选 3. 简单随机抽样 sample函数   正文: 1. CSV文件的的读取与写出 文件读取: df2 <- read.table(" ...