Skip to content

Commit 987536c

Browse files
committed
[Simulation] swea-1873
1 parent aae0ce7 commit 987536c

File tree

5 files changed

+3070
-0
lines changed

5 files changed

+3070
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
| 03 | | [Baekjoon-15662 톱니바퀴 (2)](./src/Simulation/P15662) | |
3232
| 04 | | [Baekjoon-12100 2048 (Easy)](./src/Simulation/P12100) | 삼성 SW 역량 테스트 기출 |
3333
| 05 | | [Baekjoon-1244 스위치 켜고 끄기](./src/Simulation/P1244) | |
34+
| 06 | | [SWEA-1873 상호의 배틀필드](./src/Simulation/swea1873) | |
3435

3536
## String
3637

Diff for: src/Simulation/swea1873/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [SW Expert Academy - 1873] 상호의 배틀필드
2+
3+
![image](https://user-images.githubusercontent.com/22045163/106717486-25537300-6643-11eb-9d19-2f0c7b437f71.png)
4+
![image](https://user-images.githubusercontent.com/22045163/106717556-3bf9ca00-6643-11eb-836b-c1cfd1add657.png)
5+
![image](https://user-images.githubusercontent.com/22045163/106717429-11a80c80-6643-11eb-9ece-86ba7046881a.png)

Diff for: src/Simulation/swea1873/Solution.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package Simulation.swea1873;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Solution {
7+
8+
static int T, H, W, N;
9+
static char[][] map;
10+
static int[] cur;
11+
12+
static int[] di = {-1, 1, 0, 0};
13+
static int[] dj = {0, 0, -1, 1};
14+
static String dirs = "^v<>UDLR";
15+
16+
public static void main(String[] args) throws Exception {
17+
System.setIn(new FileInputStream("src/Simulation/swea1873/input.txt"));
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
20+
21+
T = Integer.parseInt(br.readLine());
22+
for (int t = 1; t <= T; t++) {
23+
StringTokenizer st = new StringTokenizer(br.readLine());
24+
H = Integer.parseInt(st.nextToken());
25+
W = Integer.parseInt(st.nextToken());
26+
27+
map = new char[H][W];
28+
for (int i = 0; i < H; i++) {
29+
String line = br.readLine();
30+
for (int j = 0; j < W; j++) {
31+
map[i][j] = line.charAt(j);
32+
if (dirs.indexOf(map[i][j]) >= 0) cur = new int[]{i, j, dirs.indexOf(map[i][j])};
33+
}
34+
}
35+
36+
N = Integer.parseInt(br.readLine());
37+
String cmds = br.readLine();
38+
39+
for (int i = 0; i < N; i++) {
40+
char cmd = cmds.charAt(i);
41+
if (cmd == 'S') {
42+
int to_i = cur[0], to_j = cur[1];
43+
while (true) {
44+
to_i += di[cur[2]];
45+
to_j += dj[cur[2]];
46+
if (!isValidPath(to_i, to_j)) break;
47+
if (map[to_i][to_j] == '*') { map[to_i][to_j] = '.'; break; }
48+
if (map[to_i][to_j] == '#') break;
49+
}
50+
} else {
51+
cur[2] = dirs.indexOf(cmd) % 4;
52+
map[cur[0]][cur[1]] = '.';
53+
int to_i = cur[0] + di[cur[2]], to_j = cur[1] + dj[cur[2]];
54+
if (isValidPath(to_i, to_j) && map[to_i][to_j] == '.') {
55+
cur[0] = to_i;
56+
cur[1] = to_j;
57+
}
58+
map[cur[0]][cur[1]] = dirs.charAt(cur[2]);
59+
}
60+
}
61+
62+
bw.write("#" + t + " ");
63+
for (int i = 0; i < H; i++) {
64+
for (int j = 0; j < W; j++) bw.write(map[i][j]);
65+
bw.newLine();
66+
}
67+
}
68+
69+
bw.flush();
70+
bw.close();
71+
br.close();
72+
}
73+
74+
static boolean isValidPath(int i, int j) { return 0 <= i && i < H && 0 <= j && j < W; }
75+
}

0 commit comments

Comments
 (0)