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请求会一直阻塞,直到数据返回 数据不可用时,立即返回,直到被通 ...
随机推荐
- Mp3下载
- insert和delete底层实现的方式
operator delete ()全局函数原型: /*operator delete: 该函数最终是通过free来释放空间的*/void operator delete(void *pUserDat ...
- 第3节 storm高级应用:2、storm与hdfs的整合工程环境准备;3、整合代码开发
======================================== 3. storm与hdfs的整合使用 3.1.功能需求: 实现随机发送订单数据,从计算订单的总金额,然后将订单中的数 ...
- linux 串口 拼帧处理
串口每次read数据可能不是完整的数据,参照网上的代码,写了拼帧的代码#include <stdio.h> #include <termios.h> #include < ...
- boost::property_tree 读取ini配置
应用场景: 在后端服务器项目开发中,需要初始化一个Socket服务器,需要IP地址与对应端口号等参数:另外还可能因为对接数据库,就还需要数据库的相关配置参数,如我使用的是MySql数据库,就需要数据库 ...
- js 保留两位小数 input要求是数字框,
要求:input文本框只能输入数字,且只保留两位小数 问题:若设置input的 type="number" ,js处理部分若用到parseFloat方法处理,结果是string类 ...
- django+celery+ RabbitMQ实现异步任务实例
背景 django要是针对上传文件等需要异步操作的场景时,celery是一个非常不错的选择.笔者的项目就是使用了这个组合,这里就做一个备忘吧. 安装RabbitMQ 这个安装及使用我已经在前一 ...
- 嵊州普及Day4T1
题意:从n个数中选出k个数,使他们任意两数之差都等于m. 思路:任意差值都等于m,不就等价于k个数模m余数相同吗? 然后桶排储蓄一下各数余数即可. 见代码: #include<iostream& ...
- NFS实战
博客实践: (1) nfs server导出/data/application/web,在目录中提供wordpress; (2) nfs client挂载nfs server导出的文件系统至/var/ ...
- mac下添加环境变量
1.环境变量相关文件说明: a. /etc/profile b. /etc/paths c. ~/.bash_profile d. ~/.bash_login e. ~/.profile f. ~/. ...