Programming to solve the river problem

xiaoxiao2021-04-04  238

The problem is this:

One night, there were four people who had to have a river, and there was a bridge that can only withstand the weight of the two people, and the bridge surface road conditions are very poor, and hand-held flashlights can pass smoothly. The time of the four people cross the river is: 2 minutes, 3 minutes, 8 minutes and 10 minutes, and only one flashlight. Q: What is the shortest time of the four people crossing the river?

Probably programming thought is this:

(1) From the four people to pick two people cross the river, record the time (the time required for the slower in the two)

(2) Let the faster return, record time

(3) From the three people to pick two people, record the time (the time required for the slower in the two)

(4) Let the faster return, record time

(5) The remaining two people cross the river, record the time (the time required for the slower in the two)

(6) Ask the total time and temporarily stored, compare a small value with the next total time. Until all possible combinations, the shortest time is obtained.

The following is a solution program, it's been a few years, now I don't understand myself :(

#include

Static int b [4] = {2, 3, 8, 10}; Static Int A [4]; int Num1, Num2, Time = 0, Result = 100;

INT min (int A, int b) {RETURN A

Int Max (int A, int b) {RETURN A> B? A: B;}

INT min (int A, int B, int C) {a = a

Void Cross (int M, int N, int K, int R {Int min, x, y, time2; time = max (b [m], b [n]); Num1 = k-2; Num2 = 4- Num1; if (Num2 == 2) {IF (b [m]

转载请注明原文地址:https://www.9cbs.com/read-131874.html

New Post(0)