• poj 3185 The Water Bowls(反转)


    Description

    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?

    Input

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

    Output

    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.

    Sample Input

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

    Sample Output

    3

    Hint

    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]

    Source

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

    这里给出两种方法:

    第一种方法:反转
    1,反转的先后顺序是不重要的;
    2,主动对一个开关进行2次或2次以上的反转是多余的。
            这题,如果条件是每次必须反转3个碗的话,那么就很简单,先考虑最左端的碗,如果碗朝下,那么这个碗必须反转,同时带动后面两个碗一起反转,这样的话问题的规模就减少了一个,然后重复此方法判断。
            但是条件是在两端可以出现同时只反转两个碗的情况,这时候只要先枚举一下两端反转两个碗的所有情况,然后就可以把它当成每次必须反转3个碗进行处理就可以了。
     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 26
    23 #define inf 1e12
    24 int a[N],aa[N];
    25 int main()
    26 {
    27    for(int i=0;i<20;i++){
    28       scanf("%d",&a[i]);
    29    }
    30    int ans=1000000;
    31    for(int i=0;i<4;i++){
    32       int cnt=0;
    33       memcpy(aa,a,sizeof(a));
    34       if(i==1){
    35          aa[0]++;
    36          aa[1]++;
    37          cnt++;
    38       }
    39       else if(i==2){
    40          aa[18]++;
    41          aa[19]++;
    42          cnt++;
    43       }
    44       else if(i==3){
    45          aa[0]++;
    46          aa[1]++;
    47          aa[18]++;
    48          aa[19]++;
    49          cnt+=2;
    50       }
    51       for(int i=0;i<=17;i++){
    52          if(aa[i]&1){
    53             aa[i]++;
    54             aa[i+1]++;
    55             aa[i+2]++;
    56             cnt++;
    57          }
    58       }
    59       int flag=1;
    60       for(int j=0;j<20;j++){
    61          if(aa[j]&1){
    62             flag=0;
    63             break;
    64          }
    65       }
    66       if(flag){
    67          ans=min(ans,cnt);
    68       }
    69    }
    70    printf("%d
    ",ans);
    71     return 0;
    72 }
    View Code

    第二种方法:dfs枚举

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

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 26
    23 #define inf 1e12
    24 int a[N];
    25 int flag;
    26 int is_ok(){
    27    int f=1;
    28    for(int i=0;i<20;i++){
    29       if(a[i]==1){
    30          f=0;
    31          break;
    32       }
    33    }
    34    if(f){
    35       return 1;
    36    }
    37    else{
    38       return 0;
    39    }
    40 }
    41 void turn(int i){
    42    a[i]=!a[i];
    43    if(i>0){
    44       a[i-1]=!a[i-1];
    45    }
    46    if(i<19){
    47       a[i+1]=!a[i+1];
    48    }
    49 }
    50 void dfs(int cur,int num,int step){
    51    if(flag) return;
    52    if(num==step){
    53       flag=is_ok();
    54       return;
    55    }
    56    if(cur>=20) return;
    57    
    58 
    59    turn(cur);
    60    dfs(cur+1,num+1,step);
    61    turn(cur);
    62    dfs(cur+1,num,step);
    63 }
    64 int main()
    65 {
    66    int num=0;
    67    for(int i=0;i<20;i++){
    68       scanf("%d",&a[i]);
    69       if(a[i]==0){
    70          num++;
    71       }
    72    }
    73    if(num==20){
    74       printf("0
    ");
    75       return 0;
    76    }
    77 
    78    int ans;
    79    for(int i=1;i<100;i++){
    80       flag=0;
    81       dfs(0,0,i);
    82       if(flag){
    83          ans=i;
    84          break;
    85       }
    86    }
    87    printf("%d
    ",ans);
    88     return 0;
    89 }
    View Code
  • 相关阅读:
    hdu 1166 敌兵布阵 线段树区间修改、查询、单点修改 板子题
    POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)
    P1268 树的重量(板子)
    P1896 [SCOI2005]互不侵犯 状压dp
    Delphi {$M +} 与{$TYPEINFO ON} 和 {$M -} 与{$TYPEINFO OFF}
    SQL 通过SQL语句检索字段类型、长度、主键
    随笔2020.11.10
    Delphi 操作Windows系统睡眠-防止系统/电脑 进入睡眠或关闭显示器
    Delphi WinAPI SetThreadExecutionState
    Delphi TOpenDialog的使用介绍以及如何动态创建和释放
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5041951.html
Copyright © 2020-2023  润新知