백준 음식물 피하기 - 1743 [JAVA] - 22년 12월 7일

Denia·2022년 12월 6일
0

코딩테스트 준비

목록 보기
114/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*
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] firstInput = br.readLine().split(" ");
        int row = Integer.parseInt(firstInput[0]);
        int col = Integer.parseInt(firstInput[1]);
        int trashCount = Integer.parseInt(firstInput[2]);

        int[][] trashCoordinates = new int[trashCount][2];
        for (int i = 0; i < trashCount; i++) {
            String[] tempSplits = br.readLine().split(" ");
            for (int j = 0; j < tempSplits.length; j++) {
                trashCoordinates[i][j] = Integer.parseInt(tempSplits[j]);
            }
        }

        System.out.println(ts.solution(row, col, trashCoordinates));
    }
}


class Solution {
    int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    boolean[][] isVisited;
    int maxValue;
    int[][] table;
    private int gRow;
    private int gCol;

    public int solution(int row, int col, int[][] trashCoordinates) {
        gRow = row;
        gCol = col;
        table = new int[row][col];

        for (int[] trashCoordinate : trashCoordinates) {
            table[trashCoordinate[0] - 1][trashCoordinate[1] - 1] = 1;
        }

        maxValue = Integer.MIN_VALUE;
        isVisited = new boolean[row][col];

        Queue<int[]> queue = new LinkedList<>();

        for (int r = 0; r < gRow; r++) {
            for (int c = 0; c < gCol; c++) {
                if (table[r][c] == 0 || isVisited[r][c]) {
                    continue;
                }

                bfs(r, c, queue);
            }
        }

        return maxValue;
    }

    private void bfs(int row, int col, Queue<int[]> queue) {
        int tempCount = 0;

        queue.offer(new int[]{row, col});
        while (!queue.isEmpty()) {
            int[] curCoordinates = queue.poll();
            int curRow = curCoordinates[0];
            int curCol = curCoordinates[1];

            for (int[] dir : directions) {
                int newRow = curRow + dir[0];
                int newCol = curCol + dir[1];
                if (isOutOfTable(newRow, newCol)) {
                    continue;
                }

                if (isVisited[newRow][newCol]) {
                    continue;
                }

                if (table[newRow][newCol] == 0) {
                    continue;
                }

                tempCount++;
                isVisited[newRow][newCol] = true;
                queue.offer(new int[]{newRow, newCol});
            }
        }

        maxValue = Math.max(tempCount, maxValue);
    }

    boolean isOutOfTable(int row, int col) {
        return row < 0 || col < 0 || row >= gRow || col >= gCol;
    }
}
profile
HW -> FW -> Web

0개의 댓글