Simple BFs, bitwise operation, instant Spike
Card Game
Time Limit: 9000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1574 accepted submission (s): 515
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 input01
011
Sample outputno
1
Authorwangye
Recommendwangye
# Include <stdio. h> # Include <Queue> # Include <Math. h> # Include < String . H> # Include <Iostream> Using Namespace STD; Int Mark [ 2000000 ]; Queue < Int > Que [ 2 ]; Char G [ 22 ]; Int BFS (){ Int A = 0 , B = 1 ; While (Que [A]. Size ()! = 0 ) Que [A]. Pop (); While (Que [B]. Size ()! =0 ) Que [B]. Pop (); memset (mark, 0 , Sizeof (Mark )); Int St = 0 ; Int Len = Strlen (g ); For ( Int I = len- 1 ; I> = 0 ; I -- ) ST = ST * 2 + G [I]- ' 0 ' ; Int CNT =- 1 ; Que [A]. Push (ST); Mark [st] = 1 ; While (Que [A]. Size ()! = 0 ) {CNT ++ ; Swap (A, B ); While (Que [B]. Size ()! = 0 ){ Int Cur = Que [B]. Front (); que [B]. Pop (); If (Cur = 0 ) Return CNT; For ( Int I = 0 ; I <Len; I ++ ){ Int Nwnode; If (I = len- 1 ) {Nwnode = (Cur ^ ( 1 <(LEN- 1 ) ^ ( 1 <(LEN- 2 ));} If (I = 0 ) Nwnode = (cur ^ 1 ) ^ 2 ; If (Len! = 2 & I! = 0 & I! = Len- 1 ) {Nwnode = (Cur ^ ( 1 <I) ^ ( 1 <(I + 1 ) ^ ( 1 <(I- 1 )));} If (MARK [nwnode] = 0 ) {Mark [nwnode] = 1 ; Que [A]. Push (nwnode );}}}} Return - 1 ;} Int Main (){ While (Scanf ( " % S " , G )! =EOF ){ Int TMP = BFS (); If (TMP =- 1 ) Printf ( " No \ n " ); Else Printf ( " % D \ n " , TMP );} Return 0 ;}