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;
}

最新文章

  1. 【原创】我所理解的自动更新-外网web服务器配置
  2. DBCC SHOW_STATISTICS 查看统计信息
  3. postgresql命令行
  4. Windows下MinGW编译Qt4
  5. lua序列化(支持循环引用)
  6. ListView Web 服务器控件概述(MSDN)
  7. 《Java数据结构与算法》笔记-CH1
  8. C#中2、8、16进制 有符号转换10进制正负数
  9. (转载)SVM-基础(三)
  10. Linux 安装 powershell
  11. Spring-WebSocket 教程
  12. 笔记本串口连接IBM 小机
  13. [No0000138]软件开发基础知识
  14. [Unity动画]06.子状态机
  15. 1.3.3、CDH 搭建Hadoop在安装之前(端口---CDH组件使用的端口)
  16. 转css中文英文换行、禁止换行、显示省略号
  17. &quot;名字好难想队“团队项目
  18. python基础——python解析yaml类型文件
  19. web页面中可以包含多个对象
  20. 数据库-Core Data

热门文章

  1. JS条件语句优化
  2. Java Knowledge series 4
  3. BizMDM企业主数据管理平台
  4. Azure进阶攻略 | 应用流畅运行杜绝超载,自有一套好方法
  5. java集合框架——工具类
  6. C#后台unxi时间戳转换为前台JS时间的方法
  7. for循环研究
  8. Edmonds-Karp算法,最大流POJ(1459)
  9. activity 工作流学习(一)
  10. Python 二进制 八进制 十进制 十六进制