【蓝桥备考-每周一题6】n人过桥问题

一、题目
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
【解释】
17 秒
1 2 2秒
1 1秒
5 10 10秒
2 2秒
1 2 2秒

二、分析
1.当过桥人数只有1人,直接过桥;
当过桥人数有2人,直接过桥;
当过桥人数有3人,按照AC,A,AB的方式过桥(A,B,C分别为三个速度的正排序);
当过桥人数有4人及以上,有两种过桥方案:①AB,A,YZ,B②AZ,A,AY,A(A,B为速度最快的两人,Y,Z为速度最慢的两人),这样相当于每次过桥两个人,通过循环操作,直到未过桥的人数小于4人。每次需要比较两种方案的总耗时,选择耗时短的方案过桥。

2.用ArrayList来存储未过桥的人、已过桥的人和过桥路径。每次的过桥和返回,都要更新这三个链表,保持他们总是处于正确的状态。

3.更新过桥人数链表的函数
由于对于链表的操作,要么是取最小值,要么是取最大值,且取完后要从链表中删除,所以这两个操作可以分别用两个函数来写,即getMinFromList和getMaxFromList。

三、代码

package week6;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int numOfexam = in.nextInt();
		List<ArrayList<Integer>> allSpeed = new ArrayList<ArrayList<Integer>>(numOfexam);
		//存储所有案例的List,基本元素是每个案例的速度
		for(int i=0;i<numOfexam;i++)
		{
			int n=in.nextInt();
			ArrayList<Integer> speed = new ArrayList<Integer>(n);
			for(int j=0;j<n;j++)
			{
				int speedNow=in.nextInt();
				speed.add(speedNow);
			}
			allSpeed.add(speed);
		}
		Iterator<ArrayList<Integer>> iter = allSpeed.iterator();
		while(iter.hasNext())
		{
			ArrayList<Integer> speed = (ArrayList<Integer>) iter.next();
			passBridge(speed.size(),speed);
		}
	}
	private static void passBridge(int n,ArrayList<Integer> speed)
	{
		ArrayList<Integer> before = new ArrayList<Integer>(n);  //未过桥的人
		before.addAll(speed);
		ArrayList<Integer> after = new ArrayList<Integer>(n);   //已过桥的人
		ArrayList<String> track = new ArrayList<String>(n);       //过桥记录
		int count = 0;                                         //总时间
		while(n>3)   //每次过两个人
		{
			int a0,a1,a2,a3; //分别是最快两人和最慢的两人
			a0=getMinFromList(before);a1=getMinFromList(before);a3=getMaxFromList(before);a2=getMaxFromList(before);
			before.add(a0);before.add(a1);before.add(a2);before.add(a3);//此处只是取值比较,实际上并未过桥,所以把这四个速度再加进去before中
			//比较两种过河方式耗时
			if(a0+2*a1+a3<=2*a0+a2+a3)  //方式一:AB,A,YZ,B
			{
				int a=getMinFromList(before);
				int b=getMinFromList(before);
				after.add(a);after.add(b);
				track.add(String.valueOf(a)+' '+String.valueOf(b));
				count+=b;      //AB
				
				a=getMinFromList(after);
				before.add(a);
				track.add(String.valueOf(a));
				count+=a;    //A
				
				int z=getMaxFromList(before);
				int y=getMaxFromList(before);
				after.add(y);after.add(z);
				track.add(String.valueOf(y)+' '+String.valueOf(z));
				count+=z;  //YZ
				
				b=getMinFromList(after);
				before.add(b);
				track.add(String.valueOf(b));
				count+=b;    //B			
			}
			else          //方式二:AZ,A,AY,A
			{
				int a=getMinFromList(before);
				int z=getMaxFromList(before);
				after.add(a);after.add(z);
				track.add(String.valueOf(a)+' '+String.valueOf(z));
				count+=z;  //AZ
				
				a=getMinFromList(after);
				before.add(a);
				track.add(String.valueOf(a));
				count+=a; //A
				
				a=getMinFromList(before);
				int y=getMaxFromList(before);
				after.add(a);after.add(y);
				track.add(String.valueOf(a)+' '+String.valueOf(y));
				count+=y; //AY
				
				a=getMinFromList(after);
				before.add(a);
				track.add(String.valueOf(a));
				count+=a; //A
			}
			n-=2;  //更新未过桥人数
		}
		if(n==3)
		{
			int n3A=getMinFromList(before);
			int n3B=getMinFromList(before);
			int n3C=getMinFromList(before);
			track.add(String.valueOf(n3A)+' '+String.valueOf(n3B));
			track.add(String.valueOf(n3A));
			track.add(String.valueOf(n3A)+' '+String.valueOf(n3C));
			after.add(n3A);after.add(n3B);after.add(n3C);
			count+=n3A+n3B+n3C;
		}
		else if(n==2)
		{
			int n2A=getMinFromList(before);
			int n2B=getMinFromList(before);
			track.add(String.valueOf(n2A)+' '+String.valueOf(n2B));
			after.add(n2A);after.add(n2B);
			count+=n2B;
		}
		else if(n==1)
		{
			int n1A=getMinFromList(before);
			track.add(String.valueOf(n1A));
			after.add(n1A);
			count+=n1A;
		}
	
		System.out.println(count); 
		for(int i=0;i<track.size();i++)
		{
			System.out.println(track.get(i));
		}        //打印结果
		System.out.println("");  //每一种案例之间要隔开

	}
	private static int getMinFromList(ArrayList<Integer> list)//找到最小值并删除
	{
		ArrayList<Integer> toBeSorted= new ArrayList<Integer>();
		toBeSorted.addAll(list);
		toBeSorted.sort(null);
		int temp = toBeSorted.get(0);
		int index = list.indexOf(temp);
		list.remove(index);
		return temp;	
	}
	private static int getMaxFromList(ArrayList<Integer> list)//找到最大值并删除
	{
		ArrayList<Integer> toBeSorted= new ArrayList<Integer>();
		toBeSorted.addAll(list);
		toBeSorted.sort(null);
		int temp = toBeSorted.get(toBeSorted.size()-1);
		int index = list.indexOf(temp);
		list.remove(index);
		return temp;	
	}
}


猜你喜欢

转载自blog.csdn.net/weixin_42416780/article/details/86546251