zoukankan      html  css  js  c++  java
  • poj 3185 The Water Bowls

    The Water Bowls
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6731   Accepted: 2650

    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

    /*
    * @Author: Lyucheng
    * @Date:   2017-08-04 15:12:25
    * @Last Modified by:   Lyucheng
    * @Last Modified time: 2017-08-04 17:00:37
    */
    /*
     题意:给你一个20的01序列,每次你可以反转相邻的三个点,问你至少反转多少次才能使得所有的点变成0
    
     思路:开关反转问题,一个序列如果有解的话,那么需要反转的区间是一定的,有个区间反转两次是没有
        意义的,如果一个点为1那么所在区间是一定要进行反转的,并且只翻转一次,所有可以两头开始模拟
        反转,比较一下大小
    */
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAXN 25
    
    using namespace std;
    
    int pos1[MAXN];
    int pos2[MAXN];
    int res1;
    int res2;
    
    inline void init(){
        res1=0;
        res2=0;
    }
    
    int main(){ 
        // freopen("in.txt", "r", stdin);
        // freopen("out.txt", "w", stdout);
        while(scanf("%d",&pos1[1])!=EOF){
            pos2[1]=pos1[1];
            for(int i=2;i<=20;i++){
                scanf("%d",&pos1[i]);
                pos2[i]=pos1[i];
            }
            init();
            for(int i=1;i<=20;i++){
                if(pos1[i]==1){
                    if(i==20) res1=21;//无解
                    res1++;
                    pos1[i]=!pos1[i];
                    pos1[i+1]=!pos1[i+1];
                    pos1[i+2]=!pos1[i+2];
                }
            }
            for(int i=20;i>=1;i--){
                if(pos2[i]==1){
                    if(i==1) res2=21;//无解
                    res2++;
                    pos2[i]=!pos2[i];
                    pos2[i-1]=!pos2[i-1];
                    pos2[i-2]=!pos2[i-2];
                }
            }
            printf("%d
    ",min(res1,res2));
        }
        return 0;
    }
  • 相关阅读:
    到底该不该熟悉掌握struts2的ONGL呢?
    struts2 request内幕 为什么在struts2用EL表达式可以取值
    struts2 权限拦截器 拦截没有登陆的请求
    tomcat context 配置 项目部署
    tomcat 设置默认编码格式
    工作记录1
    javascript 的学习笔记(第一天)
    JavaScript for...in 循环
    indexof方法区分大小写
    java 和 IntelliJ IDEA 的一些配置
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/7286139.html
Copyright © 2011-2022 走看看