Four-person bridge-Computer Simulation Algorithm for Microsoft questions

Source: Internet
Author: User

Problem: four people bridge the bridge overnight. The walking time is 1, 2, 5, and 10 minutes respectively. Four people only have one flashlight. One trip can only take two people to bridge the bridge. One trip must hold a flashlight, when the time is the slowest, I ask if I can cross the bridge within 17 minutes. How can I cross the bridge?

# Define State char <br/> # define path char <br/> const int timelimit = 17; <br/> state State [16] = {1 }; <br/> Path [16]; <br/> const int COP [10] = {1, 2, 4, 8, 3, 5, 9, 6, 10, 12}; <br/> const char * cops [10] = <br/> {<br/> "1, time consumed: 1 minute", <br/> "2, time consumed: 2 minutes ", <br/>" 5, time consumed: 5 minutes ", <br/>" 10, time consumed: 10 minutes ", <br/>" 1 and 2, time consumed: 2 minutes ", <br/>" 1 and 5, time consumed: 5 minutes ", <br/>" 1 and 10, time consumed: 10 minutes ", <br/> "2 and 5, time consumed: 5 minutes", <br/> "2 and 10, time consumed: 1 0: ", <br/>" 5 and 10, 10 minutes consumed "<br/>}; <br/> const int time [10] = {1, 2, 5, 10, 2, 5, 10, 5, 10, 10 }; <br/> void Ferry (INT state, int Dir, int P, int time) <br/>{< br/> int I, cop, J; <br/> for (I = 0; I <10; I ++) <br/>{< br/> If (COP [I]! = (COP = State & COP [I]) continue; <br/> State & = ~ COP; <br/> time + = time [I]; <br/> If (State [dir? ~ State & 15: State] | <br/> time> timelimit) <br/>{< br/> state | = cop; <br/> time-= time [I]; <br/>}< br/> else if (DIR &&! State) <br/> {<br/> path [p] = I; <br/> printf ("% d round trips during Bridge:/N ", P + 1); <br/> for (j = 0; j <= P; j ++) <br/> printf ("% s: % s/n ", (J & 1? "Right to left": "Left to right"), cops [path [J]); <br/> printf ("/N"); <br/> break; <br/>}< br/> else <br/> {<br/> State [dir? ~ State & 15: State] = 1; <br/> path [p] = I; <br/> Ferry (~ State & 15 ,! Dir, p + 1, time); <br/> State [dir? ~ State & 15: State] = 0; <br/> time-= time [I]; <br/> state | = cop; <br/>}< br/> // call time <br/> Ferry (15, 1, 0, 0 );

There are two solutions for running the test. You can change the timelimit value to obtain the solutions for other limited time conditions.

Refer:

Computer-simulated brute-force enumeration of the "wolf-like Chinese"

Discussion on generalization of birthday paradox

Smart question: connect the power of 4444 to three times to calculate the sum of digits

ABCD * E = dcba

Question About Baidu Baike: isn't the upper limit of daffodils getting too tight?

Unique 153? Isn't there a second number that can match it?

Analysis of Miller-Rabin prime number testing ideas

List of all innovative proposals for new C ++ 0x (C ++ 09) Standards

 

Mathematical Classification

 

Summary of all Microsoft products

Microsoft's big strategy in my eyes and the idea of Chinese Software

Prospects of F # and failures of C # In my eyes

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.