<! --
@ Page {margin: 0.79in}
H2 {margin-top: 0.18in; margin-bottom: 0.18in; line-Height: 173%; page-break-Inside: Avoid}
H2.western {font-family: "Arial", sans-serif; font-size: 16pt}
H2.cjk {font-family: "", "simhei"; font-size: 16pt}
H2.ctl {font-family: "dejavu sans"; font-size: 16pt}
P {margin-bottom: 0.08in}
-->
Now Xiaoming's family has a bridge, and it is dark when crossing the bridge, so there must be lights. Now James wants to bridge the bridge
1 second, James's younger brother wants
3 seconds, James's father wants
6 seconds. James's mother needs 8 seconds. James's grandfather wants
12 seconds. Each time the bridge can be crossed by a maximum of two people, the speed of the bridge depends on the speed of the bridge, and after the light is ignited
It will go off in 30 seconds. How does James family bridge the bridge? (It was originally a smart question. Here we use a program to solve it)
Solution: --> 3 min Ming, younger brother
<-- 1 min
--> 6 min Ming, Dad
<-- 1 min
--> 12 min Ming, ye
<-- 3 min younger brother
--> 3 Min. Total time: 29 min
Conclusion: Make sure that the younger brother (the second time is small) acts as the one-time lamp delivery person, and the longest two times will come together. In this case, the younger brother (the second time person) will play the role of sending lights.
Code: (using Recursive Computing)
# Include "stdio. H "<br/> # define N 5 <br/> # define size 64 <br/> // enter the employee ID: James-0, younger brother-1, Father-2, mom-3, Grandpa-4 <br/> // current position of each person: 0 -- on the left of the bridge, 1 -- on the right of the bridge <br/> int position [N]; <br/> // array subscript of the temporary bridge scheme; temporary scheme; minimum time scheme; <br/> int index, tmpscheme [size], Scheme [size]; <br/> // The sum of the minimum bridge time, with an initial value of 100. The time required by each person to bridge the bridge <br/> int mintime = 100, time [N] = {1, 3, 6, 8, 12 };< br/> // find the best bridge scheme. Remnant: number of people not crossing the bridge; curtime: current used time; <br/> // direction: bridge direction, 1 -- right, 0 -- left <br/> void find (INT remnant, int curtime, int direction) <br/>{< br/> // all people have crossed the bridge, minimum Update Time and solution <br/> If (remnant = 0) <br/>{< br/> mintime = curtime; <br/> for (INT I = 0; I <size & tmpscheme [I]> = 0; I ++) <br/> Scheme [I] = tmpscheme [I]; <br/>}< br/> else if (Direction = 1) // The Bridge direction is right, select two people from the left of the bridge to bridge the bridge <br/>{< br/> for (INT I = 0; I <n; I ++) <br/>{< br/> I F (position [I] = 0 & curtime + time [I] <mintime) <br/>{< br/> tmpscheme [index ++] = I; <br/> position [I] = 1; <br/> for (Int J = 0; j <n; j ++) <br/>{< br/> int tmpmax = (time [I]> time [J]? Time [I]: time [J]); <br/> If (position [J] = 0 & curtime + tmpmax <mintime) <br/> {<br/> tmpscheme [index ++] = J; <br/> position [J] = 1; <br/> Find (remnant-2, curtime + tmpmax ,! Direction); <br/> position [J] = 0; <br/> tmpscheme [-- Index] =-1; <br/>}< br/> position [I] = 0; <br/> tmpscheme [-- Index] =-1; <br/>}< br/> else // The Bridge is directed to the left, select a person from the right of the bridge to deliver the light <br/> {<br/> for (Int J = 0; j <n; j ++) <br/> {<br/> If (position [J] = 1 & curtime + time [J] <mintime) <br/> {<br/> tmpscheme [index ++] = J; <br/> position [J] = 0; <br/> Find (remnant + 1, curtime + time [J],! Direction); <br/> position [J] = 1; <br/> tmpscheme [-- Index] =-1; <br/>}< br/> int main () <br/>{< br/> for (INT I = 0; I <size; I ++) // the content of the initial solution is negative, avoid conflicts with personnel labels <br/> Scheme [I] = tmpscheme [I] =-1; <br/> Find (n ); <br/> printf ("mintime = % d:", mintime); <br/> for (INT I = 0; I <size & Scheme [I]> = 0; I + = 3) <br/> printf ("% d-% d", Scheme [I], scheme [I + 1], Scheme [I + 2]); <br/> printf ("-- the last '-1' means calculate completely! "); <Br/> getchar (); // If getchar () is not used, the program will flash through, the result will not be visible <br/> // printf ("/N"); // if this line is included, you can skip getchar, will output a carriage return and then exit <br/> return 0; <br/>}