알고리즘

프로그래머스 프렌즈4블록

박카스마시며코딩 2022. 5. 20. 15:15

https://programmers.co.kr/learn/courses/30/lessons/17679

 

코딩테스트 연습 - [1차] 프렌즈4블록

프렌즈4블록 블라인드 공채를 통과한 신입 사원 라이언은 신규 게임 개발 업무를 맡게 되었다. 이번에 출시할 게임 제목은 "프렌즈4블록". 같은 모양의 카카오프렌즈 블록이 2×2 형태로 4개가 붙

programmers.co.kr

 

저는 브루트포스를 이용해 문제를 해결하였습니다.

각각의 시작점에서 4개가 붙어있는지를 체크하고 이들을 다 지웠습니다. 

그 다음으로 gravity 함수를 호출해 모든 블록을 밑으로 내렸습니다.

만약 4개 붙어있는 것이 없다면 다음을 진행하지 않고 몇개 비어있는지를 확인하였습니다.

 

import java.util.*;
class Solution {
    public int solution(int n, int m, String[] board) {
        int answer = 0;
        char[][] map = new char[n][m];
        for(int i = 0 ; i < n ; i++){
            map[i] = board[i].toCharArray();
        }
        while(checkConnected(map,n,m)){
            gravity(map,n,m);
        }
        answer = checkEmpty(map,n,m);
        return answer;
    }
    private int checkEmpty(char[][]map, int n , int m){
        int result = 0;
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < m ; j++){
                if(map[i][j] == EMPTY){
                    result++;
                }
            }
        }
        return result;
    }
    private static final char EMPTY = '0';
    private static final int CHECK_SIZE = 2;
    private void print(char[][] map,int n , int m){
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < m ; j++){
                System.out.print(map[i][j]);
            }
            System.out.println();
        }
    }
    private boolean checkConnected(char[][] map,int n , int m){
        boolean result = false;
        boolean[][] deleteBlock = new boolean[n][m];
        for(int i = 0 ; i < n - CHECK_SIZE + 1 ; i++){
            for(int j = 0 ; j < m - CHECK_SIZE + 1 ; j++){
                if(map[i][j] != EMPTY && checkType(map,i,j)){
                    result = true;
                    deleteBlock[i][j] = true;
                }
            }
        }
        if(!result){
            return false;
        }
        for(int i = 0 ; i < n - CHECK_SIZE + 1 ; i++){
            for(int j = 0 ; j < m - CHECK_SIZE + 1 ; j++){
                if(deleteBlock[i][j]){
                    delete(map,i,j);
                }
            }
        }
        return true;
    }
    private void delete(char[][]map,int y,int x){
        for(int i = y ; i < y + CHECK_SIZE ; i++){
            for(int j = x ; j < x + CHECK_SIZE ; j++){
                map[i][j] = EMPTY;
            }
        }
    }
    
    private boolean checkType(char[][]map,int y,int x){
        char type = map[y][x];
        for(int i = y ; i < y + CHECK_SIZE ; i++){
            for(int j = x ; j < x + CHECK_SIZE ; j++){
                if(map[i][j] != type){
                    return false;
                }
            }
        }
        return true;
    }
    private void gravity(char[][] map,int n,int m){
        for(int row = 0 ; row < m ; row++){
            int emptyIndex = n-1;
            for(int col = n-1 ; col >= 0 ; col--){
                if(map[col][row] != EMPTY){
                    char temp = map[emptyIndex][row];
                    map[emptyIndex][row] = map[col][row];
                    map[col][row] = temp;
                    emptyIndex--;
                }
            }
        }
    }
}