🌞Algorithm/πŸ”₯Baekjoon

[Baekjoon] 1303_μ „μŸ - μ „νˆ¬

λΏŒμ•Ό._. 2022. 4. 26. 12:15

Silver I

문제(좜처: https://www.acmicpc.net/problem/1303)

<μ „μŸ-μ „νˆ¬>

문제 

 

μ „μŸμ€ μ–΄λŠλ§ 전면전이 μ‹œμž‘λ˜μ—ˆλ‹€. κ²°κ΅­ μ „νˆ¬λŠ” λ‚œμ „μ΄ λ˜μ—ˆκ³ , 우리 병사와 적ꡭ 병사가 μ„žμ—¬ μ‹Έμš°κ²Œ λ˜μ—ˆλ‹€. κ·ΈλŸ¬λ‚˜ λ‹Ήμ‹ μ˜ 병사듀은 흰색 μ˜·μ„ μž…κ³ , 적ꡭ의 병사듀은 νŒŒλž€μƒ‰ μ˜·μ„ μž…μ—ˆκΈ° λ•Œλ¬Έμ— μ„œλ‘œκ°€ 적인지 μ•„κ΅°μΈμ§€λŠ” ꡬ뢄할 수 μžˆλ‹€. λ¬Έμ œλŠ” 같은 νŒ€μ˜ 병사듀은 λͺ¨μ΄λ©΄ λͺ¨μΌμˆ˜λ‘ κ°•ν•΄μ§„λ‹€λŠ” 사싀이닀.

Nλͺ…이 λ­‰μ³μžˆμ„ λ•ŒλŠ” N2의 μœ„λ ₯을 λ‚Ό 수 μžˆλ‹€. κ³Όμ—° μ§€κΈˆ λ‚œμ „μ˜ μƒν™©μ—μ„œλŠ” λˆ„κ°€ μŠΉλ¦¬ν•  것인가? λ‹¨, 같은 νŒ€μ˜ 병사듀이 λŒ€κ°μ„ μœΌλ‘œλ§Œ μΈμ ‘ν•œ κ²½μš°λŠ” 뭉쳐 μžˆλ‹€κ³  보지 μ•ŠλŠ”λ‹€.

 

 

μž…λ ₯

 

첫째 μ€„μ—λŠ” μ „μŸν„°μ˜ κ°€λ‘œ 크기 N, μ„Έλ‘œ 크기 M(1 ≤ N, M ≤ 100)이 μ£Όμ–΄μ§„λ‹€. κ·Έλ‹€μŒ 두 번째 μ€„μ—μ„œ M+1번째 μ€„μ—λŠ” 각각 (X, Y)에 μžˆλŠ” λ³‘μ‚¬λ“€μ˜ μ˜·μƒ‰μ΄ 띄어쓰기 없이 μ£Όμ–΄μ§„λ‹€. λͺ¨λ“  μžλ¦¬μ—λŠ” 병사가 ν•œ λͺ… μžˆλ‹€. BλŠ” νŒŒλž€μƒ‰, WλŠ” 흰색이닀. λ‹Ήμ‹ μ˜ 병사와 적ꡭ의 λ³‘μ‚¬λŠ” ν•œ λͺ… 이상 μ‘΄μž¬ν•œλ‹€.

 

 

좜λ ₯ 

 

첫 번째 쀄에 λ‹Ήμ‹ μ˜ λ³‘μ‚¬μ˜ μœ„λ ₯의 ν•©κ³Ό 적ꡭ의 λ³‘μ‚¬μ˜ μœ„λ ₯의 합을 좜λ ₯ν•œλ‹€.

 

 

문제 풀이

   - my solution (Python)

import sys
from collections import deque

def bfs(arr, visited, start, find):
    dx=[-1,1,0,0] # μƒν•˜μ’Œμš°
    dy=[0,0,-1,1]

    queue=deque([start])
    visited[start[0]][start[1]]=1
    cnt=1

    while queue:
        temp=queue.popleft()
        for i in range(4):
            x=temp[0]+dx[i]
            y=temp[1]+dy[i]
            if 0<=x<len(arr) and 0<=y<len(arr[0]):
                if arr[x][y]==find and visited[x][y]==0:
                    visited[x][y]=1
                    queue.append([x,y])
                    cnt+=1
    return cnt


if __name__=='__main__':
    n,m=map(int,sys.stdin.readline().split()) # κ°€λ‘œ, μ„Έλ‘œ

    arr=[]
    for i in range(m):
        arr.append(list(sys.stdin.readline().strip()))

    visited_w=[[0]*n for i in range(m)]
    visited_b = [[0] * n for i in range(m)]
    w_cnt=0
    b_cnt=0
    for i in range(m):
        for j in range(n):
            if arr[i][j]=='W' and visited_w[i][j]==0:
                cnt=bfs(arr, visited_w, [i,j], 'W')
                w_cnt+=(cnt**2)
            elif arr[i][j]=='B' and visited_b[i][j]==0:
                cnt=bfs(arr, visited_b, [i,j], 'B')
                b_cnt+=(cnt**2)


    print(w_cnt, end=' ')
    print(b_cnt)

 

- my solution (JAVA)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class _1303_μ „μŸ_μ „νˆ¬ {
	static int bfs(String[][] arr, int[][] visited, int[] start, String find) {
		int dx[]= {-1,1,0,0}; // μƒν•˜μ’Œμš°
		int dy[]= {0,0,-1,1};
		
		visited[start[0]][start[1]]=1;
		Queue<int[]> queue=new LinkedList<>();
		queue.add(start);
		
		int cnt=1;
		
		while(queue.size()!=0) {
			int temp[]=queue.poll();
			for(int i=0; i<4; i++) {
				int x=temp[0]+dx[i];
				int y=temp[1]+dy[i];
				if(x>=0 && x<arr.length && y>=0 && y<arr[0].length) { // λ²”μœ„ μ•ˆ
					if(arr[x][y].equals(find) && visited[x][y]==0) {
						visited[x][y]=1;
						queue.add(new int[] {x,y});
						cnt+=1;
					}
				}
			}
		}
		return cnt;
	}

	public static void main(String[] args) throws IOException{
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		
		String s=bf.readLine();
		int n=Integer.parseInt(s.split(" ")[0]); // κ°€λ‘œ
		int m=Integer.parseInt(s.split(" ")[1]); // μ„Έλ‘œ
		
		String arr[][]=new String[m][n];
		for(int i=0; i<m; i++) {
			s=bf.readLine();
			for(int j=0; j<n; j++) {
				arr[i][j]=s.split("")[j];
			}
		}
		
		int visited_w[][]=new int[m][n]; // λ°©λ¬Έ μ—¬λΆ€
		int visited_b[][]=new int[m][n];
		int cnt_w=0;
		int cnt_b=0;
		
		for(int i=0; i<m; i++) {
			for(int j=0; j<n; j++) {
				if(arr[i][j].equals("W") && visited_w[i][j]==0) {
					int cnt=bfs(arr, visited_w, new int[] {i,j}, "W");
					cnt_w+=(cnt*cnt);
				}
				if(arr[i][j].equals("B") && visited_b[i][j]==0) {
					int cnt=bfs(arr, visited_b, new int[] {i,j}, "B");
					cnt_b+=(cnt*cnt);
				}
			}
		}
		System.out.println(cnt_w +" "+ cnt_b);

	}

}

 

 

μƒν•˜μ’Œμš°λ₯Ό ν™•μΈν•˜μ—¬ μžμ‹ κ³Ό 같은 νŒ€μ΄λΌλ©΄ λͺ‡ λͺ…인지 κ΅¬ν•œ ν›„ N μ œκ³±μ„ λ”ν•΄μ€λ‹ˆλ‹€. 

 

 

  • bfs ( μž…λ ₯ 정보, λ°©λ¬Έ μ—¬λΆ€, μ‹œμž‘ μœ„μΉ˜, μ°ΎλŠ” κ°’ 'W' or 'B' )
    : μƒν•˜μ’Œμš°λ₯Ό νƒμƒ‰ν•˜μ—¬ ν˜„μž¬ μœ„μΉ˜μ˜ κ°’κ³Ό 같은 값이라면 cntλ₯Ό μ¦κ°€μ‹œμΌœμ£Όλ©° κ³„μ†ν•΄μ„œ νƒμƒ‰ν•΄μ€λ‹ˆλ‹€.
  • Main
    : κ°€λ‘œ, μ„Έλ‘œ μž…λ ₯
    : arr 정보 μž…λ ₯
    : visited_w, visited_b = λ°©λ¬Έ μ—¬λΆ€
    : w_cnt, b_cnt = λ³‘μ‚¬μ˜ μœ„λ ₯
    : arr 전체λ₯Ό μˆœν™˜ν•˜λ©° 'W'일 λ•Œμ™€ 'B'일 λ•Œ bfs 탐색
    -> λ°˜ν™˜ κ°’ = 뭉쳐 μžˆλŠ” 수 μ΄λ―€λ‘œ 각 cnt에 μ œκ³±ν•œ 값을 더함
    : w_cnt, b_cnt 좜λ ₯

 

 

❓ λŸ°νƒ€μž„ μ—λŸ¬?

λ¬Έμ œμ—μ„œ κ°€λ‘œ, μ„Έλ‘œ 순으둜 μž…λ ₯λ°›μ•˜λŠ”λ° μ²˜μŒμ— μ½”λ“œ μž‘μ„± μ‹œ μ„Έλ‘œ, κ°€λ‘œλ‘œ ν™œμš©ν–ˆκΈ° λ•Œλ¬Έμ— 인덱슀 μ—λŸ¬κ°€ λ°œμƒν•˜μ˜€λ‹€.

 


μƒκ°πŸ€”

 

BFSλ₯Ό μ‚¬μš©ν•˜μ—¬ μ‰½κ²Œ ꡬ할 수 μžˆλŠ” λ¬Έμ œμ˜€λ‹€.