코딩테스트 연습/프로그래머스 LV.0

[프로그래머스] Lv.0 안전지대 - 자바(Java)

5ein 2024. 5. 6. 21:35

문제: 코딩테스트 연습 - 안전지대 | 프로그래머스 스쿨 (programmers.co.kr)

문제설명

다음 그림과 같이 지뢰가 있는 지역과 지뢰에 인접한 위, 아래, 좌, 우 대각선 칸을 모두 위험지역으로 분류합니다.

지뢰는 2차원 배열 board에 1로 표시되어 있고 board에는 지뢰가 매설 된 지역 1과, 지뢰가 없는 지역 0만 존재합니다.

지뢰가 매설된 지역의 지도 board가 매개변수로 주어질 때, 안전한 지역의 칸 수를 return하도록 solution 함수를 완성해주세요.


제한사항

  • board는 n * n 배열입니다.
  • 1 ≤ n ≤ 100
  • 지뢰는 1로 표시되어 있습니다.
  • board에는 지뢰가 있는 지역 1과 지뢰가 없는 지역 0만 존재합니다.

입출력 예

board result
[[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]] 16
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 0]] 13
[[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]] 0

나의 문제 풀이

class Solution {
    public int solution(int[][] board) {
        int answer = 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if(board[i][j] == 1) {
                    if(i > 0) {
                        if(j > 0) board[i - 1][j - 1] = board[i - 1][j - 1] == 1? 1 : 2;
                        board[i - 1][j] = board[i - 1][j] == 1? 1 : 2;
                        if(j < board[i].length - 1) board[i - 1][j + 1] = board[i - 1][j + 1] == 1? 1 : 2;
                    }
                    if(j > 0) board[i][j - 1] = board[i][j - 1] == 1? 1 : 2;
                    if(j < board[i].length - 1) board[i][j + 1] = board[i][j + 1] == 1? 1 : 2;
                    if(i < board.length - 1) {
                        if(j > 0) board[i + 1][j - 1] = board[i + 1][j - 1] == 1? 1 : 2;
                        board[i + 1][j] = board[i + 1][j] == 1? 1 : 2;
                        if(j < board[i].length - 1) board[i + 1][j + 1] = board[i + 1][j + 1] == 1? 1 : 2;
                    }
                } //board[i][j] == 1
            }
        }
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if(board[i][j] == 0) answer++;
            }
        }
        
        return answer;
    }
}

다른 사람의 문제풀이

//다른사람의풀이1
class Solution {
    public int solution(int[][] board) {
        int answer = 0;
        int length = board.length;   //길이
        int[][] temp = new int[length+2][length+2];
        // 길이를 2 늘린 액자용 배열 생성 -> 이러면 단순한 조건식으로 안전영역 구할 수 있음

        // 액자에 board 이식.
        for(int i=1; i<length+1; i++){
            for(int j=1; j<length+1;j++){
                temp[i][j]=board[i-1][j-1];
            }
        }

        //위험지대 찾기
        for(int i=1; i<length+1; i++){
            for(int j=1; j<length+1;j++){
                if(temp[i][j]==1){
                    for(int a = i-1; a<=i+1; a++){
                        for(int b =j-1; b<=j+1; b++){
                            if(temp[a][b]!=1) temp[a][b]=2;
                        }
                    }
                }
            }
        }

        // 안전지대 카운트
        for(int i=1; i<length+1; i++){
            for(int j=1; j<length+1;j++){
                if(temp[i][j]==0) answer++;
                System.out.print(temp[i][j]);
            }
            System.out.println("");
        }

        return answer;
    }
}

//다른사람의 풀이2
class Solution {

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

    public int solution(int[][] board) {
        int answer = 0;
        int count = 0;

        for (int i=0; i<board.length; i++) {
            for (int j=0; j<board.length; j++) {
                if (board[i][j] == 1) {
                    count++;
                    for (int k=0; k<8; k++) {
                        if (i+dx[k]>=0 && i+dx[k] < board.length && j+dy[k]>=0 && j+dy[k] < board.length && 
                            board[i+dx[k]][j+dy[k]] == 0) {
                            board[i+dx[k]][j+dy[k]] = 2; //visited 배열을 따로 만들지 않고 2를 부여해 check
                            count++;
                        }
                    }
                }
            }
        }

        answer = board.length * board.length - count;

        return answer;
    }
}

//다른사람의 풀이3
class Solution {
    public int solution(int[][] board) {
        int answer = 0;
        for (int i=0; i<board.length; i++) {
            for (int j=0; j<board[0].length; j++) {
                if (i-1 >= 0 && j-1 >= 0 && board[i-1][j-1] == 1) { continue; }
                if (j-1 >= 0 && board[i][j-1] == 1) { continue; }
                if (i+1 <= board.length-1 && j-1 >= 0 && board[i+1][j-1] == 1) { continue; }
                if (i-1 >= 0 && board[i-1][j] == 1) { continue; }
                if (board[i][j] == 1) { continue; }
                if (i+1 <= board.length-1 && board[i+1][j] == 1) { continue; }
                if (i-1 >= 0 && j+1 <= board.length-1 && board[i-1][j+1] == 1) { continue; }
                if (j+1 <= board.length-1 && board[i][j+1] == 1) { continue; }
                if (i+1 <= board.length-1 && j+1 <= board.length-1 && board[i+1][j+1] == 1) { continue; }
                answer++;
            }
        }
        return answer;
    }
}

느낀점

나는 이런 좌표 관련 문제에 약한 것 같다 ㅠㅠ

다른사람의 풀이중 2번째 풀이가 가장 간결하다는 생각이 들었다.

좌표 문제 역량을 키우려면 어떻게 공부를 해야할지 ㅠㅠ 걱정이다..


문제 출처: 코딩테스트 연습 | 프로그래머스 스쿨 (programmers.co.kr)