-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metro.java
60 lines (44 loc) · 1.45 KB
/
Metro.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.*;
import java.util.*;
public class Metro
{
public static final double DIST = Math.sqrt(20000);
public static void main(String[] argv) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt(); // columns
int m = in.nextInt(); // rows
int diags = in.nextInt();
double dp[][] = new double[m+1][n+1];
while(diags-- > 0) {
int x = in.nextInt();
int y = in.nextInt();
dp[y-1][x-1] = -1;
}
for (int i = n; i >= 0; i--) {
dp[m][i]=100 * (n - i);
}
for (int i = m; i >= 0; i--) {
dp[i][n]=100 * (m - i);
}
//printTable(dp);
for (int i = m-1; i >= 0; i--) {
for (int j = n-1; j >= 0; j--) {
double nDist = Math.min ( dp[i+1][j]+100, dp[i][j+1]+100 );
if (dp[i][j] == -1) {
dp[i][j] = Math.min( nDist, dp[i+1][j+1]+DIST);
}
else {
dp[i][j] = nDist;
}
}
}
System.out.println((long) (dp[0][0]+0.5));
}
public static void printTable(double[][] table) {
System.out.println("------------------");
for (int i = table.length - 1; i>=0; i--) {
System.out.println(Arrays.toString(table[i]));
}
}
}