알고리즘
[백준] 10026번: 적록색약 - python
육빔
2024. 5. 7. 18:44
728x90
반응형
https://www.acmicpc.net/problem/10026
약간 귀찮은 BFS문제
visted를 두고 방문안했으면 --> 방문처리
R G 를 같게 두고 다시
visted 초기화 후 방문안했으면 --> 방문처리
답 출력을 진행한다.
from collections import deque
import sys
arr = []
dx = [-1, 0 , 1, 0]
dy = [0, 1, 0 , -1]
n = int(input())
visited = [[False] * n for _ in range(n)]
for _ in range(n):
k = list(input())
arr.append(k)
dq = deque()
cnt1 = 0
for i in range(n):
for j in range(n):
if visited[i][j] == False:
visited[i][j] = True
cnt1+=1
dq.append((i,j))
while dq:
x, y = dq.popleft()
for k in range(4):
nx = dx[k] + x
ny = dy[k] + y
if 0<=nx<n and 0<=ny<n:
if visited[nx][ny] == False:
if arr[nx][ny] == arr[x][y]:
visited[nx][ny] = True
dq.append((nx, ny))
for i in range(n):
for j in range(n):
if arr[i][j] == "R":
arr[i][j] = "G"
visited = [[False] * n for _ in range(n)]
cnt2 = 0
for i in range(n):
for j in range(n):
if visited[i][j] == False:
visited[i][j] = True
cnt2+=1
dq.append((i,j))
while dq:
x, y = dq.popleft()
for k in range(4):
nx = dx[k] + x
ny = dy[k] + y
if 0<=nx<n and 0<=ny<n:
if visited[nx][ny] == False:
if arr[nx][ny] == arr[x][y]:
visited[nx][ny] = True
dq.append((nx, ny))
print(cnt1, cnt2)
완성
728x90
반응형