-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSolution.java
43 lines (41 loc) · 1.32 KB
/
Solution.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
import java.util.*;
import java.io.*;
import java.math.*;
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int w = in.nextInt();
int h = in.nextInt();
if (in.hasNextLine()) {
in.nextLine();
}
String[] topLabels = in.nextLine().split(" ");
int labelsCount = topLabels.length;
char[][] grid = new char[labelsCount][h - 2];
for (int i = 0; i < h - 2; i++) {
String line = in.nextLine();
for (int j = 0; j < labelsCount; j++) {
int pipePosition = j * 3;
char direction = 'E';
if (pipePosition > 0 && line.charAt(pipePosition - 1) == '-')
direction = 'L';
else if (pipePosition < w - 1 && line.charAt(pipePosition + 1) == '-')
direction = 'R';
grid[j][i] = direction;
}
}
String[] bottomLabels = in.nextLine().split(" ");
// Find the output
for (int i = 0; i < topLabels.length; i++) {
int currentColumnIndex = i;
for (int j = 0; j < grid[0].length; j++) {
if (grid[currentColumnIndex][j] == 'R')
currentColumnIndex++;
else if (grid[currentColumnIndex][j] == 'L')
currentColumnIndex--;
}
System.out.println(topLabels[i] + "" + bottomLabels[currentColumnIndex]);
}
System.err.println(grid);
}
}