[백준 1388 파이썬] 바닥 장식

일단 해볼게·2022년 11월 21일
0

백준

목록 보기
63/132

https://www.acmicpc.net/problem/1388

# 바닥 장식

import sys
input = sys.stdin.readline
from collections import deque

N, M = map(int, input().rstrip().split())
floor_lst = [list(input().rstrip()) for _ in range(N)]

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

wood = 0


def bfs(graph, a, b, ch):
    queue = deque()
    queue.append((a, b)) # append로 안하면 a, b 따로 들어가니 조심하자
    graph[a][b] = '#' # 추가한 좌표 #으로 초기화

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

        for i in range(2): 

            if ch == '-': # - 일 때 ny 값 이용해서 가로값 체크
                nx = x
                ny = y + dy[i+2]

            elif ch == '|': # ㅣ 일 때 nx 값 이용해서 세로값 체크
                nx = x + dx[i]
                ny = y

            elif ch == '#': # #으로 초기화했을 때
                continue

            if 0 <= nx < N and 0 <= ny < M and graph[nx][ny] == ch: 
                graph[nx][ny] = '#' # #으로 왔던 곳 체크
                queue.append((nx, ny)) # 큐에 추가

for i in range(N):
    for j in range(M):
        if floor_lst[i][j] == '-':
            bfs(floor_lst, i, j, '-')
            wood += 1
            
        elif floor_lst[i][j] == '|':
            bfs(floor_lst, i, j, '|')
            wood += 1
        elif floor_lst[i][j] == '#':
            continue

print(wood)

deque() 함수는 선언할 때 들어왔던 인자를 자동으로 deque화 시켜서 저장
lst = deque([1, 2]) == lst = deque((1, 2))
lst[0] = 1, lst[1] = 2

profile
시도하고 More Do하는 백엔드 개발자입니다.

0개의 댓글