|
| 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