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请求会一直阻塞,直到数据返回 数据不可用时,立即返回,直到被通 ...
随机推荐
- ionic3记录之栅格布局使文字居中
{ display:flex; align-items:center; } 未完待续...
- Eclipse - 常见问题 - Refresh
有时候项目代码正确但运行后出现异常,是因为eclipse没有刷新 (如jar包添加了但没用),比较脑慢. 解决方法: clean缓存,或者要多点几次Refresh,或者重启 eclipse.
- PHP获取远程图片
<?php // // Function: 获取远程图片并把它保存到本地 // // // 确定您有把文件写入本地服务器的权限 // // // 变量说明: // $url 是远程图片的完整UR ...
- Java注解浅谈
注解定义(来自百度百科):指示编译器如何对待您的自定义 Annotation,预设上编译器会将Annotation资讯留在class档案中,但不被虚拟机器读取,而仅用于编译器或工具程式运行时提供资讯. ...
- (转)C#的 GC工作原理基础
作为一位C++出身的C#程序员,我最初对垃圾收集(GC)抱有怀疑态度,怀疑它是否能够稳定高效的运作:而到了现在,我自己不得不说我已经逐渐习惯并依赖GC与我的程序“共同奔跑”了,对“delete”这个习 ...
- 留学生想要搞定Reading List?只需这三步即可
听到有同学在抱怨“一本书都读不完,还怎么搞定reading list啊?”别急,小编这就来给你支招啦!你的文献阅读方法错了,读起来不仅效率低,而且无法做到熟练运用.因此,你需要以下这3步,就能搞定文献 ...
- 在Anaconda3环境下安装并切换 Tensorflow 2.0 环境
背景 Anaconda切换各种环境非常方便,现在我们就来介绍一下如何使用anaconda安装tensorflow环境. anaconda v3.5 from 清华镜像站 tensorflow v2.0 ...
- Codeforces 1294D - MEX maximizing
思维,真的很巧妙啊,看了以下博客 https://www.cnblogs.com/stelayuri/p/12230033.html
- yum出现Error downloading packages错误
yum出现Error downloading packages错误错误表现方式:yum可以list,可以clean cache,但是无法安装,错误提示: Downloading packages: E ...
- python merge、join、concat用法与区别
由于合并变化较大,以后函数可能会修改,只给出一些例子作为参考 总结: merge.join 1.当没有索引时:merge.join为按照一定条件合并 2.当有索引.并按照索引合并时,得到结果为两者混 ...