ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

10026๋ฒˆ: ์ ๋ก์ƒ‰์•ฝ

์ ๋ก์ƒ‰์•ฝ์€ ๋นจ๊ฐ„์ƒ‰๊ณผ ์ดˆ๋ก์ƒ‰์˜ ์ฐจ์ด๋ฅผ ๊ฑฐ์˜ ๋Š๋ผ์ง€ ๋ชปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์ด ๋ณด๋Š” ๊ทธ๋ฆผ์€ ์•„๋‹Œ ์‚ฌ๋žŒ์ด ๋ณด๋Š” ๊ทธ๋ฆผ๊ณผ๋Š” ์ข€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ํฌ๊ธฐ๊ฐ€ N×N์ธ ๊ทธ๋ฆฌ๋“œ์˜ ๊ฐ ์นธ์— R(๋นจ๊ฐ•), G(์ดˆ๋ก)

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๊ทธ๋ž˜ํ”„์— `R`, `G`, `B` ์„ธ ๊ฐ€์ง€ ์ƒ‰๊น”์ด ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ ๋ก์ƒ‰์•ฝ์˜ ๊ฒฝ์šฐ `G`๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์—†๋‹ค. ์ด์—, ๊ทธ๋ž˜ํ”„์˜ ์˜์—ญ์„ ํƒ์ƒ‰ํ•˜๋˜, ์ƒ‰์•ฝ์ด์ง€ ์•Š๋Š” ์‚ฌ๋žŒ์ด ๊ตฌ๋ถ„ํ•˜๋Š” ์˜์—ญ๊ณผ ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์ด ๊ตฌ๋ถ„ํ•˜๋Š” ์˜์—ญ์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์•ž์„œ ๋‹ค๋ฃฌ ๋‹จ์ง€ ๋ฒˆํ˜ธ ๋ถ™์ด๊ธฐ์™€ ๋™์ผํ•œ ๋กœ์ง์œผ๋กœ ํ’€๋ฉด ๋œ๋‹ค. ๋‹ค๋ฅธ ์ ์€ ํ•œ๋ฒˆ ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ , `G`์ธ ๊ฒฝ์šฐ์—๋Š” `R`๋กœ ๋ฐ”๊พธ์–ด ๋‹ค์‹œ ํ•œ๋ฒˆ ํƒ์ƒ‰ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ๋˜ํ•œ, ๋‹จ์ง€์˜ ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์˜์—ญ์˜ ์ˆ˜๋ฅผ ์„ผ๋‹ค๋Š” ๊ฒƒ์ด ๋‹ค๋ฅด๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


def visitable(x, y):
    return 0 <= x < n and 0 <= y < n and not visited[x][y]


def bfs(start, cur_color):
    q = deque([start])
    dirs = ((1, 0), (-1, 0), (0, -1), (0, 1))

    while q:
        x, y = q.popleft()

        for dx, dy in dirs:
            next_x, next_y = x + dx, y + dy
            if visitable(next_x, next_y) and graph[next_x][next_y] == cur_color:
                visited[next_x][next_y] = True
                q.append([next_x, next_y])


if __name__ == '__main__':
    n = int(stdin.readline())
    graph = [list(stdin.readline().rstrip()) for _ in range(n)]
    answer = [0, 0]

    for idx in range(2):
        visited = [[False] * n for _ in range(n)]
        for i in range(n):
            for j in range(n):
                if not visited[i][j]:
                    visited[i][j] = True
                    bfs([i, j], graph[i][j])
                    answer[idx] += 1
        graph = [['R' if graph[i][j] == 'G' else graph[i][j] for i in range(n)] for j in range(n)]

    print(answer[0], answer[1])

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€