요깨비's LAB

[프로그래머스, Java] 거리두기 확인하기 본문

알고리즘(Java)/프로그래머스

[프로그래머스, Java] 거리두기 확인하기

요깨비 2021. 9. 9. 19:16

import java.util.*;

class Solution {
    public int[] solution(String[][] places) {
        int[] answer = new int[5];
        int caseLen = places.length;

        for (int i = 0; i < 5; i++) {
            char[][] map = new char[5][5];
            List<Participant> participants = new ArrayList<>();

            for (int j = 0; j < 5; j++) {
                char[] carr = places[i][j].toCharArray();

                for (int k = 0; k < 5; k++) {
                    if (carr[k] == 'P') {
                        participants.add(new Participant(j, k));
                    }

                    map[j][k] = carr[k];
                }
            }

            answer[i] = validate(participants, map);
        }

        return answer;
    }

    public int validate(List<Participant> participants, char[][] map) {
        for (Participant participant : participants) {
            Queue<Element> queue = new LinkedList<>();
            boolean[][] isVisited = new boolean[5][5];

            Element element = new Element(participant.row, participant.col);
            isVisited[element.row][element.col] = true;
            queue.add(element);

            while (!queue.isEmpty()) {
                Element polledElement = queue.poll();

                if (map[polledElement.row][polledElement.col] == 'P' && polledElement.count > 0 && polledElement.count <= 2) {
                    return 0;
                }

                if (polledElement.row - 1 >= 0
                        && (map[polledElement.row - 1][polledElement.col] == 'O' || map[polledElement.row - 1][polledElement.col] == 'P')
                        && !isVisited[polledElement.row - 1][polledElement.col]) {
                    isVisited[polledElement.row - 1][polledElement.col] = true;
                    queue.add(new Element(polledElement.row - 1, polledElement.col, polledElement.count + 1));
                }

                if (polledElement.row + 1 < 5
                        && (map[polledElement.row + 1][polledElement.col] == 'O' || map[polledElement.row + 1][polledElement.col] == 'P')
                        && !isVisited[polledElement.row + 1][polledElement.col]) {
                    isVisited[polledElement.row + 1][polledElement.col] = true;
                    queue.add(new Element(polledElement.row + 1, polledElement.col, polledElement.count + 1));
                }

                if (polledElement.col - 1 >= 0
                        && (map[polledElement.row][polledElement.col - 1] == 'O' || map[polledElement.row][polledElement.col - 1] == 'P')
                        && !isVisited[polledElement.row][polledElement.col - 1]) {
                    isVisited[polledElement.row][polledElement.col - 1] = true;
                    queue.add(new Element(polledElement.row, polledElement.col - 1, polledElement.count + 1));
                }

                if (polledElement.col + 1 < 5
                        && (map[polledElement.row][polledElement.col + 1] == 'O' || map[polledElement.row][polledElement.col + 1] == 'P')
                        && !isVisited[polledElement.row][polledElement.col + 1]) {
                    isVisited[polledElement.row][polledElement.col + 1] = true;
                    queue.add(new Element(polledElement.row, polledElement.col + 1, polledElement.count + 1));
                }
            }
        }

        return 1;
    }

    class Participant {
        int row, col;

        public Participant(int row, int col) {
            this.row = row;
            this.col = col;
        }
    }

    class Element {
        int row, col;
        int count = 0;

        public Element(int row, int col) {
            this.row = row;
            this.col = col;
        }

        public Element(int row, int col, int count) {
            this.row = row;
            this.col = col;
            this.count = count;
        }
    }
}
Comments