-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob25.java
More file actions
132 lines (115 loc) · 4.97 KB
/
Copy pathprob25.java
File metadata and controls
132 lines (115 loc) · 4.97 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class prob25 {
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(new File("input.txt"));
String[][] map = new String[100][100]; //start from col 1 and row 1
int totalSectors = in.nextInt();
int jumpCount = in.nextInt();
String[] jumps = new String[jumpCount];
int maxX = 0; int maxY = 0;
for(int i = 0; i < totalSectors; i++) {
String coord = in.next();
String sectorName = in.next();
int yCoord = Integer.parseInt(coord.substring(0, 2));
int xCoord = Integer.parseInt(coord.substring(2, 4));
if(xCoord > maxX)
maxX = xCoord;
if(yCoord > maxY)
maxY = yCoord;
map[xCoord][yCoord] = sectorName;
}
in.nextLine();
for(int i = 0; i < jumpCount; i++) {
jumps[i] = in.nextLine();
}
in.close();
for(int k = 0; k < jumpCount; k++) {
//process jump names
String[] startEnd = jumps[k].split(" "); //startEnd[0] = start, startEnd[1] = end
int startX = 0, startY = 0, endX = 0, endY = 0;
boolean foundStart = false;
boolean foundEnd = false;
//find coord of start/end
for(int i = 1; i <= maxX; i++) {
for(int j = 1; j <= maxY; j++) {
if(map[i][j] != null) {
if(map[i][j].equals(startEnd[0])) {
startX = j;
startY = i;
foundStart = true;
} else if(map[i][j].equals(startEnd[1])) {
endX = j;
endY = i;
foundEnd = true;
}
}
if(foundEnd && foundStart) {
break;
}
}
if(foundEnd && foundStart) {
break;
}
}
//calculate distance
/*
distance is calculated by adding the diagnal distance between the 2 points and the remaining "axis" (along y or x axis) distance
*/
int distance = 0;
int deltaX = 0;
int deltaY = 0;
if(startX < endX) { //going left to right
//try to find diagonal distance
if(startY < endY) { //going top to down
while(startX+deltaX < endX && startY+deltaY < endY) {
// System.out.println((startX + deltaX) + " " + ((startX + deltaX) % 2 != 0));
if((startX + deltaX) % 2 != 1) {
deltaY++;
}
deltaX++;
}
} else { //going down to top
while(startX+deltaX < endX && startY+deltaY > endY) {
if((startX + deltaX) %2 != 0) {
deltaY--;
}
deltaX++;
}
}
} else { //going right to left
if(startY < endY) { //going top to down
while(startX+deltaX > endX && startY+deltaY < endY) {
if((startX + deltaX) %2 != 1) {
deltaY++;
}
deltaX--;
}
} else { //going down to top
while(startX+deltaX > endX && startY+deltaY > endY) {
if((startX + deltaX) %2 != 0) {
deltaY--;
}
deltaX--;
}
}
}
// System.out.println(deltaX + " " +deltaY);
distance += Math.abs(deltaX); //only one delta needed (since diagonal movement counts as one move)
if(startX + deltaX != endX || startY + deltaY != endY) {
if(startX + deltaX == endX) { //this means on the same column
distance += Math.abs((startY + deltaY) - endY);
} else { //this means on the same row
distance += Math.abs((startX + deltaX) - endX);
}
}
/*
distance += Math.abs(startX-endX); //x distance
if(Math.abs(startY-endY)>0)
distance += Math.abs(startY-endY) - (distance/2); //y distance (the subtraction part here takes diagnals into consideration so that the calculation doesnt "double count" on a sector
*/
System.out.println(startEnd[0] +" "+ startEnd[1] + " " + distance);
}
}
}