https://www.acmicpc.net/problem/2667
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여
www.acmicpc.net
#풀이

n=int(input())
graph=[]
num=[]
dx=[0,0,-1,1]
dy=[-1,1,0,0]
for i in range(n):
graph.append(list(map(int,input())))
def dfs(x,y):
if x<0 or x>=n or y<0 or y>=n:
return False
if graph[x][y]==1:
global count #전역변수 global 선언
count+=1
graph[x][y]=0
for i in range(4): #상하좌우 탐색
nx=x+dx[i]
ny=y+dy[i]
dfs(nx,ny)
return True
return False
count=0
result=0
for i in range(n):
for j in range(n):
if dfs(i,j)==True:
num.append(count)
result+=1
count=0
print(result)
num.sort()
for i in range(len(num)):
print(num[i])
#문제와의 싸움

흠... 너무 급하게 풀기도 했고 뭔가 씐 것처럼 사소한 것들을 틀렸었다.
좀 더 차분하게 푸는 능력을 길러야겠다.
'파이썬알고리즘' 카테고리의 다른 글
20210810#(76) 백준 1715번 카드 정렬하기 (0) | 2021.08.10 |
---|---|
20210808#(75) 백준 문자열문제풀이(백준 6문제) (0) | 2021.08.08 |
20210806#(73) 백준 1260번 DFS와 BFS (0) | 2021.08.06 |
20210805#(72) 이코테 미로탈출 DFS/BFS (0) | 2021.08.05 |
20210804#(71) DFS/BFS 개념 (0) | 2021.08.04 |