Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.PrintWriter;
  6. import java.util.StringTokenizer;
  7.  
  8. public class checklist {
  9.     public static void main(String[]args) throws Exception {
  10.         BufferedReader b = new BufferedReader(new FileReader("checklist.in"));
  11.         StringTokenizer s = new StringTokenizer(b.readLine());
  12.         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("checklist.out")));
  13.         int H = Integer.parseInt(s.nextToken());
  14.         int G = Integer.parseInt(s.nextToken());
  15.  
  16.         Pair[] hPairs = new Pair[H + 1];
  17.         Pair[] gPairs = new Pair[G + 1];
  18.  
  19.         for(int i = 1; i <= H; i++) {
  20.             s = new StringTokenizer(b.readLine());
  21.             hPairs[i] = new Pair(Integer.parseInt(s.nextToken()), Integer.parseInt(s.nextToken()));
  22.         }
  23.  
  24.         for(int i = 1; i <= G; i++) {
  25.             s = new StringTokenizer(b.readLine());
  26.             gPairs[i] = new Pair(Integer.parseInt(s.nextToken()), Integer.parseInt(s.nextToken()));
  27.         }
  28.  
  29.         int[][][] dp = new int[H+1][G+1][2];
  30.        
  31.         // initialization
  32.         // set to large number because if you are at 1, 1 you cannot be at H because that would mean you started from G
  33.         dp[1][1][0] = distance(hPairs[1], gPairs[1]);
  34.         dp[0][1][1] = 10000;
  35.         dp[0][0][1] = 10000;
  36.         dp[0][1][0] = 10000;
  37.         dp[0][0][0] = 0;
  38.         dp[1][0][0] = 0;
  39.         dp[1][0][1] = 10000;
  40.         dp[1][1][1] = distance(hPairs[1], gPairs[1]);
  41.        
  42.         // when h = 1
  43.        
  44.         for(int g = 2; g <= G; g++) {
  45.             dp[1][g][1] = dp[1][g - 1][1] + distance(gPairs[g], gPairs[g - 1]);
  46.             dp[1][g][0] = 10000;
  47.         }
  48.        
  49.         for(int h = 2; h <= H; h++) {
  50.             dp[h][1][0] = dp[h - 1][1][0] + distance(hPairs[h], hPairs[h - 1]);
  51.             dp[h][1][1] = 10000;
  52.         }
  53.  
  54.         // 0 = currently at h, 1 = currently at g
  55.         for(int h = 2; h <= H; h++) {
  56.             for(int g = 2; g <= G; g++) {
  57.                 dp[h][g][0] = Math.min(dp[h -1][g][0] + distance(hPairs[h - 1], hPairs[h]),
  58.                         dp[h -1][g][1] + distance(hPairs[h], gPairs[g]));
  59.                 dp[h][g][1] = Math.min(dp[h][g - 1][0] + distance(gPairs[g], hPairs[h]),
  60.                         dp[h][g - 1][1] + distance(gPairs[g - 1], gPairs[g]));
  61.             }
  62.         }
  63.        
  64.         for(int[][] arr: dp) {
  65.             for(int[] arr2: arr) {
  66.                 System.out.print("(");
  67.                 for(int i : arr2) {
  68.                     System.out.print(i + ", ");
  69.                 }
  70.                 System.out.print(")");
  71.             }
  72.             System.out.println();
  73.         }
  74.         System.out.println(dp[H][G][0]);
  75.         out.close();
  76.  
  77.  
  78.     }
  79.  
  80.     static int distance(Pair a, Pair b) {
  81.         return (int)Math.pow(a.x - b.x, 2) + (int)Math.pow(a.y - b.y, 2);
  82.     }
  83.  
  84.     static int min(int a, int b, int c, int d) {
  85.         return Math.min(a, Math.min(b, Math.min(c, d)));
  86.     }
  87.     static class Pair {
  88.         int x, y;
  89.         public Pair(int x, int y) {
  90.             this.x = x;
  91.             this.y = y;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement