-
Notifications
You must be signed in to change notification settings - Fork 2
/
PathTracing.java
70 lines (59 loc) · 1.92 KB
/
PathTracing.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
/*
Problem: https://open.kattis.com/problems/pathtracing
Author: Adrian Reithaug
Submitted: December 22nd, 2017
Time: 0.38s / 2.00s
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PathTracing {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int col = 500;
int row = 500;
int minCol = 500;
int maxCol = 500;
int minRow = 500;
int maxRow = 500;
char[][] grid = new char[minCol + maxCol + 1][minRow + maxRow + 1];
grid[col][row] = 'S';
String direction;
while ((direction = reader.readLine()) != null) {
switch (direction.charAt(0)) {
case 'l':
col--;
break;
case 'r':
col++;
break;
case 'u':
row--;
break;
default:
row++;
break;
}
maxRow = (row > maxRow) ? row : maxRow;
minRow = (row < minRow) ? row : minRow;
maxCol = (col > maxCol) ? col : maxCol;
minCol = (col < minCol) ? col : minCol;
if (grid[row][col] != 'S') {
grid[row][col] = '*';
}
}
reader.close();
grid[row][col] = 'E';
for (int i = minRow - 1; i <= maxRow + 1; i++) {
for (int j = minCol - 1; j <= maxCol + 1; j++) {
if ((i == minRow - 1 || i == maxRow + 1) || (j == minCol - 1 || j == maxCol + 1)) {
grid[i][j] = '#';
} else if (grid[i][j] == 0) {
grid[i][j] = ' ';
}
System.out.print(grid[i][j]);
}
System.out.println("");
}
}
}