Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
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
Archives
Today
Total
관리 메뉴

전공공부

[BOJ_16918] 봄버맨 본문

Study/Problem Solving

[BOJ_16918] 봄버맨

monitor 2024. 1. 5. 20:05

설명


로직을 따라서 폭탄을 설치한다. 그리고, 폭탄 설치시 이미 그 전에 설치된 폭탄이라면 다음번에는 폭발 할 수 있게끔 체크 하는 로직을 추가했다.

코드


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

public class Main {
    static char[][] map;
    static boolean[][] check;
    static int R, C, N;

    static int dx[] = {-1, 0, 1, 0};
    static int dy[] = {0, -1, 0, 1};

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());

        R = Integer.parseInt(tk.nextToken()); // 행
        C = Integer.parseInt(tk.nextToken()); // 열
        N = Integer.parseInt(tk.nextToken()); // 초
        map = new char[R][C];
        check = new boolean[R][C];

        for (int i = 0; i < R; i++) {
            map[i] = in.readLine().toCharArray();
        }

        N -= 1;
        for (int t = 1; t <= N; t++) {
            if (t % 2 == 1) {
                checkBombs();
                installBombs();
            } else {
                explodeBombs();
                check = new boolean[R][C];
            }
        }

        printMap();
    }

    private static void checkBombs() {
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                if (map[i][j] == 'O') {
                    check[i][j] = true;
                }
            }
        }
    }

    private static void installBombs() {
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                if (map[i][j] == '.') {
                    map[i][j] = 'O';
                }
            }
        }
    }

    private static void explodeBombs() {
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                if (check[i][j]) {
                    map[i][j] = '.';
                    for (int d = 0; d < 4; d++) {
                        int nx = i + dx[d];
                        int ny = j + dy[d];
                        if (nx >= 0 && nx < R && ny >= 0 && ny < C) {
                            map[nx][ny] = '.';
                        }
                    }
                }
            }
        }
    }

    private static void printMap() {
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                System.out.print(map[i][j]);
            }
            System.out.println();
        }
    }
}

 

 

16918번: 봄버맨

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

www.acmicpc.net

 

'Study > Problem Solving' 카테고리의 다른 글

[BOJ_20366] 같이 눈사람 만들래?  (0) 2024.01.06
[BOJ_1325] 효율적인 해킹  (2) 2024.01.05
[BOJ_22945] 팀 빌딩  (2) 2024.01.05
[BOJ_14940] 쉬운 최단거리  (2) 2024.01.05
[BOJ_20364] 부동산 다툼  (0) 2023.12.31