算法:快速过桥

问题描述:

A group of n people wish to cross a bridge at night.
n个人的队伍想在晚上通过一座大桥。

At most two people may cross at any time, and each group must have a flashlight.
任何时间最多有2人通过,每组必须有一个手电筒。

Only one flashlight is available among the n people, so some sort of shuttle arrangement must be arranged in order to return the flashlight so that more people may cross.
很可怜,这n个人只有一个手电筒可用,因此必须合理地安排,让手电筒能回到另一端,这样才能让更多人通过大桥。

Each person has a different crossing speed; the speed of a group is determined by the speed of the slower member.
每个人有不同的速度,编组后,一个组的速度等于慢的那个人的速度。

Your job is to determine a strategy that gets all n people across the bridge in the minimum time.
你的任务是实现一种策略,让所有人在最短时间内通过。

[输入]

The input begins with a single positive integer on a line by itself indicating the number of test cases, followed by a blank line. There is also a blank line between each two consecutive inputs.
第一行是一个单独的数字,代表测试案例的个数;随后是一个空行。后面每两个输入之间都有一个空行。

The first line of each case contains n, followed by n lines giving the crossing times for each of the people. There are not more than 1,000 people and nobody takes more than 100 seconds to cross the bridge.
每个测试案例的第一行是n的值,随后n行是每个人单独通过大桥的时间。人数≤1000,时间≤100s

【输出】

For each test case, the first line of output must report the total number of seconds required for all n people to cross the bridge.
对每个案例,首行是n个人通过的时间。

Subsequent lines give a strategy for achieving this time. Each line contains either one or two integers, indicating which person or people form the next group to cross.
随后是你的通过和返回次序,每一行是一个或者两个整数,对应着人(通过时间代表人)。返回也要占一行。

Each person is indicated by the crossing time specified in the input.Although many people may have the same crossing time, this ambiguity is of no consequence.
虽然不同的人可能有相同的速度,但这没有影响(看做一种人就好了)。

Note that the crossings alternate directions, as it is necessary to return the flashlight so that more may cross.
注意方向,意思就是说返回情况也要占一行。

If more than one strategy yields the minimal time, any one will do.
多种方案都是最短时间的,任选其一皆可。

The output of two consecutive cases must be separated by a blank line.
案例之间空行分界。

【样例输入】
1

4
1
2
5
10



【样例输出】

17
1 2
1
5 10
2
1 2



【解释】

171 2   21     15 10  102     21 2   2

解题思路

首先将所有过桥时间排序,以下为获得最小过桥时间的算法:

(1)过桥总人数为 1,该人的过桥时间即为最短过桥时间。

(2)过桥总人数为 2,过桥时间较大的人的过桥时间即为最短过桥时间。

(3)过桥总人数为 3,假设为A,B,C,则(AB),(A),(AC)策略和 (AC),(A),(AB)策略的时间相同,3人过桥时间之和为最短时间。

(4)过桥总人数大于3,假设最前面为A,B两人,最后为Y,Z两人,有两种策略:(AB),(A),(YZ),(B),(AB)和(AZ),(A),(AY),(A),(AB)。

比较两种策略那种过桥时间少就选那种,然后将总人数减去 2,若总人数仍大于 3,继续该步骤直到剩下需要过桥的人数小于等于 3。

可用递归或直接迭代实现。
若用递归:进入递归条件是总数大于3;递归最深深度截至条件(也就是常说的递归截至条件,这词是我自己加理解起的)是人数等于1或2或3

[解题代码]
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;

public class Num6_Bridge {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int m = scanner.nextInt();
		scanner.nextLine();
		//遍历案例
		for (int i = 0; i < m; i++) { 
			int n = scanner.nextInt();
			scanner.nextLine();
			int [] data = new  int[n];
			//初始化每组人的时间
			for (int j = 0; j < n; j++) {
				data[j] = scanner.nextInt();
			}
			//排序
			Arrays.sort(data);
			
			MyTimerDS deal = deal(data,data.length);
			deal.out();
			  if (i<m-1)//最后一个空行,UVA的空行也严格比对
			        System.out.println();
		}
		scanner.close();
		
	}
	/***
	 * 
	 * @param data
	 * @param length 要处理的人数
	 * @return
	 */

	private static MyTimerDS deal(int[] data,int length) {
			int A = data[0];
			MyTimerDS myTimerDS = new MyTimerDS();
			if (length == 1) {//一个人
				myTimerDS.totalTime += A;
				myTimerDS.record.add(String.valueOf(A));
				return myTimerDS;
			}
			int B = data[1];
			if(length == 2) {//二个人
				myTimerDS.totalTime += B;
				myTimerDS.record.add(A+" "+B);
				return myTimerDS;
			}
			int C = data[2];
			if(length == 3) {//三个人
				myTimerDS.totalTime += A+B+C;
				myTimerDS.record.add(A+" "+C);
				myTimerDS.record.add(String.valueOf(A));
				myTimerDS.record.add(A+" "+B);
				return myTimerDS;
			}
			if (length > 3) {
				
				int Z = data[length-1];
				int Y = data[length-2];
				//A Z先走
				if ((Z+Y+A)<(B+Z+B)) {
					//递归  确保了每次调用捎带了数据。调用并函数处理完后,赋给下次myTimerDS,执行本次数据的添加。
					myTimerDS = deal(data, length-2);
					myTimerDS.totalTime += A+Z+Y+A;
					//按照题目要求和解题思路,先运行的结果放在队列后面,后执行的放在前面,也就是按时间从存放,用一直插入队头实现先进后出
					//插入的队头。代码自上向下执行,先执行的是被后执行插入排到后面。 执行插入后:A,A Y,A,A Z  结果:A Z,A,A Y,A
					myTimerDS.record.addFirst(String.valueOf(A));
					myTimerDS.record.addFirst(A+" "+Y);
					myTimerDS.record.addFirst(String.valueOf(A));
					myTimerDS.record.addFirst(A+" "+Z);
				}else {
					myTimerDS = deal(data, length-2);
					myTimerDS.totalTime += B+A+Z+B;
					myTimerDS.record.addFirst(String.valueOf(B));
					myTimerDS.record.addFirst(Z+" "+Y);
					myTimerDS.record.addFirst(String.valueOf(A));
					myTimerDS.record.addFirst(A+" "+B);
				}
			}
		return myTimerDS;
		
	}

}
  class MyTimerDS{
	int totalTime = 0;
//	List<String> record = new ArrayList<>();
	Deque<String> record = new LinkedList<>();

	void out() {
		System.out.println(totalTime);
		for(String string :record)
			System.out.println(string);
		
	}
}

PC/UVa IDs: 110403/10037,

Popularity: B,

Success rate: low Level: 3

测试地址:https://vjudge.net/problem/UVA-10037

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值