Description

  1. 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.
  2.  
  3. 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).
  4.  
  5. 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

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

Output

  1. 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

  1.  

Hint

  1. Explanation of the sample:
  2.  
  3. Flip bowls , , and to make them all drinkable:
  4. [initial state]
  5. [after flipping bowl ]
  6. [after flipping bowl ]
  7. [after flipping bowl ]

Source

 
 
  1. 题意:翻盖有奖:将一列碗翻成口朝上,一把下去可能同时反转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=;i<;i++){
  28. scanf("%d",&a[i]);
  29. }
  30. int ans=;
  31. for(int i=;i<;i++){
  32. int cnt=;
  33. memcpy(aa,a,sizeof(a));
  34. if(i==){
  35. aa[]++;
  36. aa[]++;
  37. cnt++;
  38. }
  39. else if(i==){
  40. aa[]++;
  41. aa[]++;
  42. cnt++;
  43. }
  44. else if(i==){
  45. aa[]++;
  46. aa[]++;
  47. aa[]++;
  48. aa[]++;
  49. cnt+=;
  50. }
  51. for(int i=;i<=;i++){
  52. if(aa[i]&){
  53. aa[i]++;
  54. aa[i+]++;
  55. aa[i+]++;
  56. cnt++;
  57. }
  58. }
  59. int flag=;
  60. for(int j=;j<;j++){
  61. if(aa[j]&){
  62. flag=;
  63. break;
  64. }
  65. }
  66. if(flag){
  67. ans=min(ans,cnt);
  68. }
  69. }
  70. printf("%d\n",ans);
  71. return ;
  72. }

第二种方法: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=;
  28. for(int i=;i<;i++){
  29. if(a[i]==){
  30. f=;
  31. break;
  32. }
  33. }
  34. if(f){
  35. return ;
  36. }
  37. else{
  38. return ;
  39. }
  40. }
  41. void turn(int i){
  42. a[i]=!a[i];
  43. if(i>){
  44. a[i-]=!a[i-];
  45. }
  46. if(i<){
  47. a[i+]=!a[i+];
  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>=) return;
  57.  
  58. turn(cur);
  59. dfs(cur+,num+,step);
  60. turn(cur);
  61. dfs(cur+,num,step);
  62. }
  63. int main()
  64. {
  65. int num=;
  66. for(int i=;i<;i++){
  67. scanf("%d",&a[i]);
  68. if(a[i]==){
  69. num++;
  70. }
  71. }
  72. if(num==){
  73. printf("0\n");
  74. return ;
  75. }
  76.  
  77. int ans;
  78. for(int i=;i<;i++){
  79. flag=;
  80. dfs(,,i);
  81. if(flag){
  82. ans=i;
  83. break;
  84. }
  85. }
  86. printf("%d\n",ans);
  87. return ;
  88. }

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. UESTC_神秘绑架案 CDOJ 881

    神秘绑架案 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...

  2. 什么是优先级队列(priority queue)?

    有时候我们需要在某个元素集合中找到最小值和最大值 .优先级队列抽象数据(Priority Queue ADT)模型是我们能够使用的方法之一,这是一种支持插入和删除最小值(DeleteMin)或者最大值 ...

  3. LeeCode-Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  4. FZU1862(线段树 或者 DP)

    Problem 1862 QueryProblem Accept: 100    Submit: 249Time Limit: 2000 mSec    Memory Limit : 32768 KB ...

  5. HDOJ-1014 Uniform Generator

    http://acm.hdu.edu.cn/showproblem.php?pid=1014 给出式子seed(x+1) = [seed(x) + STEP] % MOD seed初始为0,给出STE ...

  6. JAVA JNI

    jni非常好的一篇文章 http://m.blog.csdn.net/article/details?id=22827307 JAVA JNI介绍 http://blog.csdn.net/cyg08 ...

  7. error C2440

    error C2440: "初始化": 无法从"std::_List_const_iterator<std::_List_val<std::_List_sim ...

  8. 所闻所获6:meditashayne项目总结

    项目源码下载地址: https://github.com/ShayneYeorg/Meditashayne 1.首先一开始设计这个App的时候,我就希望它能比系统自带的备忘录更方便:比如备忘录需要手动 ...

  9. 在apache2.4版本之前做客户端访问控制,是用Allow Deny Order指令做访问控制的,

    在apache2.4版本之前做客户端访问控制,是用Allow Deny Order指令做访问控制的,而在2.4的版本上是用的用法跟之前的版本大不相同,如下 ###################### ...

  10. [网络流最大流经典][uva 11082][矩阵解压]

    题目大意 分析 #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring ...