Microsoft interview question state machine

Source: Internet
Author: User

It is known that four switches are distributed on the four corners of the square.

1. Each switch has only two States on or off. When you touch the switch, its status changes to the other one (On, off, off, and on)

2. YouInvisibleWhat is the status of the current switch, and you cannot determine whether the current switch is on or off through touch

3. You can change the status of one switch or two switches for each operation.

4. After the operation is complete, the disc will rotate randomly (that is to say, the positions of these switches have been moved during the next operation)

5. Because the distance between the adjacent and Diagonal Points of a square is different, when you change the two switches, you can determine whether you change the diagonal or adjacent points.

 

6. When the switch is fully turned on or off, there will be a light outside, so congratulations, you have done it.

 

Q:

Is there a strict process that enables all the initial states to reach the goal through certain operations and enable the light to shine?

 

PS: This question is mainly about the state transition between the state machine and the state machine.

PS: I personally think this question is very difficult. It is very difficult to solve it if I have never encountered it or find the right idea.

 

Simple Solution logic:

It can be proved that the initial state is equivalent to four States: 0000 0001 0011 0101 other states.

For example, 0000 = 1111 0001 = 0010 = 1110 (the reason for opening, closing, and unknown order cannot be determined)

Because 0000 is already the result, there may be only three States at the beginning

A: 0001 B: 0011 C: 0101

 

Procedure:

If the initial status is C: 0101, the two switches at a time must reach the final result.

If the initial status is not C, there are two cases: 0011 and 0001.

 

Assuming the initial value is B: 0011, it can be proved that 0011 will be converted to 0110 or another value when the two switches on the diagonal corner are pressed, but all are equivalent to 0011, so the current status is still 0011

Next, press 0011 next to two adjacent switches in the current status. The current status is either 0000 or 0101,000, and the switch is successful, if the switch fails, press the switch on the diagonal corner again. Then the value 0101 must be 0000.

If the current operation is 0001, no matter how many operations are performed (two buttons are pressed for each operation), the final state is equivalent to 0001. In this case, you only need to perform the operation once (only one switch is pressed) then 0001 will be changed to 0000, 0011, 0101, or, one of the three types, and then return to the above operation logic for a solution.

 

Chart operation logic

Initial status

A: 0101

B: 0011

C: 0001

Two diagonal Switches

Complete

Still equivalent to 0011

Still equivalent to 0001

Based on two adjacent Switches

N/

Changed to 0101

Still equivalent to 0001

Two diagonal Switches

 

N/A completed

Still equivalent to 0001

Press any switch

 

N/

Complete 0011 or 0101

If you haven't succeeded yet, now only 0011 and 0101 are supported.

 

 

 

 

 

 

 

 

PS: the solution is very simple, but it is very difficult to find the right path.

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.