swust oj--牛喝水

Time limit(ms): 1000    Memory limit(kb): 65535

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?

Description

Line 1: A single line with 20 space-separated integers

Input

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.

Output

1

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0

Sample Input

1

3

Sample Output

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)

 1 #include<iostream>
 2 using namespace std;
 3 int bowl[21], step, flag;
 4 int range()
 5 {
 6     int i;
 7     for (i = 0; i<20; i++)
 8     if (bowl[i] == 1)
 9         return 0;
10     return 1;
11 }
12 void turn(int i)
13 {
14     bowl[i] = !bowl[i];
15     if (i>0)
16         bowl[i - 1] = !bowl[i - 1];
17     if (i<19)bowl[i + 1] = !bowl[i + 1];
18 }
19 void DFS(int i, int dp)
20 {
21     if (step == dp)
22     {
23         flag = range();
24         return;
25     }
26     if (i >= 20 || flag) return;
27     turn(i);
28     DFS(i + 1, dp + 1);
29     turn(i);
30     DFS(i + 1, dp);
31 }
32 int main()
33 {
34     int i;
35     for (i = 0; i < 20; i++)
36         cin >> bowl[i];
37     for (step = 0; step<20; step++)
38     {
39         flag = 0;
40         DFS(0, 0);
41         if (flag) break;
42     }
43     cout << step << endl;
44     return 0;
45 }

至于用位运算0ms  直接水过~~~

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 #define inf 0x7fffffff
 5 #define min(a,b) a<b?a:b
 6 int mpt[20], tmp[20], sign[20];
 7 int main(){
 8     int i, j, cnt, minn = inf, x;
 9     for (j = 0; j < 20; j++){
10         cin >> x;
11         if (x)
12             mpt[j] |= 1 << 0;
13     }
14 //行为1,列为20直接转置
15     for (i = 0; i < 1 << 1; i++){
16         for (j = 0; j < 20; j++)
17             tmp[j] = mpt[j];
18         for (j = 0; j < 20; j++){
19             sign[j] = j == 0 ? i : tmp[j - 1];
20             tmp[j] ^= sign[j];
21             tmp[j + 1] ^= sign[j];
22         }
23         if (!tmp[19]){
24             cnt = 0;
25             for (j = 0; j < 20; j++)
26             if (sign[j] & 1)
27                 cnt++;
28         }
29         minn = min(minn, cnt);
30     }
31     cout << minn << endl;
32     return 0;
33 }

时间: 2024-12-14 17:51:39

swust oj--牛喝水的相关文章

背包 [POJ 2184 SWUST OJ 145] Cow Exhibition

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9479   Accepted: 3653 Description "Fat and docile, big and dumb, they look so stupid, they aren't much  fun..."  - Cows with Guns by Dana Lyons The cows want to prove to

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

线段树 [SWUST OJ 764] 校门外的树 Plus Plus

校门外的树 Plus Plus(0764) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 214 Accepted: 15 Description 西南某科技大学的校门外长度为 L 的公路上有一排树,每两棵相邻的树之间的间隔都是 1 米.我们可以把马路看成一个数轴,马路的一端在数轴 1 的位置,另一端在 L 的位置:数轴上的每个整数点,即 1,2,……,L,都种有一棵树. 现在要将这排树的某一段涂成某种颜色,给定 N 组区间[ 

Animate.css让添加CSS动画像喝水一样容易

animate.css是一堆很酷的,有趣的,跨浏览器的动画效果库,你可以随意在你的项目中使用.用在你想要突出的任何地方,如主页,滑块,这像喝水一样容易,迷死人了. 用法 在您的网站上使用animate.css,只要简单地把样式表插入到文档中的<HEAD>中,并为需要动画的元素添加一个CSS类名即可,以及动画的名称.就是这样!你就有了一个CSS动画效果.超强! <head>   <link rel="stylesheet" href="animat

swust oj 1026--Egg pain&#39;s hzf

题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf is crazy about reading math recently,and he is thinking about a boring problem. Now there are n integers Arranged in a line.For each integer,he wants to know

SWUST OJ Euclid&#39;s Game(0099)

Euclid's Game(0099) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 1855 Accepted: 589 Description Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the boar

茶瓶喝水

当别人把茶瓶拿走时,我才想喝水,平时就在面前而不想喝, 洗过澡才发现,原来我是那么虚弱 而每天都是建立在它的之上 洗完澡才发现,原来我是那么虚弱 而每天都是建立在它的之上 洗玩澡才发现,原来我是那么虚弱 而每天都是建立在它的之上 洗完澡才发现,原来我是那么虚弱 而每天都是建立在它的之上 洗完澡才发现,原来我是那么虚弱而每天都是建立在它的之上 当别人把茶瓶拿走时,我才想喝水,平时就在面前而不想喝 当别人把茶瓶拿走时,我才想喝水,平时就在面前而不想喝 一同发现并寻找一切有可能引发重大安全危机的漏洞,

口渴了才喝水 这些坏习惯加速衰老

健康是工作和生活的本钱,拥有一个健康的身体才是最重要的.好习惯有助于延年益寿,而坏习惯则加速衰老.日常生活中,我们的一些生活好习惯就能帮助我们养出健康好身体,而下面这些习惯,不仅影响健康,还可能让你迅速衰老. 1.口渴了才喝水 科学研究表明,渴了才喝水,会加速衰老的进程.因为人体的细胞大部分都是由水构成的,人体缺水容易损害肾脏和肝脏,还会增高血液黏稠度,影响血液循环.成年人每天平均需水量在2500毫升以上,其中约1500~2000毫升来源于直接饮水,余下的则从食物中摄取. 建议:为了避免缺水,不

成都5岁男孩趁奶奶喝水阳台玩耍 坠下6楼身亡

“可惜了,那么乖一个孩子.”望着楼下的一摊血迹,市民姚女士忍不住感慨道.7月21日晚7点过,成都市武侯区十二街十一巷发生一起惨剧.5岁男童果果(化名)趁奶奶喝水的功夫,独自一人到阳台处玩耍.不慎从6楼坠下,送医后不到一个小时便抢救无效去世. 晚上9时许,华西都市报记者赶到现场时,现场已被网络pos机封锁.但是在致民路上,男童坠落的案发地,在灯光的照射下,还是能清楚看到一摊血迹. “上面6楼就是男孩的家.”在该条路上卖碟片的刘女士说.根据记者目测,从6楼到地面,距离约有18米左右.由于是老小区,男

swust oj 649--NBA Finals(dp,后台略(hen)坑)

题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two teams, Lakers and Celtics, playing a series of NBA Finals until one of the teams wins n games. Assume that the probability of Lakers winning a game is