-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon1347.java
104 lines (84 loc) · 2.89 KB
/
Baekjoon1347.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package algo.Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* https://www.acmicpc.net/problem/1347
* 백준 1347 미로만들기
*/
public class Baekjoon1347 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static String[][] map = new String[102][102];
static int currentPositionX = 50;
static int currentPositionY = 50;
static int currentLookAt = 0;
static int[] dx = {1, 0, -1, 0};
static int[] dy = {0, -1, 0, 1};
public static void main(String[] args) throws IOException {
br.readLine();
String input = br.readLine();
Queue<String> queue = new LinkedList<>();
for (int i = 0; i < input.length(); i++) {
queue.add(String.valueOf(input.charAt(i)));
}
map[currentPositionX][currentPositionY] = ".";
while (!queue.isEmpty()) {
String current = queue.poll();
if ("R".equals(current)) {
currentLookAt += 1;
if (currentLookAt == 4) {
currentLookAt = 0;
}
continue;
}
if ("L".equals(current)) {
currentLookAt -= 1;
if (currentLookAt == -1) {
currentLookAt = 3;
}
continue;
}
if ("F".equals(current)) {
currentPositionX += dx[currentLookAt];
currentPositionY += dy[currentLookAt];
if (!".".equals(map[currentPositionX][currentPositionY]))
map[currentPositionX][currentPositionY] = ".";
}
}
List<String> answer = new ArrayList<>();
int length = 0;
int C = 0;
int startX = Integer.MAX_VALUE;
int startY = Integer.MAX_VALUE;
int endY = 0;
for (int i = 0; i < 102; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < 102; j++) {
if (".".equals(map[i][j])) {
sb.append(map[i][j]);
startX = Math.min(startX, j);
C = Math.max(C, j);
}
}
if (sb.length() > 0) {
answer.add(sb.toString());
length++;
startY = Math.min(startY, i);
endY = i;
}
}
for (int i = startY; i < endY + 1; i++) {
String[] strings = Arrays.copyOfRange(map[i], startX, C + 1);
StringBuilder sb = new StringBuilder();
for (int j = 0; j < strings.length; j++) {
if (".".equals(strings[j])) {
sb.append(".");
} else {
sb.append("#");
}
}
System.out.println(sb.toString());
}
}
}