ACM-牛喝水
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-牛喝水的更多相关文章
- [Swust OJ 781]--牛喝水
Time limit(ms): 1000 Memory limit(kb): 65535 The cows have a line of 20 water bowls from which the ...
- FZYZOJ-1569 喝水
P1569 -- 喝水 时间限制:2000MS 内存限制:131072KB 状态:Accepted 标签: 无 无 无 Description GH的N个妹子要喝水, ...
- [BZOJ 3441]乌鸦喝水
3441: 乌鸦喝水 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 374 Solved: 148[Submit][Status][Discuss] ...
- Bzoj3441 乌鸦喝水
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 258 Solved: 97 Description [题目背景] 一只乌鸦在自娱自乐,它在面 ...
- ACM牛人博客
ACM牛人博客 kuangbin kuangbin(新) wuyiqi wuyiqi(新) ACM!荣耀之路! 九野的博客 传说中的ACM大牛!!! read more
- BZOJ:3441 乌鸦喝水
bzoj:3441 乌鸦喝水 题目传送门 Description 一只乌鸦在自娱自乐,它在面前放了n个有魔力的水缸,水缸里装有无限的水. 他准备从第1个水缸飞到第n个水缸,共m次.在飞过一个水缸的过程 ...
- 8.18 NOIP模拟测试25(B) 字符串+乌鸦喝水+所驼门王的宝藏
T1 字符串 卡特兰数 设1为向(1,1)走,0为向(1,-1)走,限制就是不能超过$y=0$这条线,题意转化为从(0,0)出发,走到(n+m,n-m)且不越过$y=0$,然后就裸的卡特兰数,$ans ...
- 推荐一款健康App 多喝水,引领全民时尚喝水生活习惯
推荐一款健康App 多喝水,引领全民时尚喝水生活习惯 1 介绍 多喝水,一款鼓励大众喝水的APP.我们倡导大众健康生活,培养人们爱喝水的习惯,让每一次喝水,都能产生价值,让人们在喝水的同时,可享受赚钱 ...
- 通过喝水清晰简单了解I/O五大模型
一般单次I/O请求会分为两个阶段,每个阶段对于I/O的处理方式是不同的 I/O会经历一个等待资源的阶段 阻塞,指的是在数据不可用时,I/O请求会一直阻塞,直到数据返回 数据不可用时,立即返回,直到被通 ...
随机推荐
- Vue下URL地址栏参数改变却不能刷新界面
在完成毕业设计(基于Vue的信息资讯展示与管理平台)的过程中,处理如下图所示的 点击左侧栏目列表跳转到对应文章列表 的问题时,初次点击可以跳转到对应的页面,但是当第二次点击时,虽然地址栏的参数改变了, ...
- 【转载】如何快速转载CSDN中的博客
前言 对于喜欢逛CSDN的人来说,看别人的博客确实能够对自己有不小的提高,有时候看到特别好的博客想转载下载,但是不能一个字一个字的敲了,这时候我们就想快速转载别人的博客,把别人的博客移到自己的空间 ...
- P1250 种树 题解
题目描述 一条街道的一边有几座房子,因为环保原因居民想要在路边种些树,路边的居民被分割成 n 块,并被编号为 1…n.每块大小为一个单位尺寸并最多可种一棵树.每个居民想在门前种些树并指定了三个数b,e ...
- 如何知道某个ACTIVITY是否在前台?
本文链接:http://zengrong.net/post/1680.htm 有一个Android应用包含包含一个后台程序,该程序会定期连接服务器来实现自定义信息的推送.但是,当这个应用处于前台的时候 ...
- rem布局js脚本代码
目前代码在750屏幕分辨率下是十倍 基本上使用iphone是375宽度 所以就是20倍 图片背景可以使用二倍图 (function (doc, win) { var docEl = doc.docum ...
- sklearn中的多项式回归算法
sklearn中的多项式回归算法 1.多项式回归法多项式回归的思路和线性回归的思路以及优化算法是一致的,它是在线性回归的基础上在原来的数据集维度特征上增加一些另外的多项式特征,使得原始数据集的维度增加 ...
- node批量修改文件名称
let fs = require('fs');//引用文件系统模块 let PATH = `./app_zijietiaodong/`;//当前文件夹 let ext = { readFileList ...
- HiBench成长笔记——(7) 阅读《The HiBench Benchmark Suite: Characterization of the MapReduce-Based Data Analysis》
<The HiBench Benchmark Suite: Characterization of the MapReduce-Based Data Analysis>内容精选 We th ...
- f_lseek
我在STM32中移植了fatfs文件系统,实现在SD卡对文件的读写.在普通读写中都没有问题,但是一旦我关闭文件系统,再次打开读写,之前写的数据就被覆盖.比如举个例子: u8 tx_buff ...
- 通过虚拟机增加Linux的磁盘(分区容量)
因为安装oracle设置的磁盘空间不足,所以安装失败.这里总结一下如何添加磁盘挂载 1. 右键虚拟机点击设置,然后点击磁盘,点击添加按钮 2.然后点击下一步下一步,直到安装成功 3.然后输入 fdis ...