COMP2396
Loading...
Searching...
No Matches
Question2.java
Go to the documentation of this file.
1package assignment1;
2import java.io.*;
3import java.util.*;
4
5class ProblemSolver {
6 int max_section;
7 int time_cost_in_road_switching;
8 String[] first_road;
9 String[] second_road;
10 ProblemSolver(int max_section, int time_cost_in_road_switching,
11 String[] first_road, String[] second_road) {
12 this.max_section = max_section;
13 this.time_cost_in_road_switching = time_cost_in_road_switching;
14 this.first_road = first_road;
15 this.second_road = second_road;
16 }
17 enum Road {
19 CastlePeakRoad
20 }
21 public int minimum_time_recursively(int time_costed, int current_section, ArrayList<Road> roads) {
22 if (current_section == max_section + 1) {
23 return time_costed;
24 }
25 else {
26 int result = 0;
27 Road current_road = roads.get(current_section);
28 if (current_road == Road.TuenMunRoad) {
29 int time_costed_in_TuenMunRoad = Integer.parseInt(first_road[current_section]);
30 int time_costed_in_CastlePeakRoad =
31 Integer.parseInt(second_road[current_section]) + time_cost_in_road_switching;
32 ArrayList<Road> first_copy = new ArrayList<>(List.copyOf(roads));
33 ArrayList<Road> second_copy = new ArrayList<>(List.copyOf(roads));
34 first_copy.add(Road.TuenMunRoad);
35 second_copy.add(Road.CastlePeakRoad);
36 result = Math.min(
37 minimum_time_recursively(
38 time_costed + time_costed_in_TuenMunRoad,
39 current_section + 1,
40 first_copy),
41 minimum_time_recursively(
42 time_costed + time_costed_in_CastlePeakRoad,
43 current_section + 1,
44 second_copy)
45 );
46 }
47 if (current_road == Road.CastlePeakRoad) {
48 int time_costed_in_TuenMunRoad =
49 Integer.parseInt(first_road[current_section]) + time_cost_in_road_switching;
50 int time_costed_in_CastlePeakRoad = Integer.parseInt(second_road[current_section]);
51 ArrayList<Road> first_copy = new ArrayList<>(List.copyOf(roads));
52 ArrayList<Road> second_copy = new ArrayList<>(List.copyOf(roads));
53 first_copy.add(Road.TuenMunRoad);
54 second_copy.add(Road.CastlePeakRoad);
55 result = Math.min(
56 minimum_time_recursively(
57 time_costed + time_costed_in_TuenMunRoad,
58 current_section + 1,
59 first_copy),
60 minimum_time_recursively(
61 time_costed + time_costed_in_CastlePeakRoad,
62 current_section + 1,
63 second_copy)
64 );
65 }
66 return result;
67 }
68 }
69}
70
71public class Question2 {
72 private static ProblemSolver getProblemSolver() throws IOException {
73 InputStreamReader reader = new InputStreamReader(System.in);
74 BufferedReader buffer = new BufferedReader(reader);
75 int num_road_interchanges = Integer.parseInt(buffer.readLine());
76 int time_cost_in_road_switching = Integer.parseInt(buffer.readLine());
77 String[] time_TuenMunRoads = buffer.readLine().split(" ");
78 String[] time_CastlePeakRoads = buffer.readLine().split(" ");
79
80 return new ProblemSolver(num_road_interchanges ,
81 time_cost_in_road_switching, time_TuenMunRoads, time_CastlePeakRoads);
82 }
83 public static void main(String[] args) throws IOException {
84 ProblemSolver problem = getProblemSolver();
85 System.out.println("The minimum time needed is "
86 + Math.min(
87 problem.minimum_time_recursively(0,
88 0, new ArrayList<>(List.of(ProblemSolver.Road.TuenMunRoad))),
89 problem.minimum_time_recursively(0,
90 0, new ArrayList<>(List.of(ProblemSolver.Road.CastlePeakRoad)))
91 ) + "."
92 );
93 }
94}
static void main(String[] args)
static ProblemSolver getProblemSolver()