POJ 3185

The Water Bowls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4088   Accepted: 1609

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

 
固定左边的第一个位置的翻转情况,然后据此推算出其他所有位置的情况。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 int w[25];
 9 int flip[25];
10 
11 int get(int x) {
12     int c = w[x] + flip[x];
13     if(x - 1 >= 1) c += flip[x - 1];
14     if(x + 1 <= 20) c += flip[x + 1];
15     return c % 2;
16 }
17 
18 int cal() {
19         int res = 0;
20         for(int i = 2; i <= 20; ++i) {
21                 if(get(i - 1)) flip[i] = 1;
22         }
23 
24         if(get(20)) return -1;
25         for(int i = 1; i <= 20; ++i) res += flip[i];
26 
27         return res;
28 }
29 
30 void solve() {
31         int ans = -1;
32         for(int v = 0; v <= 1; ++v) {
33                 memset(flip,0,sizeof(flip));
34                 flip[1] = v;
35                 int num = cal();
36                 if(num >= 0 && (ans < 0 || ans > num)) {
37                         ans = num;
38                 }
39         }
40 
41         printf("%d
",ans);
42 }
43 
44 int main()
45 {
46     //freopen("sw.in","r",stdin);
47     for(int i = 1; i <= 20; ++i) {
48             scanf("%d",&w[i]);
49     }
50 
51     solve();
52 
53     return 0;
54 }
View Code
原文地址:https://www.cnblogs.com/hyxsolitude/p/3632523.html