프로그래밍
백준 10026 적록색약] 파이썬 풀이 🦄
Rudi
2021. 2. 17. 23:28
[백준 10026 적록색약] 파이썬 풀이 🦄
문제
동일한 값에 대해서 bfs/dfs를 통하여 탐색하는 기본 문제
Keypoints
- 두 가지 버전에 대해서 탐색해야 하므로, board 를 Copy한다.
Solution
dx = [0,0,1,-1]
dy = [1,-1,0,0]
def bfs3(board, x,y):
color = board[y][x]
board[y][x]=0
queue = [(x,y)]
while queue:
x,y = queue.pop(0)
for i in range(4):
nx, ny = x+dx[i], y+dy[i]
if 0<=nx<n and 0<=ny<n and board[ny][nx] == color:
queue.append((nx,ny))
board[ny][nx]=0
def bfs2(board, x,y):
color = board[y][x]
board[y][x]=0
if color=="G" or color=="R":
color_list=["R", "G"]
else:
color_list =["B"]
queue = [(x,y)]
while queue:
x,y = queue.pop(0)
for i in range(4):
nx, ny = x+dx[i], y+dy[i]
if 0<=nx<n and 0<=ny<n and board[ny][nx] in color_list:
queue.append((nx,ny))
board[ny][nx]=0
def count(board, bfs):
count = 0
for x in range(n):
for y in range(n):
if board[y][x] != 0:
bfs(board, x,y)
count +=1
return count
import copy
n = int(input())
board = [list(input().strip()) for i in range(n)]
board2 = copy.deepcopy(board)
a = count(board,bfs3)
b = count(board2,bfs2)
print(a, b)