Card Game
Time Limit: 9000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2180 accepted submission (s): 787
Problem description has a card game, which is very interesting. It gives you n cards in a single line. Cards have both sides. The cards at the beginning may be in a messy state (somewhat positive, and now you need to sort these cards. But the trouble is that every time you flip a card (from the front to the back, or from the back to the front), two cards (the leftmost and rightmost cards, it will only affect the nearby one. You must also flip it. Now, I will give you a messy state and ask if you can sort them out so that each card is facing up. If you can, the minimum number of operations required.
Input has multiple cases. Each case is input with A 01 sign string (length not greater than 20). 1 indicates the opposite side facing up, and 0 indicates the front facing up.
For each group of cases, If you can flip the output, the output requires at least the number of flip operations; otherwise, the output is no.
Sample input01011
Sample outputno1 each card is determined by three cards (excluding the first two cards). For the first card, if you do not consider the last card, you only consider the first card, so by the I-1 card status can decide the I card do not flip, (because when the flop is not necessary to flip a card multiple times, so each card is only two situations: flip or don't flip), if the I-1 card is the opposite, then the I-1, I, I + 1 are turned once, otherwise do not flip back (to I + 1) after performing the preceding operations, it is clear that the initial status is 1st cards or not, and the preceding operations are performed for both statuses to see which status can be obtained. At the end, you only need to judge whether the last card is positive (all the cards in front of the first card are facing up before I ). Code:
1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 # include <iostream> 5 using namespace STD; 6 # define INF 999999999 7 8 int A [22]; 9 char C [22]; 10 int N; 11 12 13 int DFS (int K, int step) {14 15 if (k> = N) {16 return a [k-1]? INF: Step; 17 18} 19 if (a [k-1]) {// if the front of the K card is the opposite, so you need to turn the K card so that the K-1 card face up 20 A [k-1] = 0; 21 A [k] =! A [k]; 22 A [k + 1] =! A [k + 1]; 23 step ++; 24} 25 return DFS (k + 1, step); 26} 27 28 main () 29 {30 int I, J; 31 While (scanf ("% s", c )! = EOF) {32 N = strlen (c); 33 for (I = 0; I <n; I ++) A [I] = C [I]-'0'; 34 A [0] =! A [0]; A [1] =! A [1]; // flip the first card 35 int num = inf; 36 37 num = min (Num, DFS (1, 1); 38 for (I = 0; I <n; I ++) A [I] = C [I]-'0'; 39 num = min (Num, DFS (1, 0 )); // do not flip the first card 40 if (num = inf) printf ("NO \ n"); 41 else printf ("% d \ n", num ); 42} 43}