A coin Problem

Source: Internet
Author: User

Subject: A coin question (ACM competition question)
 
A coin Problem
Given n coins, there is a positive inverse input. In this way, you can only flip the adjacent three coins at a time to turn the coins to the desired state: both front and back.
Requires the least steps to complete and print out the steps
Input Method: 1 indicates the front, and 0 indicates the opposite.
For example, 1000111
Requirement 1 for results
That is to say, the order of coins is positive and negative, and the result is positive.
Required language: c

Reply to: mathe () Credit: 105 15: 36: 53z score: 25


? Simple, directly from left to right, in order to make the previous N-2 coin adjusted to the required direction, if the last two coins at this time has reached the requirements, has found the optimal solution, or no solution.
For example, 1000111
First, check that the first element is already 1 and skipped.
Then the second element is 0, which is 2, 3, 4,
Then let's look at the 3, 4, 5, all of which are 1, so it's over.
Because 6, 7 bits are 1, we need to flip the optimal solution once.

Reply to: kbsoft (jingle) () Credit: 105 18: 49: 10z score: 15


? # Include <iostream>
# Include <vector>

Using namespace STD;

# Include <memory. h>

Const int maxcoins = 25;

Int N;
Char visited [1 <maxcoins];
Int all_up, all_down;
Int masks [maxcoins], M;

Int val_of (string S, int L ){
Int val = 0;
For (INT I = 0; I <n; I ++)
Val = (Val <1) + s [I]-'0 ';
Return val;
}

Char * str_of (int v, int L ){
Static char Buf [maxcoins + 2];
While (L --){
Buf [l] = (V & 0x01) + '0 ';
V >>> = 1;
}
Return Buf;
}

Struct status {
Int Val, Prev;
} Q [1 <maxcoins];
Int HD, Tl;

Int main (void ){
While (CIN> N ){
Memset (visited, 0, sizeof (visited ));
All_up = (1 <n)-1;
All_down = 0;
Masks [m = 0] = 0x07;
While (masks [m] <= all_up ){
Masks [M + 1] = masks [m] <1;
M ++;
}

String faces;
Cin> faces;
Int current = val_of (faces, N );

Q [0]. Val = current;
Q [0]. Prev =-1;
Int steps = 0;
Bool solved = visited [all_down] | visited [all_up];
Hd = 0;
TL = 1;
While (HD <TL &&! Solved ){
Int TT = TL;
Steps ++;
While (HD <TT ){
Current = Q [hd]. Val;
For (INT I = 0; I <m; I ++ ){
Int next = Current ^ masks [I];
Q [TL]. Val = next;
Q [TL]. Prev = HD;
If (next = all_down | next = all_up ){
Solved = true;
Goto conclusion;
}
If (visited [next]) continue;
TL ++;
Visited [next] = 1;
}
HD ++;
}
}

Conclusion:
If (solved ){
Cout <"solvable in" <steps <"steps:" <Endl;
Vector <int> List (0 );
While (tL> = 0 ){
List. push_back (Q [TL]. Val );
TL = Q [TL]. Prev;
}
For (INT I = List. Size ()-1; I> = 0; I --)
Cout <str_of (list [I], n) <<'' <Endl;
}
Else {
Cout <"no solution" <Endl;
}
}
}

Reply to: mathe () Credit: 105 17: 17: 40z score: 0


? As zlf_jack said,
It is most important to switch the order of any two operations and the result remains unchanged.
In fact, the conclusion is very clear, the writing is more rigorous proof of the need to use the method of group theory.
For any operation sequence
P1, P2, P3,..., P (I), P (I + 1),..., P (n)

We exchange two adjacent operations, P (I), P (I + 1), and get the same result.
That is, the combination of operations
P (I) P (I + 1) = P (I + 1) P (I)
The proof is very simple,
If the two operations change the status of coins, both operations change the status of the coins once, so they are the same.
If either of the two operations changes the status of a coin is public, both methods change the status of the coin twice (that is, restoring the original status) non-public coins change the status exactly once, so they are the same.

From this, we can conclude that:
Inference: any two operations in the Switching Operation Sequence have the same result.
That is, pp1p2... PNQ = qp1p2... PNP
Proof: pp1p2... PNQ = p1pp2... PNQ = p1p2p... PNQ =... = P1P2... pnqp = P1P2... qpnp =... = p1p2q... PNP
= P1qp2... PNP = qp1p2... PNP
This proves the above conclusion.

Then we can come to the conclusion that
Inference 2: all operations in an operation sequence can be reordered randomly and the results are the same.
That is to say, A1, A2,..., an is a random arrangement of 1, 2,..., n, so
P (A1) P (A2)... P (an) = P (1) P (2)... P (n)
The proof is very simple. Just construct it directly. If A1 <> 1, in the original sequence, you can exchange the position where P (A1) is the same as P (1, then we can use mathematical induction or continue construction.

Inference 3: For an optimal operation sequence, no operation is repeated.
If yes, we only need to move the two operations to the last two through the switching operation sequence. Then we can know that the previous N-2 operation has been able to achieve the goal, conflict with the optimum. So there will be no repeated operations.

Then, for an optimal sequence of operations, we can first sort all the operations according to the position of the first coin in which the status changes, and then obtain another optimal sequence of ordered Operations.
Therefore, only the first operation changes the first coin of this operation, and no operation changes the coin in front of this coin, because eventually all coins are in the status of 1, therefore, the status of the coin must be 0, and the status of the coin in front of it must be 1. That is to say, we can select the first operation as the operation starting from the first coin in the 0 state.
After this operation, the remaining operation sequence must be the optimal operation for the new coin sequence (otherwise, if we can find a better operation sequence for this sequence, after adding the first operation, we can find a better operation in the original sequence, which is contradictory ). So we can use the same method.

As for the program, the time complexity is O (n ).

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.