Iriton's log

[Java/BOJ] 3184번: 양 본문

Problem Solving/Java

[Java/BOJ] 3184번: 양

Iriton 2023. 9. 26. 11:10

문제


미키의 뒷마당에는 특정 수의 양이 있다. 그가 푹 잠든 사이에 배고픈 늑대는 마당에 들어와 양을 공격했다.

마당은 행과 열로 이루어진 직사각형 모양이다. 글자 '.' (점)은 빈 필드를 의미하며, 글자 '#'는 울타리를, 'o'는 양, 'v'는 늑대를 의미한다.

한 칸에서 수평, 수직만으로 이동하며 울타리를 지나지 않고 다른 칸으로 이동할 수 있다면, 두 칸은 같은 영역 안에 속해 있다고 한다. 마당에서 "탈출"할 수 있는 칸은 어떤 영역에도 속하지 않는다고 간주한다.

다행히 우리의 양은 늑대에게 싸움을 걸 수 있고 영역 안의 양의 수가 늑대의 수보다 많다면 이기고, 늑대를 우리에서 쫓아낸다. 그렇지 않다면 늑대가 그 지역 안의 모든 양을 먹는다.

맨 처음 모든 양과 늑대는 마당 안 영역에 존재한다.

아침이 도달했을 때 살아남은 양과 늑대의 수를 출력하는 프로그램을 작성하라.

 

입력


첫 줄에는 두 정수 R과 C가 주어지며(3 ≤ R, C ≤ 250), 각 수는 마당의 행과 열의 수를 의미한다.

다음 R개의 줄은 C개의 글자를 가진다. 이들은 마당의 구조(울타리, 양, 늑대의 위치)를 의미한다.

 

출력


하나의 줄에 아침까지 살아있는 양과 늑대의 수를 의미하는 두 정수를 출력한다.

 

코드


import java.util.Scanner;

public class Main {
    static int[] dx = {0, 1, 0, -1}; // 상하좌우
    static int[] dy = {1, 0, -1, 0};
    
    static int rows, cols;
    
    // DFS를 이용하여 양과 늑대의 수를 계산
    static int[] dfs(char[][] graph, boolean[][] visited, int x, int y) {
        int sheepCount = 0;
        int wolfCount = 0;
        
        if (graph[x][y] == 'o')
            sheepCount++;
        else if (graph[x][y] == 'v')
            wolfCount++;
        
        visited[x][y] = true;
        
        // 상하좌우로 이동
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            
            if (nx >= 0 && ny >= 0 && nx < rows && ny < cols && !visited[nx][ny] && graph[nx][ny] != '#') {
                int[] counts = dfs(graph, visited, nx, ny);
                sheepCount += counts[0];
                wolfCount += counts[1];
            }
        }
        
        int[] result = {sheepCount, wolfCount};
        return result;
    }
    
    // 양과 늑대의 수를 계산하는 함수
    static int[] countSheepAndWolf(char[][] graph) {
        boolean[][] visited = new boolean[rows][cols];
        int totalSheep = 0;
        int totalWolf = 0;
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (!visited[i][j] && graph[i][j] != '#') {
                    int[] counts = dfs(graph, visited, i, j);
                    int sheep = counts[0];
                    int wolf = counts[1];
                    
                    if (sheep > wolf)
                        totalSheep += sheep;
                    else
                        totalWolf += wolf;
                }
            }
        }
        
        int[] result = {totalSheep, totalWolf};
        return result;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        rows = scanner.nextInt();
        cols = scanner.nextInt();
        scanner.nextLine(); // 개행 문자 처리
        
        char[][] graph = new char[rows][cols];
        
        // 행렬 입력
        for (int i = 0; i < rows; i++) {
            String row = scanner.nextLine();
            for (int j = 0; j < cols; j++) {
                graph[i][j] = row.charAt(j);
            }
        }
        
        int[] result = countSheepAndWolf(graph);
        System.out.println(result[0] + " " + result[1]);
    }
}
Comments