-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD3_1873_상호의배틀필드.java
140 lines (132 loc) · 5.18 KB
/
D3_1873_상호의배틀필드.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.swea.D3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class D3_1873_상호의배틀필드 {
static int[][] direction = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; ++t) {
String[] HW = br.readLine().split(" ");
int H = Integer.parseInt(HW[0]);
int W = Integer.parseInt(HW[1]);
char[][] map = new char[H][W];
for (int i = 0; i < H; ++i) {
map[i] = br.readLine().toCharArray();
}
int sR = -1;
int sC = -1;
for (int i = 0; i < H; ++i) {
if (sR != -1 && sC != -1) break;
for (int j = 0; j < W; ++j) {
char c = map[i][j];
if (c == '<' || c == '>' || c == '^' || c == 'v') {
sR = i;
sC = j;
break;
}
}
}
int dir = -1;
switch (map[sR][sC]) {
case '^':
dir = 0;
break;
case '>':
dir = 1;
break;
case 'v':
dir = 2;
break;
case '<':
dir = 3;
break;
}
map[sR][sC] = '.';
int L = Integer.parseInt(br.readLine());
char[] cmd = br.readLine().toCharArray();
for (int i = 0; i < L; ++i) {
char c = cmd[i];
switch (c) {
case 'S':
int tR = sR;
int tC = sC;
while (true) {
tR = tR + direction[dir][0];
tC = tC + direction[dir][1];
if (tR < 0 || tR >= H || tC < 0 || tC >= W) break;
char temp = map[tR][tC];
if (temp == '#') break;
if (temp == '*') {
map[tR][tC] = '.';
break;
}
}
break;
case 'U':
dir = 0;
int nR1 = sR + direction[dir][0];
int nC1 = sC + direction[dir][1];
if (nR1 < 0 || nR1 >= H || nC1 < 0 || nC1 >= W) break;
if (map[sR + direction[dir][0]][sC + direction[dir][1]] == '.') {
sR = sR + direction[dir][0];
sC = sC + direction[dir][1];
}
break;
case 'R':
dir = 1;
int nR2 = sR + direction[dir][0];
int nC2 = sC + direction[dir][1];
if (nR2 < 0 || nR2 >= H || nC2 < 0 || nC2 >= W) break;
if (map[sR + direction[dir][0]][sC + direction[dir][1]] == '.') {
sR = sR + direction[dir][0];
sC = sC + direction[dir][1];
}
break;
case 'L':
dir = 3;
int nR3 = sR + direction[dir][0];
int nC3 = sC + direction[dir][1];
if (nR3 < 0 || nR3 >= H || nC3 < 0 || nC3 >= W) break;
if (map[sR + direction[dir][0]][sC + direction[dir][1]] == '.') {
sR = sR + direction[dir][0];
sC = sC + direction[dir][1];
}
break;
case 'D':
dir = 2;
int nR4 = sR + direction[dir][0];
int nC4 = sC + direction[dir][1];
if (nR4 < 0 || nR4 >= H || nC4 < 0 || nC4 >= W) break;
if (map[sR + direction[dir][0]][sC + direction[dir][1]] == '.') {
sR = sR + direction[dir][0];
sC = sC + direction[dir][1];
}
break;
}
}
switch (dir) {
case 0:
map[sR][sC] = '^';
break;
case 1:
map[sR][sC] = '>';
break;
case 2:
map[sR][sC] = 'v';
break;
case 3:
map[sR][sC] = '<';
break;
}
System.out.print("#" + t + " ");
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
System.out.print(map[i][j]);
}
System.out.println();
}
}
}
}