๐Ÿ‘ฉโ€๐Ÿ’ป SWEA_1873_์ƒํ˜ธ์˜ ๋ฐฐํ‹€ ํ•„๋“œ

๐Ÿ’ฌ ๋‹ค์‹œ ํ’€์–ด๋ณด๋‹ˆ ์‰ฌ์šด ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.


๐Ÿ’ก ํ’€์ด ๋ฐฉ๋ฒ•

  • ์ฃผ์–ด์ง„ ์ง€๋ฌธ์— ๋‚˜์™€์žˆ๋Š”๋ฐ๋กœ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฐœ์‚ฌํ•˜๋Š” ๋ถ€๋ถ„์€ ์ƒ/ํ•˜ ์ขŒ/์šฐ ๋ฅผ ๋‚˜๋ˆ  ๋ฐ˜๋ณตํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

  • ์ด๋™ํ•  ๋•Œ ์ฒ˜๋ฆฌํ•˜๋Š” if๋ฌธ ๋งˆ๋‹ค ๋ฒ”์œ„์— ๋Œ€ํ•œ ์กฐ๊ฑด์„ ๊ฑธ์–ด์ฃผ์–ด ํ•„์š”์—†๋Š” ๊ฒ€์‚ฌ๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.
    - if (start_i + 1 < row && field[start_i + start_d][start_j] == '.')

๐Ÿ”ฅ ์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class SWEA_1873_์ƒํ˜ธ์˜๋ฐฐํ‹€ํ•„๋“œ {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		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());
			int row = Integer.parseInt(st.nextToken());
			int col = Integer.parseInt(st.nextToken());

			char field[][] = new char[row][col];
			int start_i = 0;
			int start_j = 0;
			int start_d = 0;
			int start_n = 0; // ํƒ€์ž…

			for (int i = 0; i < row; i++) {
				String s = br.readLine();
				for (int j = 0; j < col; j++) {
					field[i][j] = s.charAt(j);
					if (s.charAt(j) == '^') {
						start_i = i;
						start_j = j;
						start_d = -1;
						start_n = 1;
					}
					if (s.charAt(j) == 'v') {
						start_i = i;
						start_j = j;
						start_d = 1;
						start_n = 1;
					}
					if (s.charAt(j) == '<') {
						start_i = i;
						start_j = j;
						start_d = -1;
						start_n = 2;
					}
					if (s.charAt(j) == '>') {
						start_i = i;
						start_j = j;
						start_d = 1;
						start_n = 2;
					}
				}
			}

			int num = Integer.parseInt(br.readLine());
			String attack = br.readLine();
			for (int i = 0; i < num; i++) {
				if (attack.charAt(i) == 'S') {
					int nx = start_i;
					int ny = start_j;
					switch (start_n) {
					case 1:
						while (true) {
							nx += start_d;
							if (nx < 0 || nx >= row)
								break;
							if (field[nx][start_j] == '#') {
								break;
							}
							if (field[nx][start_j] == '*') {
								field[nx][start_j] = '.';
								break;
							}
						}
						break;
					case 2:
						while (true) {
							ny += start_d;
							if (ny < 0 || ny >= col)
								break;
							if (field[start_i][ny] == '#') {
								break;
							}
							if (field[start_i][ny] == '*') {
								field[start_i][ny] = '.';
								break;
							}
						}
						break;
					}
				}
				if (attack.charAt(i) == 'U') {
					start_d = -1;
					start_n = 1;
					field[start_i][start_j] = '^';
					if (start_i - 1 >= 0 && field[start_i + start_d][start_j] == '.') {
						field[start_i + start_d][start_j] = field[start_i][start_j];
						field[start_i][start_j] = '.';
						start_i += start_d;
					}
				}
				if (attack.charAt(i) == 'D') {
					start_d = 1;
					start_n = 1;
					field[start_i][start_j] = 'v';
					if (start_i + 1 < row && field[start_i + start_d][start_j] == '.') {
						field[start_i + start_d][start_j] = field[start_i][start_j];
						field[start_i][start_j] = '.';
						start_i += start_d;
					}
				}
				if (attack.charAt(i) == 'L') {
					start_d = -1;
					start_n = 2;
					field[start_i][start_j] = '<';
					if (start_j - 1 >= 0 && field[start_i][start_j + start_d] == '.') {
						field[start_i][start_j + start_d] = field[start_i][start_j];
						field[start_i][start_j] = '.';
						start_j += start_d;
					}
				}
				if (attack.charAt(i) == 'R') {
					start_d = 1;
					start_n = 2;
					field[start_i][start_j] = '>';
					if (start_j + 1 < col && field[start_i][start_j + start_d] == '.') {
						field[start_i][start_j + start_d] = field[start_i][start_j];
						field[start_i][start_j] = '.';
						start_j += start_d;
					}
				}
			}
			System.out.print("#" + T + " ");
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < col; j++) {
					System.out.print(field[i][j]);
				}
				System.out.println();
			}
		}
	}

}

๐Ÿค” ๋Š๋‚€ ์ 

  • ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋งค ๋ฌธ์ œ๋งˆ๋‹ค ๋ฐœ์ƒํ•˜๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•œ ๊ณ ๋ ค๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ