Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- /**
- * Created by BoldijarPaul on 31/01/2017.
- */
- public class Main {
- public static class Location implements Comparable {
- int x;
- int y;
- double distance;
- int initialIndex;
- Location(int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public int compareTo(Object o) {
- if (o == null || !(o instanceof Location)) {
- return 0;
- }
- Location location = (Location) o;
- if (distance < location.distance) {
- return -1;
- }
- return 1;
- }
- }
- public static class TestCase {
- Location userLocation;
- TreeSet<Location> stores;
- }
- private static double distance(Location location1, Location location2) {
- return Math.sqrt((location1.x - location2.x) * (location1.x - location2.x) +
- (location1.y - location2.y) * (location1.y - location2.y));
- }
- private static Location locationFromString(String locationString) {
- String[] coords = locationString.split("\\s+");
- return new Location(Integer.valueOf(coords[0]), Integer.valueOf(coords[1]));
- }
- public static void main(String[] args) throws IOException {
- List<TestCase> testCaseList = new ArrayList<>();
- BufferedReader reader = new BufferedReader(new FileReader(new File("task1-test-input.txt")));
- int testCases = Integer.valueOf(reader.readLine());
- reader.readLine();
- for (int i = 1; i <= testCases; i++) {
- TestCase testCase = new TestCase();
- testCase.stores = new TreeSet<>();
- testCase.userLocation = locationFromString(reader.readLine());
- int storesCount = Integer.valueOf(reader.readLine());
- for (int j = 1; j <= storesCount; j++) {
- Location storeLocation = locationFromString(reader.readLine());
- storeLocation.distance = distance(storeLocation, testCase.userLocation);
- storeLocation.initialIndex = j;
- testCase.stores.add(storeLocation);
- }
- testCaseList.add(testCase);
- reader.readLine();
- }
- reader.close();
- outputTestCases(testCaseList);
- }
- private static void outputTestCases(List<TestCase> testCaseList) throws FileNotFoundException, UnsupportedEncodingException {
- PrintWriter printWriter = new PrintWriter("task1-test-output.txt", "UTF-8");
- for (int i = 0, testCaseListSize = testCaseList.size(); i < testCaseListSize; i++) {
- TestCase testCase = testCaseList.get(i);
- Location closestLocation = testCase.stores.first();
- printWriter.println(String.format("Case #%d: %d %.3f", i + 1, closestLocation.initialIndex, closestLocation.distance));
- }
- printWriter.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement