SWEA1873 상호의 배틀필드

·2022년 4월 18일
0

SWEA 알고리즘

목록 보기
4/29

주어지는 조건에 맞게 구현하면 되는 문제다.
2차원 배열을 다룰 수 있으면 어렵지 않게 풀 수 있다.

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

class Solution {
	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;
	static char[][] map;
	static int x, y, dir; // x,y는 탱크의 좌표. dir[0], 1, 2, 3은 각 상하좌우.
	static int shell_x;
	static int shell_y; // 포탄의 좌표.

	public static void playBattleField(char input) {
		if (input == 'U') { // 위.
			dir = 0;
			map[x][y] = '^';
			move(0);
		} else if (input == 'D') { // 아래.
			dir = 1;
			map[x][y] = 'v';
			move(1);
		} else if (input == 'L') { // 좌.
			dir = 2;
			map[x][y] = '<';
			move(2);
		} else if (input == 'R') { // 우.
			dir = 3;
			map[x][y] = '>';
			move(3);
		} else if (input == 'S') { // 발사.
			shell_x = x;
			shell_y = y;
			shell_move(dir);
		}
	}

	private static void shell_move(int dir) {
		switch (dir) {
		case 0:
			while (true) {
				if (shell_x - 1 >= 0) {
					shell_x--;
					if (map[shell_x][y] == '*') { // 포탄이 벽돌에 충돌할 때.
						map[shell_x][y] = '.';
						break;
					} else if (map[shell_x][y] == '#') { // 강철에 충돌할 때.
						break;
					} else { // 나머지.
						continue;
					}
				} else { // 맵 밖으로 벗어났을 때.
					break;
				}
			}
			break;
		case 1:
			while (true) {
				if (shell_x + 1 < map.length) {
					shell_x++;
					if (map[shell_x][y] == '*') { // 포탄이 벽돌에 충돌할 때.
						map[shell_x][y] = '.';
						break;
					} else if (map[shell_x][y] == '#') { // 강철에 충돌할 때.
						break;
					} else { // 나머지.
						continue;
					}
				} else { // 맵 밖으로 벗어났을 때.
					break;
				}
			}
			break;
		case 2:
			while (true) {
				if (shell_y - 1 >= 0) {
					shell_y--;
					if (map[x][shell_y] == '*') { // 포탄이 벽돌에 충돌할 때.
						map[x][shell_y] = '.';
						break;
					} else if (map[x][shell_y] == '#') { // 강철에 충돌할 때.
						break;
					} else { // 나머지.
						continue;
					}
				} else { // 맵 밖으로 벗어났을 때.
					break;
				}
			}
			break;
		case 3:
			while (true) {
				if (shell_y + 1 < map[x].length) {
					shell_y++;
					if (map[x][shell_y] == '*') { // 포탄이 벽돌에 충돌할 때.
						map[x][shell_y] = '.';
						break;
					} else if (map[x][shell_y] == '#') { // 강철에 충돌할 때.
						break;
					} else { // 나머지.
						continue;
					}
				} else { // 맵 밖으로 벗어났을 때.
					break;
				}
			}
			break;
		}
	}

	private static void move(int dir) {
		switch (dir) {
		case 0:
			if (x - 1 >= 0 && map[x - 1][y] == '.') {
				map[x][y] = '.';
				x--;
				map[x][y] = '^';
			}
			break;
		case 1:
			if (x + 1 < map.length && map[x + 1][y] == '.') {
				map[x][y] = '.';
				x++;
				map[x][y] = 'v';
			}
			break;
		case 2:
			if (y - 1 >= 0 && map[x][y - 1] == '.') {
				map[x][y] = '.';
				y--;
				map[x][y] = '<';
			}
			break;
		case 3:
			if (y + 1 < map[x].length && map[x][y + 1] == '.') {
				map[x][y] = '.';
				y++;
				map[x][y] = '>';
			}
			break;
		}
	}

	private static void findTank() { // x, y, dir을 구함.
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				if (map[i][j] == '^') {
					x = i;
					y = j;
					dir = 0;
				} else if (map[i][j] == 'v') {
					x = i;
					y = j;
					dir = 1;
				} else if (map[i][j] == '<') {
					x = i;
					y = j;
					dir = 2;
				} else if (map[i][j] == '>') {
					x = i;
					y = j;
					dir = 3;
				}
			}
		}
	}

	private static void printmap() throws IOException {
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				bw.write(map[i][j]);
			}
			bw.write("\n");
		}
	}

	public static void main(String args[]) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int T = Integer.parseInt(br.readLine());
		for (int tc = 1; tc <= T; tc++) {
			int H, W;
			st = new StringTokenizer(br.readLine(), " ");
			H = Integer.parseInt(st.nextToken());
			W = Integer.parseInt(st.nextToken());
			map = new char[H][W];

			for (int i = 0; i < H; i++) {
				map[i] = br.readLine().toCharArray();
			}
			findTank();
			int N = Integer.parseInt(br.readLine());
			char[] input = br.readLine().toCharArray();
			for (int i = 0; i < N; i++) {
				playBattleField(input[i]);
				/*
				 * printmap(); System.out.println();
				 */
			}
			bw.write(String.format("#%d ", tc));
			printmap();

		}
		bw.flush();
		bw.close();
	}
}
profile
SSAFY 7기

0개의 댓글