ํฐ์คํ ๋ฆฌ ๋ทฐ
728x90
๋ฐ์ํ
๋ฌธ์
๋ฌธ์ ํ์ด
๊ทธ๋ํ์ `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
๋ฐ์ํ
'๐จโ๐ป ์ฝ๋ฉํ ์คํธ > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค: 1600 ๋ง์ด ๋๊ณ ํ ์์ญ์ด (0) | 2020.09.14 |
---|---|
๋ฐฑ์ค: 14395 4์ฐ์ฐ (0) | 2020.09.13 |
๋ฐฑ์ค: 11728 ๋ฐฐ์ด ํฉ์น๊ธฐ (0) | 2020.09.12 |
๋ฐฑ์ค: 10816 ์ซ์ ์นด๋ 2 (0) | 2020.09.12 |
๋ฐฑ์ค: 10815 ์ซ์ ์นด๋ (0) | 2020.09.12 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ