백준 양 - 3184 [JAVA] - 22년 12월 23일

Denia·2022년 12월 22일
0

코딩테스트 준비

목록 보기
119/201
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static public void main(String[] args) throws IOException {
        Solution ts = new Solution();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] fl = br.readLine().split(" ");
        int R = Integer.parseInt(fl[0]);
        int C = Integer.parseInt(fl[1]);

        char[][] table = new char[R][C];

        for (int i = 0; i < R; i++) {
            char[] input = br.readLine().toCharArray();
            table[i] = input;
        }

        ts.solution(R, C, table);
    }
}

class Solution {
    final char FIELD = '.';
    final char FENCE = '#';
    final char SHEEP = 'o';
    final char WOLF = 'v';
    int gRow;
    int gCol;
    char[][] gTable;
    int sheep;
    int wolf;
    int[][] directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

    public void solution(int R, int C, char[][] table) {
        gRow = R;
        gCol = C;
        gTable = table;
        sheep = 0;
        wolf = 0;

        int[] answer = new int[2];

        for (int row = 0; row < gRow; row++) {
            for (int col = 0; col < gCol; col++) {
                if (table[row][col] != FENCE) {
                    bfs(row, col);
                }
            }
        }

        answer[0] = sheep;
        answer[1] = wolf;

        System.out.printf("%d %d\n", answer[0], answer[1]);
    }

    private void bfs(int row, int col) {
        int tempWolf = 0;
        int tempSheep = 0;

        Queue<Coordination> queue = new LinkedList<>();
        if (gTable[row][col] == WOLF) {
            tempWolf++;
        } else if (gTable[row][col] == SHEEP) {
            tempSheep++;
        }
        gTable[row][col] = FENCE;
        queue.add(new Coordination(row, col));

        while (!queue.isEmpty()) {
            Coordination tempCoordi = queue.poll();
            int cR = tempCoordi.row;
            int cC = tempCoordi.col;

            for (int i = 0; i < directions.length; i++) {
                int newRow = cR + directions[i][0];
                int newCol = cC + directions[i][1];

                if (isOutOfTable(newRow, newCol)) continue;

                if (gTable[newRow][newCol] != FENCE) {
                    if (gTable[newRow][newCol] == WOLF) {
                        tempWolf++;
                    } else if (gTable[newRow][newCol] == SHEEP) {
                        tempSheep++;
                    }
                    gTable[newRow][newCol] = FENCE;
                    queue.add(new Coordination(newRow, newCol));
                }
            }
        }

        if (tempWolf < tempSheep) {
            sheep += tempSheep;
        } else {
            wolf += tempWolf;
        }
    }


    private boolean isOutOfTable(int row, int col) {
        return row < 0 || row >= gRow || col < 0 || col >= gCol;
    }
}

class Coordination {
    int row;
    int col;

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

profile
HW -> FW -> Web

0개의 댓글