Algorithm/BOJ

[Silver I/JAVA] 3187 μ–‘μΉ˜κΈ° 꿍

carsumin 2024. 12. 19. 11:10

https://www.acmicpc.net/problem/3187

 

 

쑰건
  • μ–‘ > λŠ‘λŒ€ -> μ–‘λ§Œ λ‚¨μŒ , λŠ‘λŒ€ >= μ–‘ -> λŠ‘λŒ€λ§Œ λ‚¨μŒ
  • μšΈνƒ€λ¦¬ λ°”κΉ₯μ—λŠ” μ–‘ λŠ‘λŒ€ μ—†μŒ
  • λΉˆκ³΅κ°„ 있음

 

ν•„μš”ν•œ 것
  • λ°©λ¬Έ μ—¬λΆ€ 체크
  • λ°©ν–₯ 이동 λ°°μ—΄ dx, dy
  • μšΈνƒ€λ¦¬, λΉˆκ³΅κ°„, μ–‘λŠ‘λŒ€ μž…λ ₯ 받을 map λ°°μ—΄
  • μž…λ ₯을 곡백없이 λ°›μŒ (charAt μ‚¬μš©)

 

풀이 κ³Όμ •
  • λ°©λ¬Έν•œ 적 μ—†κ³ , μšΈνƒ€λ¦¬κ°€ μ•„λ‹ˆλ©΄ BFS 탐색
  • μƒν•˜μ’Œμš° λ°©ν–₯ μ΄λ™ν•˜λ©΄μ„œ BFS 탐색을 ν•˜κ³  λŠ‘λŒ€μ™€ μ–‘μ˜ 개수λ₯Ό 각각 카운트
  • λ§ˆμ§€λ§‰μ— μ΅œμ’… λŠ‘λŒ€μ™€ μ–‘μ˜ 개수λ₯Ό 비ꡐ
  • μ’Œν‘œ ν˜•νƒœλ‘œ 큐에 ν•œλ²ˆμ— 담을 수 있음

 

전체 μ½”λ“œ
import java.io.*;
import java.util.*;

public class Main{
    static int R,C; //κ°€λ‘œC,μ„Έλ‘œR
    static char[][] map;
    static boolean[][] visited;
    static int[] dx = {-1,1,0,0};
    static int[] dy = {0,0,-1,1};
    static int wolf = 0;
    static int sheep = 0;
    
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] str = br.readLine().split(" ");
        R = Integer.parseInt(str[0]); //μ„Έλ‘œ
        C = Integer.parseInt(str[1]); //κ°€λ‘œ
        
        map = new char[R][C];
        visited = new boolean[R][C];
        
        //mapμ΄ˆκΈ°ν™”
        for(int i=0; i<R; i++){
            String s = br.readLine();
            for(int j=0; j<C; j++){
                map[i][j] = s.charAt(j);
            }
        }
        
        for(int i=0; i<R; i++){
            for(int j=0; j<C; j++){
                //λ°©λ¬Έx μšΈνƒ€λ¦¬x
                if(!visited[i][j] && map[i][j] != '#'){
                    bfs(i,j);
                }
            }
        }
        
        //κ²°κ³Ό 좜λ ₯
        System.out.println(sheep + " " + wolf);
    }
    
    private static void bfs(int x, int y){
        //μ’Œν‘œν˜•νƒœλ‘œ μ €μž₯ν•˜κΈ° μœ„ν•œ 큐 μ„ μ–Έ
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{x,y}); //크기가 2인 μ •μˆ˜λ°°μ—΄ κ°’ μΆ”κ°€
        visited[x][y] = true;
        
        int sheepCnt = 0;
        int wolfCnt = 0;
        
        if(map[x][y] == 'k') sheepCnt++;
        if(map[x][y] == 'v') wolfCnt++;
        
        while(!queue.isEmpty()){ //큐에 값이 있으면 반볡
            int[] current = queue.poll(); //ν˜„μž¬κ°’ 꺼냄
            int currentX = current[0];
            int currentY = current[1];
            
            for(int i=0; i<4; i++){
                int nx = currentX + dx[i];
                int ny = currentY + dy[i];
                
                if(nx >= 0 && nx < R && ny >= 0 && ny < C && !visited[nx][ny] && map[nx][ny] != '#'){
                    visited[nx][ny] = true;
                    queue.offer(new int[]{nx, ny});
                    
                    //μ–‘, λŠ‘λŒ€ 카운트
                    if(map[nx][ny] == 'k') sheepCnt++;
                    if(map[nx][ny] == 'v') wolfCnt++;
                }
            }
        }
        
        if(sheepCnt > wolfCnt){ //μ–‘>λŠ‘λŒ€ -> μ–‘ 개수만 λ‚¨μŒ
            sheep += sheepCnt;
        }else{
            wolf += wolfCnt;
        }
    }
}