-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem02_JediGalaxy.java
82 lines (68 loc) · 2.52 KB
/
Problem02_JediGalaxy.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Problem02_JediGalaxy {
static int[][] matrix;
static long jediSum = 0L;
static int rows;
static int cols;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] dimensions = reader.readLine().split(" ");
rows = Integer.valueOf(dimensions[0]);
cols = Integer.valueOf(dimensions[1]);
matrix = new int[rows][cols];
buildMatrix();
while (true) {
String input = reader.readLine();
if (input.equals("Let the Force be with you")) break;
String[] coordinates = input.split(" ");
int jediRow = Integer.valueOf(coordinates[0]);
int jediCol = Integer.valueOf(coordinates[1]);
coordinates = reader.readLine().split(" ");
int evilRow = Integer.valueOf(coordinates[0]);
int evilCol = Integer.valueOf(coordinates[1]);
// process the dark path
if (evilRow >= rows) {
int shiftValue = evilRow - rows + 1;
evilRow -= shiftValue;
evilCol -= shiftValue;
}
if (evilCol >= cols) {
int shiftValue = evilCol - cols + 1;
evilRow -= shiftValue;
evilCol -= shiftValue;
}
while (evilRow >= 0 && evilCol >= 0) {
matrix[evilRow][evilCol] = 0;
evilRow--;
evilCol--;
}
//process the jedi path
if (jediRow >= rows) {
int shiftValue = jediRow - rows + 1;
jediRow -= shiftValue;
jediCol += shiftValue;
}
if (jediCol < 0) {
int shiftValue = Math.abs(jediCol);
jediRow -= shiftValue;
jediCol += shiftValue;
}
while (jediRow >= 0 && jediCol < cols) {
jediSum += matrix[jediRow][jediCol];
jediRow--;
jediCol++;
}
}
System.out.println(jediSum);
}
static void buildMatrix() {
int counter = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
matrix[row][col] = counter++;
}
}
}
}