[프로그래머스] 안전지대

package org.example.알고리즘.안전지대;

import java.util.Arrays;

class Solution {
    public static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
    
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.solution(new int[][]{
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0},
                {0, 0, 1, 0, 0},
                {0, 0, 0, 0, 0},
        }));
    }
    
    public int solution(int[][] board) {
        System.out.println("board = " + Arrays.deepToString(board));
        int totalCount = board.length * board[0].length;
        int boomCount = 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == 1) {
                    boomCount++;
                    boomCount += boom(board, i, j);
                }
            }
        }
        
        return totalCount - boomCount;
    }
    
    private int boom(int[][] board, int i, int j) {
        int boomCount = 0;
        for (int k = 0; k < 8; k++) {
            int[] direction = DIRECTIONS[k];
            int nextRow = i + direction[0];
            int nextCol = j + direction[1];
            if (!(0 <= nextRow && nextRow < board.length) || !(0 <= nextCol && nextCol < board[0].length)) {
                continue;
            }
            if (board[nextRow][nextCol] == 0) {
                // 위험지역 분류
                board[nextRow][nextCol] = 2;
                boomCount++;
            }
        }
        
        return boomCount;
    }
}
  • easy
    • 2차원 배열 순회
      • 폭탄 발견시 모든 방향으로 위험지역(2) 설정만 해주고 카운트 하면됨


Uploaded by N2T