zoukankan      html  css  js  c++  java
  • POJ3185 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]

            这题属于反转(开关)类型的题,典型的特征就是:
    1,反转的先后顺序是不重要的。
    2。主动对一个开关进行2次或2次以上的反转是多余的。

            这题。假设条件是每次必须反转3个碗的话,那么就非常easy,先考虑最左端的碗,假设碗朝下,那么这个碗必须反转,同一时候带动后面两个碗一起反转,这种话问题的规模就降低了一个,然后反复此方法推断。
            可是条件是在两端能够出现同一时候仅仅反转两个碗的情况,这时候仅仅要先枚举一下两端反转两个碗的全部情况,然后就能够把它当成每次必须反转3个碗进行处理就能够了。
    #include <stdio.h>
    #include <vector>
    #include <math.h>
    #include <string.h>
    #include <string>
    #include <iostream>
    #include <queue>
    #include <list>
    #include <algorithm>
    #include <stack>
    #include <map>
    
    using namespace std;
    
    int main()
    {
    #ifdef _DEBUG
    	freopen("e:\in.txt", "r", stdin);
    #endif
    	int side[21];
    	int side1[21];
    	for (int i = 0; i < 20; i ++)
    	{
    		scanf("%d", &side[i]);
    	}
    	int minCount = 50;
    	for (int i = 0; i < 4;i++)
    	{
    		int count1 = 0;
    		memcpy(side1, side, sizeof(side));
    		if (i == 1)
    		{
    			side1[0]++;
    			side1[1]++;
    			count1++;
    		}
    		else if (i == 2)
    		{
    			side1[18]++;
    			side1[19]++;
    			count1++;
    		}
    		else if (i == 3)
    		{
    			side1[0]++;
    			side1[1]++;
    			side1[18]++;
    			side1[19]++;
    			count1++;
    			count1++;
    		}
    		for (int i = 0; i <= 17; i++)
    		{
    			if (side1[i] & 1)
    			{
    				side1[i] ++;
    				side1[i + 1]++;
    				side1[i + 2]++;
    				count1++;
    			}
    		}
    		if (!(side1[1] & 1 || side1[18] & 1 || side1[19] & 1))
    		{
    			if (minCount > count1)
    			{
    				minCount = count1;
    			}
    		}
    	}
    	printf("%d
    ", minCount);
    	return 1;
    }



  • 相关阅读:
    生成随机数的三种方法
    老外最常说的二十句钻石级英语
    线性探查法实现的散列表(哈希表)类
    STL容器之间可以直接相互赋值使用
    用位向量实现集合抽象数据类型
    一个利用map统计一段英文文章中每个单词出现次数的小程序
    各种排序算法的稳定性和时间复杂度小结
    24佳句对译收藏
    SQL 将一个字段内用逗号分隔的内容分成多条记录
    SQL递归查询(with cte as)
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/6890912.html
Copyright © 2011-2022 走看看