반응형
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

[BOJ/Java] 16918. 봄버맨 본문

알고리즘/구현

[BOJ/Java] 16918. 봄버맨

ImJay 2024. 1. 20. 18:38
반응형

[BOJ/Java] 16918. 봄버맨

 

16918번: 봄버맨

첫째 줄에 R, C, N (1 ≤ R, C, N ≤ 200)이 주어진다. 둘째 줄부터 R개의 줄에 격자판의 초기 상태가 주어진다. 빈 칸은 '.'로, 폭탄은 'O'로 주어진다.

www.acmicpc.net


풀이

import java.io.*;
import java.util.StringTokenizer;

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

        // 초기 입력값 받기
        String input = br.readLine();
        StringTokenizer st = new StringTokenizer(input);

        int r = Integer.parseInt(st.nextToken());
        int c = Integer.parseInt(st.nextToken());
        int n = Integer.parseInt(st.nextToken());

        // 격자판의 초기 상태 및 현재 상태를 저장할 배열 선언
        char[][] graph = new char[r][c];
        char[][] original = new char[r][c];

        // 초기 상태 입력
        for (int x = 0; x < r; x++) {
            String row = br.readLine();
            for (int y = 0; y < c; y++) {
                graph[x][y] = row.charAt(y);
                original[x][y] = row.charAt(y);
            }
        }

        // 초 단위로 반복
        for (int i = 2; i <= n; i++) {
            // 폭탄 설치 및 폭탄이 터지는 시점에 대한 조건문
            int[][] direction = {{0, 0}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
            if (i % 4 == 2 || i % 4 == 0) {
                // 2초 또는 4초일 때, 모든 칸에 폭탄 설치
                for (int x = 0; x < r; x++) {
                    for (int y = 0; y < c; y++) {
                        graph[x][y] = 'O';
                    }
                }
            } else {
                // 폭탄 터지는 시점일 때, 폭탄 터지는 로직 수행
                for (int x = 0; x < r; x++) {
                    for (int y = 0; y < c; y++) {
                        if (original[x][y] == 'O') {
                            for (int d = 0; d < 5; d++) {
                                int newX = x + direction[d][0];
                                int newY = y + direction[d][1];
                                // 주변 5방향으로 폭탄이 터지도록 설정
                                if (0 <= newX && newX < r && 0 <= newY && newY < c) {
                                    graph[newX][newY] = '.';
                                }
                            }
                        }
                    }
                }
                // 현재 상태 갱신
                for (int x = 0; x < r; x++) {
                    for (int y = 0; y < c; y++) {
                        original[x][y] = graph[x][y];
                    }
                }
            }
        }

        // 최종 결과 출력
        for (int x = 0; x < r; x++) {
            for (int y = 0; y < c; y++) {
                sb.append(graph[x][y]);
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
}

 

반응형
Comments