본문 바로가기
알고리즘/프로그래머스

프로그래머스 거리두기 확인하기

by 김어찐 2021. 8. 30.
728x90
package prog;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class 거리두기_확인하기 {
    public static void main(String[] args) {
        Solution s = new Solution();
        int[] answer =s.solution(new String[][] {
                {"POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"},
                {"POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"},
                {"PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"},
                {"OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"},
                {"PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"}});
        System.out.println("Arrays.toString(answer) = " + Arrays.toString(answer));
    }
}


class Solution {
    static int[][] dMove={{-1,0},{0,1},{1,0},{0,-1}};

    public int[] solution(String[][] places) {


        int tc =places.length;
        int[] answer = new int[tc];
        Arrays.fill(answer,1);
        loop : for (int test_case = 0; test_case < tc; test_case++) {

            int row =places[0].length;
            int col = places[0][0].length();
            char[][] board = new char[row][col];

            for (int i = 0; i < row; i++) {
                board[i] = places[test_case][i].toCharArray();

            }
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if(board[i][j]=='P'){
                        if(!checkDistance(board,i,j,row,col)){
                            answer[test_case]=0;
                            continue loop;
                        }
                    }
                }
            }
            answer[test_case]=1;

        }
        return answer;
    }

    private boolean checkDistance(char[][] board, int x, int y,int row,int col) {
        Queue<Check> q = new LinkedList<>();
        boolean[][] check = new boolean[row][col];

        int distance=0;
        q.add(new Check(x,y,distance));
        check[x][y]=true;
        while(!q.isEmpty()){
            Check ck = q.poll();
            if(ck.distance>=2)continue;
            for (int i = 0; i < 4; i++) {
                int nx = ck.x + dMove[i][0];
                int ny = ck.y + dMove[i][1];
                if(nx>=0 && nx <row && ny>=0 && ny < col ){
                    if(board[nx][ny]=='O' && !check[nx][ny]){
                        check[nx][ny]=true;
                        q.add(new Check(nx,ny,ck.distance+1));
                    }
                    else if(!check[nx][ny] && board[nx][ny]=='P') {
                        return false;
                    }
                }

            }
        }
        return true;
    }
}

class Check{
    int x;
    int y;
    int distance;

    public Check(int x, int y, int distance) {
        this.x = x;
        this.y = y;
        this.distance = distance;
    }
}
728x90

'알고리즘 > 프로그래머스' 카테고리의 다른 글

카카오 프렌즈 컬러링북  (0) 2021.09.03
직업군 추천하기  (0) 2021.09.03
오픈채팅방  (0) 2021.09.03
문자열 압축  (0) 2021.09.03
2주차  (0) 2021.08.31