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