[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.我们倡导大众健康生活,培养人们爱喝水的习惯,让每一次喝水,都能产生价值,让人们在喝水的同时,可享受赚钱 ...
随机推荐
- 微信公众号token验证失败的一些总结
这几天准备弄一个微信公众号,在进行服务器配置的时候出现总是出现token验证失败的报错. 实际上,这个问题很好解决.既然微信平台没有给我们很明确的报错提示,那么我们就可以通过跟踪获取到的请求参数进行分 ...
- PHP PDO select语句结果行数计算
PDO有一个函数PDOStatement::rowCount返回上一个SQL语句影响的行数. rowCount函数对于DELETE, INSERT, 或者UPDATE语句的结果是正确的,但对于sele ...
- Mongodb PHP开发类库
<?php /** * Mongodb 基本操作API,支持基本类似关系统型数据库的操作接口 * * @version 1.0 * * [说明] * * 1:该版本API实现了 Mongodb ...
- pyhton10min系列之程序员的浪漫-足迹生成器,有视频教程
记录去过的足迹 本文为原创文章 项目主页 跪求star 程序员的浪漫,我女朋友蘑菇喜欢旅游,于是我做了这个,记录2015一起去过的地方,祝她圣诞快乐 如果觉得对你有帮助,github求个star 视频 ...
- Objective-C浅拷贝和深拷贝
浅拷贝就是对内存地址的复制,让目标对象指针和源对象指向同一片内存空间 如: char* str = (char*)malloc(100);char* str2 = str; 浅拷贝只是对对象的简单拷贝 ...
- HDU 1501 Zipper 动态规划经典
Zipper Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- CF 459A(Pashmak and Garden-正方形给出2点求2点)
A. Pashmak and Garden time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 使用C#对MongoDB中的数据进行查询,改动等操作
首先,使用的是官方提供的C#訪问组件https://github.com/mongodb/mongo-csharp-driver 然后.编译后引用MongoDB.Bson.dll及MongoDB.Dr ...
- React Native for android 项目驱动教程
第一节 搭建开发环境 第二节 显示页面标题 第三节 实现页面布局 # React native是什么? React Native,是颠覆性的移动开发技术.它使用js开发,又是原生应用,不同于Hybri ...
- 【转】How to Start Intel Hardware-assisted Virtualization (hypervisor) on Linux to Speed-up Intel Android x86 Emulator
[转]How to Start Intel Hardware-assisted Virtualization (hypervisor) on Linux to Speed-up Intel Andro ...