https://www.acmicpc.net/problem/3184
from collections import deque
col,row = map(int,input().split())
arr = []
for i in range(col):
a = str(input())
a = list(a)
arr.append(a)
visit = [[False]*row for _ in range(col)]
dx = [1,-1,0,0]
dy = [0,0,1,-1]
w,s = 0,0
for i in range(col):
for j in range(row):
if visit[i][j] == False:
if arr[i][j] == 'v' or arr[i][j] =='o':
wolf,sheep =0,0
q = deque()
q.append((i,j))
visit[i][j] = True
while q:
x,y = q.popleft()
if arr[x][y] == 'v':
wolf += 1
if arr[x][y] == 'o':
sheep += 1
for k in range(4):
nx = dx[k] + x
ny = dy[k] + y
if 0 <= nx < col and 0 <= ny < row:
if visit[nx][ny] == False:
if arr[nx][ny] != '#':
visit[nx][ny] = True
q.append((nx,ny))
if wolf <sheep:
s += sheep
else:
w += wolf
print(s,w)
BFS를 이용한 문제풀이.
'알고리즘' 카테고리의 다른 글
프로그래머스) 카드뭉치 (0) | 2023.03.19 |
---|---|
프로그래머스)덧칠하기 (0) | 2023.03.05 |
백준)1로 만들기 (0) | 2023.02.28 |
백준)정수 삼각형 (0) | 2023.02.28 |
백준)모둔 순열 (0) | 2023.02.26 |