어이없게..
테스트 케이스중
1 1
0
이걸 예외처리를 멍청하게 해서.. 엄청 헤멘... 문제..
문제는 사실상 단지 번호 붙히기랑 똑같아서 바로 풀 줄 알았으나... 아니였다...
from queue import Queue
N, M = map(int, input().split())
mapData = [list(map(int, input().split())) for _ in range(N)]
visited = [[0] * M for _ in range(N)]
direction = [[-1, 0], [1, 0], [0, 1], [0, -1]]
counter = []
def bfs(x, y):
que = Queue()
que.put([x,y])
visited[x][y] = 1
count = 1
while not que.empty():
x, y = que.get()
for i in range(len(direction)):
nx = x + direction[i][0]
ny = y + direction[i][1]
if 0 <= nx < N and 0 <= ny < M:
if not visited[nx][ny] and mapData[nx][ny]:
que.put([nx, ny])
count = count + 1
visited[nx][ny] = 1
counter.append(count)
for i in range(N):
for j in range(M):
if mapData[i][j] and not visited[i][j]:
bfs(i, j)
if not counter:
print(0)
print(0)
else:
print(len(counter))
print(max(counter))
그래도 몇일 동안 진짜 오지게 헤멘 결과 능지가 오른 기분이긴하다..
그리고 툴을 Jupyter notebook에서 Pycharm으로 바꾸고 Git도 연동했다..
이유는 디버깅하려고..
'Algorithm > BFS, DFS' 카테고리의 다른 글
[백준] 1012번 유기농 배추 (0) | 2020.09.09 |
---|---|
[백준] 2644번 촌수계산 (0) | 2020.09.08 |
[백준] 10809번 알파벳 찾기 (0) | 2020.09.04 |
[백준] 단어 공부 (0) | 2020.09.04 |
[백준] 2675 문자열 반복 (0) | 2020.09.04 |
댓글