Coding - Algo/python
[백준] 1926번:그림 (Python 파이썬)
jainn
2021. 8. 3. 00:00
728x90
문제
https://www.acmicpc.net/problem/1926
풀이 및 소스코드
1의 개수가 하나도 없을 때 고려해주지 않으면
valueerror가 뜬다 ㅠㅠ ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ ㅠㅠㅠ...
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000)
from collections import deque
n,m = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(n)]
count = []
v = [[0]*m for _ in range(n)]
def bfs(i, j):
global cnt
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
q = deque()
q.append([i, j])
v[i][j] = 1
while q:
x, y = q.popleft()
for k in range(4):
nowx, nowy = x+dx[k], y+dy[k]
if 0<=nowx<n and 0<=nowy<m :
if arr[nowx][nowy] == 1 and v[nowx][nowy] == 0:
q.append([nowx,nowy])
v[nowx][nowy] = 1
cnt += 1
count.append(cnt)
for i in range(n):
for j in range(m):
if(arr[i][j]==1 and v[i][j]==0):
cnt = 1
bfs(i, j)
if(len(count)==0):
print(len(count))
print(0)
else:
print(len(count))
print(max(count))
반응형