/
SWAcademy 1873
110 lines (104 loc) · 2.45 KB
/
SWAcademy 1873
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
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
static char[][] arr;
static int H = 0, W = 0, x = 0, y = 0;
static int[] dy = { -1, 1, 0, 0 };
static int[] dx = { 0, 0, -1, 1 };
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
StringTokenizer st = new StringTokenizer(br.readLine());
H = Integer.parseInt(st.nextToken());
W = Integer.parseInt(st.nextToken());
arr = new char[H][W];
for (int i = 0; i < H; i++) {
String str = br.readLine();
for (int j = 0; j < W; j++) {
arr[i][j] = str.charAt(j);
if (arr[i][j] == '>' || arr[i][j] == '<' || arr[i][j] == '^' || arr[i][j] == 'v') {
y = i;
x = j;
}
}
}
int N = Integer.parseInt(br.readLine());
String text = br.readLine();
for (int i = 0; i < N; i++) {
switch (text.charAt(i)) {
case 'U':
arr[y][x] = '^';
if (y - 1 >= 0 && arr[y - 1][x] == '.') {
arr[y][x] = '.';
arr[--y][x] = '^';
}
break;
case 'D':
arr[y][x] = 'v';
if (y + 1 < H && arr[y + 1][x] == '.') {
arr[y][x] = '.';
arr[++y][x] = 'v';
}
break;
case 'L':
arr[y][x] = '<';
if (x - 1 >= 0 && arr[y][x - 1] == '.') {
arr[y][x] = '.';
arr[y][--x] = '<';
}
break;
case 'R':
arr[y][x] = '>';
if (x + 1 < W && arr[y][x + 1] == '.') {
arr[y][x] = '.';
arr[y][++x] = '>';
}
break;
case 'S':
shot(y, x);
break;
default:
break;
}
}
System.out.print("#"+t+" ");
for (int ki = 0; ki < H; ki++) {
for (int j = 0; j < W; j++) {
System.out.print(arr[ki][j]);
}
System.out.println();
}
}
}
private static void shot(int y, int x) {
int ny = y;
int nx = x;
while (true) {
if (arr[y][x] == '^') {
nx = nx + dx[0];
ny = ny + dy[0];
} else if (arr[y][x]== 'v') {
nx = nx + dx[1];
ny = ny + dy[1];
} else if (arr[y][x] == '<') {
nx = nx + dx[2];
ny = ny + dy[2];
} else if (arr[y][x] == '>') {
nx = nx + dx[3];
ny = ny + dy[3];
}
if (ny >= H || ny < 0 || nx >= W|| nx < 0)
break;
if (arr[ny][nx] == '#') {
break;
}
else if(arr[ny][nx] == '*') {
arr[ny][nx] = '.';
break;
}
}
}
}