Home  >  Article  >  Java  >  Detailed explanation of classic algorithmic problem of crossing the river

Detailed explanation of classic algorithmic problem of crossing the river

little bottle
little bottleforward
2019-04-30 15:26:332512browse

The main content of this article is a detailed explanation of the classic algorithm problem of crossing the river. Interested friends can learn more I hope it helps you.

Description

A group of N people want to cross the river in a boat. This boat can only carry two people at most. So some kind of shuttle arrangement has to be put in place to paddle back and forth so that everyone can get across. Everyone has a different rowing speed; the speed of a pair of runners depends on the speed of the slower person. Your job is to determine a strategy that will minimize the time it takes these people to cross the river.
Input

The first line of input contains an integer T (187f32fdd96957f128a2b1bc83250ebc7= 4, the problem is much more complicated, because if any two people cross the river, there are many situations where one of them comes back after crossing the river. We need to analyze it here
Observing the question, we can find that there are two most important points in crossing the river.
Scheme [1] For two people crossing the river, the time spent is determined by the longest person
In view of this, we can put the slowest d and the second slowest c together, so that the second slowest time c Just ignored.
Plan [2] The time spent by a person who comes back is determined by him alone
In view of this, we can let the fastest a send the others one by one, and then let the fastest a take the boat Send it back

Realize the above solution
When n = 4 (The speed of N people below is represented by abcd..., and according to the speed Sorting in ascending order) () indicates the time spent
Scheme [1] abcd
ab (b) past
a (a) back
cd (d) past
b (b) back
ab(b) past
Time spent: a 3b d

Plan [2] abcd
ad(d) past
a(a) back
ac (c) past
a (a) back
ab (b) past
Time spent : 2a b c d

Calculation example
Now we import the question sample {1, 2, 5, 10}
Plan [1] Time = 17
Plan [2] Time = 19
So use plan [1] It takes the shortest time, the time is 17

But if we modify the data {1, 2, 2, 10}
Scheme [1] time = 17
Scheme [2] time = 16
This time, plan [2] takes the shortest time, with a time of 16;

If we approximate the time spent by the two plans,
Plan [1]: 2b
plan [2]: a c
It can be seen that the time spentThe decisive factor lies in the fastest a, the second fastest b and the second slowest c. We only need to compare 2b and a c and choose to spend The solution that takes the least time is sufficient.

When n > 4We can express it as using the first two fastest people to transport the slowest two people, and the number of people will be reduced by 2 after transporting.

Related tutorials: Java video tutorial

The following is the AC code, for reference only

import java.util.Arrays;
import java.util.Scanner;

public class 过河         
{
	static long time = 0L;
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int m = sc.nextInt();
		for (int i = 0; i < m; i++)
		{
			int n = sc.nextInt();
			int[] A = new int[n];
			for (int j = 0; j < n; j++)
			{
				A[j] = sc.nextInt();
			}
			Arrays.sort(A);
			f(A);
			System.out.println(time);
			time = 0L;
		}
	}
	public static void f(int[] A) {
		if(A.length == 3) {
			time += A[0] + A[1] + A[2];
			return;
		}
		if(A.length == 2) {
			time += A[1];
			return;
		}
		if(A.length == 1) {
			time += A[0];
			return;
		}
		if(A[0] + A[A.length - 2] < A[1] * 2) {
			time += 2 * A[0] + A[A.length - 2] + A[A.length - 1];
		}else {
			time += A[0] + 2 * A[1] + A[A.length - 1];
		}
		int[] B = Arrays.copyOfRange(A, 0, A.length - 2);
		f(B);
	}
}

The above is the detailed content of Detailed explanation of classic algorithmic problem of crossing the river. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete