Description

The cows have a line of  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  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 (=undrinkable, =drinkable -- it even looks like a bowl), what is the minimum number of bowl flips necessary to turn all the bowls right-side-up?

Input

Line : A single line with  space-separated integers

Output

Line : The minimum number of bowl flips necessary to flip all the bowls right-side-up (i.e., to ). For the inputs given, it will always be possible to find some combination of flips that will manipulate the bowls to  's.

Sample Input

                   

Sample Output


Hint

Explanation of the sample: 

Flip bowls , , and  to make them all drinkable:
[initial state]
[after flipping bowl ]
[after flipping bowl ]
[after flipping bowl ]

Source

 
 
题意:翻盖有奖:将一列碗翻成口朝上,一把下去可能同时反转3个或2个(首尾),求最小翻转次数。

这里给出两种方法:

第一种方法:反转
1,反转的先后顺序是不重要的;
2,主动对一个开关进行2次或2次以上的反转是多余的。
        这题,如果条件是每次必须反转3个碗的话,那么就很简单,先考虑最左端的碗,如果碗朝下,那么这个碗必须反转,同时带动后面两个碗一起反转,这样的话问题的规模就减少了一个,然后重复此方法判断。
        但是条件是在两端可以出现同时只反转两个碗的情况,这时候只要先枚举一下两端反转两个碗的所有情况,然后就可以把它当成每次必须反转3个碗进行处理就可以了。
 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int a[N],aa[N];
int main()
{
for(int i=;i<;i++){
scanf("%d",&a[i]);
}
int ans=;
for(int i=;i<;i++){
int cnt=;
memcpy(aa,a,sizeof(a));
if(i==){
aa[]++;
aa[]++;
cnt++;
}
else if(i==){
aa[]++;
aa[]++;
cnt++;
}
else if(i==){
aa[]++;
aa[]++;
aa[]++;
aa[]++;
cnt+=;
}
for(int i=;i<=;i++){
if(aa[i]&){
aa[i]++;
aa[i+]++;
aa[i+]++;
cnt++;
}
}
int flag=;
for(int j=;j<;j++){
if(aa[j]&){
flag=;
break;
}
}
if(flag){
ans=min(ans,cnt);
}
}
printf("%d\n",ans);
return ;
}

第二种方法:dfs枚举

思路:枚举反转的步数,dfs,判断是否可行,可行则输出,否则继续。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int a[N];
int flag;
int is_ok(){
int f=;
for(int i=;i<;i++){
if(a[i]==){
f=;
break;
}
}
if(f){
return ;
}
else{
return ;
}
}
void turn(int i){
a[i]=!a[i];
if(i>){
a[i-]=!a[i-];
}
if(i<){
a[i+]=!a[i+];
}
}
void dfs(int cur,int num,int step){
if(flag) return;
if(num==step){
flag=is_ok();
return;
}
if(cur>=) return; turn(cur);
dfs(cur+,num+,step);
turn(cur);
dfs(cur+,num,step);
}
int main()
{
int num=;
for(int i=;i<;i++){
scanf("%d",&a[i]);
if(a[i]==){
num++;
}
}
if(num==){
printf("0\n");
return ;
} int ans;
for(int i=;i<;i++){
flag=;
dfs(,,i);
if(flag){
ans=i;
break;
}
}
printf("%d\n",ans);
return ;
}

poj 3185 The Water Bowls(反转)的更多相关文章

  1. POJ 3185 The Water Bowls 【一维开关问题 高斯消元】

    任意门:http://poj.org/problem?id=3185 The Water Bowls Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. poj 3185 The Water Bowls

    The Water Bowls 题意:给定20个01串(最终的状态),每个点变化时会影响左右点,问最终是20个0所需最少操作数? 水题..直接修改增广矩阵即可:看来最优解不是用高斯消元(若是有Gaus ...

  3. POJ 3185 The Water Bowls(高斯消元-枚举变元个数)

    题目链接:http://poj.org/problem?id=3185 题意:20盏灯排成一排.操作第i盏灯的时候,i-1和i+1盏灯的状态均会改变.给定初始状态,问最少操作多少盏灯使得所有灯的状态最 ...

  4. POJ 3185 The Water Bowls (高斯消元)

    题目链接 题意:翻译过来就是20个0或1的开关,每次可以改变相邻三个的状态,问最小改变多少次使得所有开关都置为0,题目保证此题有解. 题解:因为一定有解,所以我们可以正序逆序遍历两次求出较小值即可.当 ...

  5. POJ 3185 The Water Bowls (高斯消元 求最小步数)

    题目链接 题意:有20个数字,0或1.如果改变一个数的状态,它左右两边的两个数的状态也会变反.问从目标状态到全0,至少需要多少次操作. 分析: 和上一题差不多,但是比上一题还简单,不多说了,但是在做题 ...

  6. poj 3185 The Water Bowls 高斯消元枚举变元

    题目链接 给一行0 1 的数, 翻转一个就会使他以及它左右两边的都变, 求最少多少次可以变成全0. 模板题. #include <iostream> #include <vector ...

  7. POJ:3185-The Water Bowls(枚举反转)

    The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7402 Accepted: 2927 Descr ...

  8. POJ3185 The Water Bowls 反转(开关)

    Description The cows have a line of 20 water bowls from which they drink. The bowls can be either ri ...

  9. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

随机推荐

  1. jQuery中append html后绑定事件不起作用

    事件一定要紧跟append之后, 否则append元素点击不起作用 $(function(){$('div').append('<ul><li id="appli" ...

  2. 正则表达式小试牛刀--匹配我的csdn博文标题

    正则表达式小试牛刀--匹配我的博文标题 作者:vpoet 邮箱:vpoet_sir@163.com 正则匹配,我以我的博客页面的博客标题为例:http://blog.csdn.net/u0130187 ...

  3. 【转】android 物理按键

    关键词:android   按键  矩阵按键 AD按键  平台信息: 内核:linux2.6/linux3.0 系统:android/android4.0 平台:S5PV310(samsung exy ...

  4. 【转】Android LCD(二):LCD常用接口原理篇

    关键词:android LCD TFT TTL(RGB)  LVDS  EDP MIPI  TTL-LVDS  TTL-EDP 平台信息:内核:linux2.6/linux3.0系统:android/ ...

  5. Python常用模块 (2) (loging、configparser、json、pickle、subprocess)

    logging 简单应用 将日志打印到屏幕 import logging logging.debug('debug message') logging.info('info message') log ...

  6. FMDB使用

    FMDBManager.h #import <Foundation/Foundation.h> #import "FMDatabase.h" @interface FM ...

  7. HDU 4333 Revolving Digits 扩展KMP

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4333 题意:给以数字字符串,移动最后若干位到最前边,统计得到的数字有多少比原来大,有多少和原来同样,有多少 ...

  8. 【分割平面,分割空间类题】【HDU1290 HDU2050】

    HDU 2050 折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. 关于JS 对象与JSON对象

    Js对象 格式 : //var dataStr = '{ code: 1, msg: "修改成功", read: 1 }'; 序列化字符串为js对象: var p = eval(& ...

  10. escape和unescape给字符串编码

    var before = "\xxx\xxx" var after = escape(before); var after2 = unescape(after );