- 문제 : https://www.acmicpc.net/problem/1926

 

내부 로직은 맞았는데, 결과 출력할 때 잘못 작성해서 53%에서 계속 틀렸다. 

조건에 따라 bfs로 넓이를 구한뒤 그 값을 reulst=[]에 삽입해서 그것의 갯수와 최댓값을 구하는 로직을 짰는데

내가 봤을 땐 result=[]일 때 에러가 생겼던 것 같다 

 

그래서 이 부분을 result=0으로 바꾸고, bfs 실행할 때마다 max로 값 비교를 한 후 출력하니 정답이 떴다 

 

- 정답 풀이 :

from collections import deque

n,m=map(int,input().split())
data=[]
for _ in range(n):
    data.append(list(map(int,input().split())))
visit=[[0]*m for _ in range(n)]
dx=[-1,1,0,0]
dy=[0,0,-1,1]    
          
def bfs(x,y):
    queue=deque()
    queue.append((x,y))   
    count=1        
    while queue:
           x,y=queue.popleft()
           for i in range(4):
               nx,ny=x+dx[i],y+dy[i]
               if 0<=nx<n and 0<=ny<m:
                if visit[nx][ny] ==0 and data[nx][ny]==1:
                    visit[nx][ny]=1
                    count+=1
                    queue.append((nx,ny))
    return count

result=0
answer=0
for i in range(n):
    for j in range(m):
        if visit[i][j] ==0  and data[i][j]==1:
            visit[i][j]=1
            result=max(result,bfs(i,j))
            answer+=1

print(answer)
print(result)

 

-틀렸던 부분

result=[]  
answer=0
for i in range(n):
    for j in range(m):
        if visit[i][j] ==0  and data[i][j]==1:
            visit[i][j]=1
            result.append(bfs(i,j))
            answer+=1

if len(result)==0:
    print(0)
else:    
    print(answer)           
    print(max(result))

'백준 > Search' 카테고리의 다른 글

[dfs/백준] 13023번 : ABCDE  (0) 2022.08.05
[bfs/백준] 1325번 : 효율적인 해킹  (0) 2022.08.03
[dfs/백준] 1068번 : 트리  (0) 2022.07.31
[dfs/백준] 9466번 : 텀 프로젝트  (0) 2022.07.31
[dfs/백준] 2210번 : 숫자판 점프  (0) 2022.07.30

+ Recent posts