[Swust OJ 781]--牛喝水
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.
|
1
|
0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0
|
|
1
|
3
|
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代表需要翻转的碗,问最少可以几次把碗全部翻转(0表示)
这道题呐,用dfs 也能做 ,以前就是dfs a掉的,今天做了(杭电的Stange Billboard(可以看看))位运算后想了想这不就是一个行为1,列为20的位位运算吗,(上面的说过行大于列就转置,不然第一行枚举2^20,,转置了就只有两种可能了)~~
先上dfs的代码 100ms(反正不低于100)
#include<iostream>
using namespace std;
int bowl[], step, flag;
int range()
{
int i;
for (i = ; i<; i++)
if (bowl[i] == )
return ;
return ;
}
void turn(int i)
{
bowl[i] = !bowl[i];
if (i>)
bowl[i - ] = !bowl[i - ];
if (i<)bowl[i + ] = !bowl[i + ];
}
void DFS(int i, int dp)
{
if (step == dp)
{
flag = range();
return;
}
if (i >= || flag) return;
turn(i);
DFS(i + , dp + );
turn(i);
DFS(i + , dp);
}
int main()
{
int i;
for (i = ; i < ; i++)
cin >> bowl[i];
for (step = ; step<; step++)
{
flag = ;
DFS(, );
if (flag) break;
}
cout << step << endl;
return ;
}
至于用位运算0ms 直接水过~~~
#include<iostream>
#include<cstring>
using namespace std;
#define inf 0x7fffffff
#define min(a,b) a<b?a:b
int mpt[], tmp[], sign[];
int main(){
int i, j, cnt, minn = inf, x;
for (j = ; j < ; j++){
cin >> x;
if (x)
mpt[j] |= << ;
}
//行为1,列为20直接转置
for (i = ; i < << ; i++){
for (j = ; j < ; j++)
tmp[j] = mpt[j];
for (j = ; j < ; j++){
sign[j] = j == ? i : tmp[j - ];
tmp[j] ^= sign[j];
tmp[j + ] ^= sign[j];
}
if (!tmp[]){
cnt = ;
for (j = ; j < ; j++)
if (sign[j] & )
cnt++;
}
minn = min(minn, cnt);
}
cout << minn << endl;
return ;
}
关于位运算可以戳戳这里:http://www.cnblogs.com/zyxStar/p/4564335.html
[Swust OJ 781]--牛喝水的更多相关文章
- ACM-牛喝水
题目描述:牛喝水 The cows have a line of 20 water bowls from which they drink. The bowls can be either righ ...
- FZYZOJ-1569 喝水
P1569 -- 喝水 时间限制:2000MS 内存限制:131072KB 状态:Accepted 标签: 无 无 无 Description GH的N个妹子要喝水, ...
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- [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 [题目背景] 一只乌鸦在自娱自乐,它在面 ...
- 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.我们倡导大众健康生活,培养人们爱喝水的习惯,让每一次喝水,都能产生价值,让人们在喝水的同时,可享受赚钱 ...
随机推荐
- python自定义排序函数
Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...
- Mathematica
Mathematica是一款科学计算软件,很好地结合了数值和符号计算引擎.图形系统.编程语言.文本系统.和与其他应用程序的高级连接.很多功能在相应领域内处于世界领先地位,它也是使用最广泛的数学软件之一 ...
- perl5 第七章 控制结构
第七章 控制结构 by flamephoenix 一.条件判断二.循环: 1.while循环 2.until循环 3.for循环 4.针对列表(数组)每个元素的foreach循环 5. ...
- WEB和APP谁是互联网未来
据中国多家权威报告显示,作为多年专业化互联网公司炎帝网络科技综合评估预测,预计2016年全球互联网设备将达到100亿部.如果届时全球人口达到73亿,意味着平均每人将有1.4部设备.智能交通将增长50倍 ...
- HDU 3360 National Treasures
题目大意:大厅每个位置都有一个文物或者一个守卫,文物是安全的前提是: 关键位置上必须有一个守卫,或者文物本身的位置上有一个守卫.求保证每个文物是安全的守卫的最少数量. #include <cst ...
- AlertDialog弹出时背景明暗程度调整
今天有个需求是把弹出AlertDialog时的变暗的背景调整得不要那么暗. 一开始懒惰就直接百度中文搜索,结果找到的代码试了几次都不行. 后来老老实实开google.stackoverflow搜索,搜 ...
- 淘特房产CMS系统 7.5
资源描写叙述: 淘特房产CMS系统採用淘特AspCms开发,全部前台信息生成静态HTM,提供了楼盘.二手房.房产中介.房产经济人.业主社区等管理模块,集成了淘特CMS与动网论坛,Discuz,Oblo ...
- Duplicate entry '97112' for key 1
1.错误描写叙述 2014-07-08 10:27:13,939 ERROR(com.you.conn.JDBCConnection:104) -com.mysql.jdbc.exceptions.j ...
- 【iOS开发-52】假设要模仿练习,怎样找到其它应用程序的icon、使用框架等资源?
(1)在91助手等站点下载ipa格式的安装包.假设是安卓的应该是apk格式的.此处仅仅说iOS的.比方以下这个,下载到电脑. (2)然后,找到它,右击"归档",相当于解压. (3 ...
- ping的意思
Ping是测试网络联接状况以及信息包发送和接收状况非常有用的工具,是网络测试最常用的命令.Ping向目标主机(地址)发送一个回送请求数据包,要求目标主机收到请求后给予答复,从而判断网络的响应时间和本机 ...