题目描述:牛喝水 

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?

输入

Line 1: A single line with 20 space-separated integers

输出

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.

样例输入

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0

样例输出

3

思路:就像是背包问题或者是开关灯问题,每到一步有放和不放/开不开 两种状态,每种状态做尝试,遍历搜索即可。
备注:之前尝试DFS模拟,但是反转的情况考虑不尽,所以直接用每步判断比较省事。
// 牛喝水.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <iostream>
using namespace std; const int MAX = ; int n = , ans, flag, arr[MAX]; int check()
{
for (int i = ; i < n; i++)
{
if (arr[i] == )
return ;
}
return ;
} void printa()
{
for (int i = ; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
} void change(int pos)
{
arr[pos] = !arr[pos];
if (pos - >= ) arr[pos - ] = !arr[pos - ];
if (pos + <= n) arr[pos + ] = !arr[pos + ];
} //也许是反转的情况考虑少了。。。。。
//void DFS(int a[])
//{
// printa();
//
// if (is(a) == 1) return;
//
// for (int i = 0; i < n; i++)
// {
// if (a[i] == 1)
// {
// //cout << "i:" << i << "\ta[i]:" << a[i] << endl;
// if ((i+1) < n && a[i + 1] == 1)
// {
// a[i] = change(a[i]);
// a[i + 1] = change(a[i + 1]);
// if (i + 2 < n) a[i + 2] = change(a[i + 2]);
// }
// else
// {
// a[i] = change(a[i]);
// if (i - 1 >= 0) a[i - 1] = change(a[i - 1]);
// if (i + 1 < n) a[i + 1] = change(a[i + 1]);
// }
// num++;
// break;
//
// }
//
// }
// DFS(a);
//} void DFS(int pos,int sum, int start)
{
//cout << "pos:" << pos << "\tsum" << sum << "\tstart:" << start << endl;
if (flag) return;
if (sum == start) { flag = check(); ans = sum; return; } if (n - pos + < start - sum) return; change(pos);
DFS(pos + , sum + ,start); change(pos);
DFS(pos + , sum, start);
} int main()
{
for (int i = ; i < n; i++) cin >> arr[i]; flag = ;
ans = -;
for (int i = ; i < n; i++)
{
DFS(, , i);
if (flag)
{
cout << ans << endl;
break;
}
}
if (flag == ) cout << "" << endl; return ;
}

ACM-牛喝水的更多相关文章

  1. [Swust OJ 781]--牛喝水

    Time limit(ms): 1000 Memory limit(kb): 65535   The cows have a line of 20 water bowls from which the ...

  2. FZYZOJ-1569 喝水

    P1569 -- 喝水 时间限制:2000MS      内存限制:131072KB 状态:Accepted      标签:    无   无   无 Description GH的N个妹子要喝水, ...

  3. [BZOJ 3441]乌鸦喝水

    3441: 乌鸦喝水 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 374  Solved: 148[Submit][Status][Discuss] ...

  4. Bzoj3441 乌鸦喝水

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 258  Solved: 97 Description [题目背景]     一只乌鸦在自娱自乐,它在面 ...

  5. ACM牛人博客

    ACM牛人博客 kuangbin kuangbin(新) wuyiqi wuyiqi(新) ACM!荣耀之路! 九野的博客 传说中的ACM大牛!!! read more

  6. BZOJ:3441 乌鸦喝水

    bzoj:3441 乌鸦喝水 题目传送门 Description 一只乌鸦在自娱自乐,它在面前放了n个有魔力的水缸,水缸里装有无限的水. 他准备从第1个水缸飞到第n个水缸,共m次.在飞过一个水缸的过程 ...

  7. 8.18 NOIP模拟测试25(B) 字符串+乌鸦喝水+所驼门王的宝藏

    T1 字符串 卡特兰数 设1为向(1,1)走,0为向(1,-1)走,限制就是不能超过$y=0$这条线,题意转化为从(0,0)出发,走到(n+m,n-m)且不越过$y=0$,然后就裸的卡特兰数,$ans ...

  8. 推荐一款健康App 多喝水,引领全民时尚喝水生活习惯

    推荐一款健康App 多喝水,引领全民时尚喝水生活习惯 1 介绍 多喝水,一款鼓励大众喝水的APP.我们倡导大众健康生活,培养人们爱喝水的习惯,让每一次喝水,都能产生价值,让人们在喝水的同时,可享受赚钱 ...

  9. 通过喝水清晰简单了解I/O五大模型

    一般单次I/O请求会分为两个阶段,每个阶段对于I/O的处理方式是不同的 I/O会经历一个等待资源的阶段 阻塞,指的是在数据不可用时,I/O请求会一直阻塞,直到数据返回 数据不可用时,立即返回,直到被通 ...

随机推荐

  1. 如何对接网建SMS短信通短信验证码接口

    1首先注册登录网建SMS网站  http://www.smschinese.cn/ 2.下载Java代码 3.JAVA调用 import java.io.UnsupportedEncodingExce ...

  2. 安装redis集群

    1.搭建集群需要使用到官方提供的ruby脚本. 需要安装ruby的环境. 安装ruby yum install ruby yum install rubygems 2.将ruby包redis-3.0. ...

  3. 使用SpringMVC时,web应用的资源路径问题

    web应用常见的资源存方式 在WEB-INF下新建lib,存放要使用的jar包 在WEB-INF下新建jsp文件夹,存放jsp文件.首页除外,首页就放到web下. 在web下,或WEB-INF下,新建 ...

  4. Lesson 2 Spare that spider

    How much of each year do spiders killing insects? Why, you may wonder, should spiders be our friends ...

  5. Docker registry自签名证书

    权威Registry 获取安全证书有两个办法:互联网认证的CA处获取.自建CA自己给自己签名. 1.从认证CA处获取签名证书,大多数是需要付出一定费用的,近些年也有认证CA提供免费证书,例如Let’s ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:按钮组

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. Python学习笔记之基础篇(二)python入门

    一.pycharm 的下载与安装: 使用教程:https://www.cnblogs.com/jin-xin/articles/9811379.html 破解的方法:http://xianchang. ...

  8. Golang:GOROOT、GOPATH、GOBIN变量的含义

    背景 移植完了go以后,在配置变量的时候不太清楚这些变量的含义,找了有关的资料. 使用 go env可以获取 go 有关的环境变量,下面是我的go环境: GO111MODULE="" ...

  9. ubuntu18.04下neo4j的安装

    参考CSDN博客 安装jdk8方式与博客中有不同,按照博客中方法没有成功 以下方法配置环境变量成功 进入配置文件 [root@cuierdan java]# vim /etc/profile在文件的后 ...

  10. 洛谷 P2634 聪聪可可

    题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已 ...