Java algorithm contest entry classic example - the ants

Problem description
a length L on the n-ants cm stick, each ant crawling or leftward or rightward climb speed of 1 cm / sec.
When two ants collide, both at the same time turn around (turn around time is negligible).
Each ant given initial position and orientation, the position of each ant T seconds after the calculation.

Input format
number of the first input data set behavior. 3 positive integer L, T, n (0≤n≤10 000 ) of the first row of each data; n lines each of the following description of an ant initial position,
wherein the distance x is an integer of ant from the left end of the stick (unit: cm), the letter represents the initial direction (L denotes the left, R represents rightward).

Output Format
For each test, n output lines, sequentially output according to the input position and orientation of each ant (Turning collision is represented).
T seconds before the first has fallen ants sticks (not exactly stick climb edge) output Fell off.

Sample input. 1
2
10. 4. 1
. 1 R & lt
. 5 R & lt
. 3 L
10 R & lt
10 2. 3
. 4 R & lt
. 5 L
. 8 R & lt

Sample output. 1
Case #. 1:
2 Turning
. 6 R & lt
2 Turning
Fell OFF

Case #2:
3 L
6 R
10 R

PS:
When I collided with two ants can be used as two ants go through
while I kept turned around again, but my position has not changed relative
I started to fall was always the most was sidelined two

package 第七次模拟;

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

public class Demo2蚂蚁 {
	public static class Node implements Comparable<Node> {
		int id;// 输入顺序
		int location;// -1,0,1
		int p;// 位置

		@Override
		public int compareTo(Node o) {
			// TODO 自动生成的方法存根
			if (this.p > o.p) {
				return 1;
			} else if (this.p < o.p) {
				return -1;
			}
			return 0;
		}
	}

	public static Node[] start;
	public static Node[] end;
	public static int[] order;
	public static String[] loca = { "L", "Turning", "R" };;

	public static void main(String[] args) {
		int num=1;
		Scanner sc = new Scanner(System.in);
		int count = sc.nextInt();
		while (count-- > 0) {

			int l = sc.nextInt();
			int t = sc.nextInt();
			int n = sc.nextInt();
			start = new Node[n];
			end = new Node[n];
			order = new int[n];
			for (int i = 0; i < n; i++) {
				start[i] = new Node();
				start[i].p = sc.nextInt();
				String c = sc.next();
				if (c.equals("L"))
					start[i].location = -1;
				else
					start[i].location = 1;
				start[i].id = i;
				end[i] = new Node();
				end[i].id = 0;
				end[i].p = start[i].p + t * start[i].location;
				end[i].location = start[i].location;

			}
			Arrays.sort(start);
			Arrays.sort(end);
			for (int j = 0; j < n; j++) {
				order[start[j].id] = j;
			}
			for (int j = 0; j < n - 1; j++) {
				if (end[j].p == end[j + 1].p) {
					end[j].location = 0;
					end[j + 1].location = 0;
				}
			}
			
			System.out.println("Case #"+ num++ +":");
			for (int i = 0; i < n; i++) {
				int temp = order[i];
				if (end[temp].p > l || end[temp].p < 0) {
					System.out.println("Fell off\n");
				} else {
					System.out.println(end[temp].p + " " + loca[end[temp].location + 1]);
				}
			}
		}
	}

}

Released 1370 original articles · won praise 10000 + · views 1.34 million +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104614655