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

Source: Internet
Author: User

Problem: when a person uploads a wolf to a river, the wolf and the sheep, the sheep and the grass cannot be alone. Ask how to cross the river.

# Define State char <br/> # define path char <br/> state State [8] = {1}; <br/> Path [16]; <br/> char * BIOS [] = {"Grass", "goat", "wolf", "man" };< br/> void Ferry (INT state, int Dir, int p) <br/>{< br/> int I, bio, J; <br/> If (! Dir & (State & 6 )! = 6 & (State & 3 )! = 3) <br/>{< br/> path [p] = 3; <br/> Ferry (~ State & 7, 1, p + 1); <br/>}< br/> for (I = 0; I <3; I ++) <br/>{< br/> If (Bio = State & (1 <I) <br/>{< br/> State & = ~ Bio; <br/> If (State [dir? ~ State & 7: State] | <br/> (State & 6) = 6 | (State & 3) = 3) <br/> state | = bio; <br/> else if (DIR &&! State) <br/> {<br/> path [p] = I; <br/> printf ("Ferry process, total round trips % d:/N ", P + 1); <br/> for (j = 0; j <= P; j ++) <br/> printf ("% s: % s/n ", (J & 1? "Right to left": "Left to right"), BIOS [path [J]); <br/> printf ("/N"); <br/> break; <br/>}< br/> else <br/> {<br/> State [dir? ~ State & 7: State] = 1; <br/> path [p] = I; <br/> Ferry (~ State & 7 ,! Dir, p + 1); <br/> State [dir? ~ State & 7: State] = 0; <br/> state | = bio; <br/>}< br/>}

Call method:

Ferry (7, 1, 0 );

Refer:

Four-person bridge-Computer Simulation of Microsoft interview questions

Discussion on generalization of birthday paradox

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.