1. 장소별로 순회
  2. 장소 별 한 자리씩 순회
  3. 한 자리가 만약 사람이라면 주변 탐색(맨해튼 거리 기준에 드는 좌석)
  4. 조건문을 통해 거리두기 기준 검사
package kakao.intern2021;

import java.util.Arrays;

public class Q2_1 {
    public static void main(String[] args) {
        String[][] places = {{"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"}};
//        String[][] places = {{"PXXXX", "XPXXX", "XXXPP", "XXXXX", "XXXXX"}};
        System.out.println(Arrays.toString(new Solution().solution(places)));
    }

    static class Solution {
        public int checkPlace(String[][] place, int row, int col) {
            if (row - 1 >= 0 && place[row - 1][col].equals("P")) {
                // 상1
                return 0;
            }
            if (row + 1 < place.length && place[row + 1][col].equals("P")) {
                // 하1
                return 0;
            }
            if (col - 1 >= 0 && place[row][col - 1].equals("P")) {
                // 좌1
                return 0;
            }
            if (col + 1 < place.length && place[row][col + 1].equals("P")) {
                // 우1
                return 0;
            }

            // 맨해튼 거리 2에 사람이 앉으려면 그 사이는 빈테이블이면 안된다.
            if (row - 2 >= 0 && place[row - 2][col].equals("P") && !place[row - 1][col].equals("X")) {
                // 상2
                return 0;
            }
            if (row + 2 < place.length && place[row + 2][col].equals("P") && !place[row + 1][col].equals("X")) {
                // 하2
                return 0;
            }
            if (col - 2 >= 0 && place[row][col - 2].equals("P") && !place[row][col - 1].equals("X")) {
                // 좌2
                return 0;
            }
            if (col + 2 < place.length && place[row][col + 2].equals("P") && !place[row][col + 1].equals("X")) {
                // 우2
                return 0;
            }
            // 대각선
            if ((row - 1 >= 0 && col - 1 >= 0 && place[row - 1][col - 1].equals("P")) && !(place[row - 1][col].equals("X") && place[row][col - 1].equals("X"))) {
                // 좌상
                return 0;
            }
            if (row + 1 < place.length && col - 1 >= 0 && place[row + 1][col - 1].equals("P") && !(place[row + 1][col].equals("X") && place[row][col - 1].equals("X"))) {
                // 좌하
                return 0;
            }

            if (row - 1 >= 0 && col + 1 < place.length && place[row - 1][col + 1].equals("P") && !(place[row - 1][col].equals("X") && place[row][col + 1].equals("X"))) {
                // 우상
                return 0;
            }

            if (row + 1 < place.length && col + 1 < place.length && place[row + 1][col + 1].equals("P") && !(place[row + 1][col].equals("X") && place[row][col + 1].equals("X"))) {
                // 우하
                return 0;
            }
            return 1;
        }

        public int[] solution(String[][] places) {
            int[] answer = new int[5];
            for (int a = 0; a < places.length; a++) {
                int check = 1;
                // 자료 구조 변경
                String[][] room = new String[5][5];
                for (int i = 0; i < places[a].length; i++) {
                    room[i] = places[a][i].split("");
                }
                // 한 자리 씩 순회
                loop:
                for (int i = 0; i < room.length; i++) {
                    for (int j = 0; j < room[i].length; j++) {
                        if (room[i][j].equals("P")) {
                            check = checkPlace(room, i, j);
                            if (check == 0)
                                break loop;
                        }
                    }
                }
                // 검사 결과 저장
                answer[a] = check;
            }
            return answer;
        }
    }

→ “P” 를 기준으로 조건문을 구성하면, 조건문이 너무 복잡하고 예외 경우를 생각하기 힘듬

  1. O를 기준으로 주변에 P가 두개 이상이면 조건 불만족
  2. P를 기준으로 주변에 P가 하나 이상이면 조건 불만족
package kakao.intern2021;

import java.util.Arrays;

public class Q2_2 {
    public static void main(String[] args) {
//        String[][] places = {{"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"}};
//        String[][] places = {{"PXXXX", "XPXXX", "XXXPP", "XXXXX", "XXXXX"}};
        String[][] places = {{"OOPOO", "OPOOO", "OOOOO", "OOOOO", "OOOOO"}};
        System.out.println(Arrays.toString(new Solution().solution(places)));
    }

    static class Solution {

        boolean isPossible(int row, int col) {
            return row >= 0 && row < 5 && col >= 0 && col < 5;
        }

        public int[] solution(String[][] places) {
            int[] answer = new int[5];
            // 좌우상하
            int[] rowMove = {1, -1, 0, 0}, colMove = {0, 0, 1, -1};

            for (int a = 0; a < places.length; a++) {
                int check = 1;
                // 자료 구조 변경
                System.out.println(Arrays.toString(places[a]));
                String[][] room = new String[5][5];
                for (int i = 0; i < places[a].length; i++) {
                    room[i] = places[a][i].split("");
                }
                System.out.println(Arrays.deepToString(room));
                // 한 자리 씩 순회
                loop:
                for (int row = 0; row < room.length; row++) {
                    for (int col = 0; col < room[row].length; col++) {
                        // O 상하좌우에 P가 두개 이상 있으면 안된다.
                        if (room[row][col].equals("O")) {
                            int cnt = 0;
                            for (int x = 0; x < 4; x++) {
                                int nextRow = row + rowMove[x];
                                int nextCol = col + colMove[x];
                                if (isPossible(nextRow, nextCol) && room[nextRow][nextCol].equals("P"))
                                    cnt++;
                            }
                            if (cnt >= 2) {
                                check = 0;
                                break loop;
                            }
                            // P 상하좌우에 P가 하나라도 있으면 안된다.
                        } else if (room[row][col].equals("P")) {
                            for (int x = 0; x < 4; x++) {
                                int nextRow = row + rowMove[x];
                                int nextCol = col + colMove[x];
                                if (isPossible(nextRow, nextCol) && room[nextRow][nextCol].equals("P")) {
                                    check = 0;
                                    break loop;
                                }

                            }
                        }
                    }
                }
                // 검사 결과 저장
                answer[a] = check;
            }
            return answer;
        }
    }
}