반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Archives
Today
Total
05-19 00:03
관리 메뉴

ImJay

[SWEA/Java] 1873. 상호의 배틀필드 본문

SW Expert Academy/D3

[SWEA/Java] 1873. 상호의 배틀필드

ImJay 2024. 1. 24. 22:06
반응형

[SWEA/Java] 1873. 상호의 배틀필드


해설

풀이

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

public class Solution {
    static char[][] graph;
    static int direction, h, w, x, y;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        // 테스트 케이스의 수 입력
        int testCase = Integer.parseInt(br.readLine());

        for (int t = 1; t <= testCase; t++) {
            // 게임 맵의 크기 입력
            String input = br.readLine();
            StringTokenizer st = new StringTokenizer(input);
            h = Integer.parseInt(st.nextToken());
            w = Integer.parseInt(st.nextToken());

            // 게임 맵 초기화
            graph = new char[h][w];
            
            // 게임 맵 상태 입력 및 전차의 시작 위치 찾기
            for (int r = 0; r < h; r++) {
                input = br.readLine();
                for (int c = 0; c < w; c++) {
                    graph[r][c] = input.charAt(c);
                    findStart(r, c);
                }
            }

            // 사용자 입력의 개수 입력
            int n = Integer.parseInt(br.readLine());
            
            // 사용자 입력에 따른 동작 수행
            input = br.readLine();
            for (int i = 0; i < n; i++) {
                runCommand(input.charAt(i));
            }

            // 결과 출력
            sb.append("#" + t + " ");
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    sb.append(graph[i][j]);
                }
                sb.append("\n");
            }
        }
        System.out.print(sb);
    }

    // 전차의 시작 위치 찾기
    public static void findStart(int r, int c) {
        if (graph[r][c] == '^' || graph[r][c] == 'v' || graph[r][c] == '<' || graph[r][c] == '>') {
            x = r;
            y = c;
            switch (graph[r][c]) {
                case '^':
                    direction = 1;
                    break;
                case 'v':
                    direction = 2;
                    break;
                case '<':
                    direction = 3;
                    break;
                case '>':
                    direction = 4;
                    break;
            }
        }
    }

    // 사용자 입력에 따른 동작 수행
    public static void runCommand(char command) {
        switch (command) {
            case 'U':
                direction = 1;
                move();
                break;
            case 'D':
                direction = 2;
                move();
                break;
            case 'L':
                direction = 3;
                move();
                break;
            case 'R':
                direction = 4;
                move();
                break;
            case 'S':
                shoot();
                break;
        }
    }

    // 전차 이동
    public static void move() {
        int newX = x;
        int newY = y;
        char status = 0;

        switch (direction) {
            case 1:
                newX--;
                status = '^';
                break;
            case 2:
                newX++;
                status = 'v';
                break;
            case 3:
                newY--;
                status = '<';
                break;
            case 4:
                newY++;
                status = '>';
                break;
        }

        // 이동 가능한지 확인 후 이동
        if (0 <= newX && newX < h && 0 <= newY && newY < w && graph[newX][newY] == '.') {
            graph[x][y] = '.';
            x = newX;
            y = newY;
        }
        graph[x][y] = status;
    }

    // 포탄 발사
    public static void shoot() {
        switch (direction) {
            case 1:
                for (int i = x - 1; i > -1; i--) {
                    if (graph[i][y] == '*') {
                        graph[i][y] = '.';
                        break;
                    }
                    if (graph[i][y] == '#') {
                        break;
                    }
                }
                break;
            case 2:
                for (int i = x + 1; i < h; i++) {
                    if (graph[i][y] == '*') {
                        graph[i][y] = '.';
                        break;
                    }
                    if (graph[i][y] == '#') {
                        break;
                    }
                }
                break;
            case 3:
                for (int i = y - 1; i > -1; i--) {
                    if (graph[x][i] == '*') {
                        graph[x][i] = '.';
                        break;
                    }
                    if (graph[x][i] == '#') {
                        break;
                    }
                }
                break;
            case 4:
                for (int i = y + 1; i < w; i++) {
                    if (graph[x][i] == '*') {
                        graph[x][i] = '.';
                        break;
                    }
                    if (graph[x][i] == '#') {
                        break;
                    }
                }
                break;
        }
    }
}
반응형
Comments