관리 메뉴

어읽로꾸거

BOJ 6186 Best Grass 본문

알고리즘

BOJ 6186 Best Grass

어읽로꾸거 2019. 4. 23. 14:20

백준 6186 링크

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

 

6186번: Best Grass

Bessie is planning her day of munching tender spring grass and is gazing out upon the pasture which Farmer John has so lovingly partitioned into a grid with R (1 <= R <= 100) rows and C (1 <= C <= 100) columns. She wishes to count the number of grass clump

www.acmicpc.net

풀이

🙄

코드

from collections import deque
def dajik():
    q=deque([(i,j)])
    while q:
        y,x=q.popleft()
        for k in range(4):
            ny,nx=y+ly[k],x+lx[k]
            if 0<=ny<n and 0<=nx<m and mp[ny][nx]=='#' and visit[ny][nx]==0:
                visit[ny][nx]=1
                q.append((ny,nx))
n,m=map(int,input().split())
mp=[input() for _ in range(n)]
visit=[[0]*m for _ in range(n)]
ly,lx=[0,0,-1,1],[1,-1,0,0]
count=0
for i in range(n):
    for j in range(m):
        if mp[i][j]=='#' and visit[i][j]==0:
            visit[i][j]=1
            dajik()
            count=count+1
print(count)

'알고리즘' 카테고리의 다른 글

BOJ 1038 감소하는 수  (0) 2019.09.15
BOJ 17141 연구소 2  (0) 2019.09.13
BOJ 13549 숨바꼭질3  (0) 2019.04.22
BOJ 6118 숨바꼭질  (0) 2019.04.19
BOJ 6087 레이저 통신  (0) 2019.04.18