본문 바로가기

백준

백준 2667번: 단지번호붙이기

www.acmicpc.net/problem/2667

 

2667번: 단지번호붙이기

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

www.acmicpc.net

big = int(input())

#지도크기

map =[] #전체 지도
save=[] #탐색했던곳의 주소 (튜플로 저장)
sum =0 #단지합 구하는 곳 (x단지에는 sum세대가 있다)

suml=[0] #단지합 모아두는곳. 나중에 소트해서 오름차순출력

sumnum=0 #단지가 몇개인지


for i in range(big):
    map.append(1)    
#일단 사이즈에 맞는 배열 만들어두고     
    
for i in range(big):
    b= str(input())
    map[i] = b
#원하는 내용으로  채우기
    
def checker():
	global sum
	global big
	for i in save:
		if int(i[0])-1>=0:
			if (int(i[0])-1,int(i[1])) not in save and map[int(i[0])-1][int(i[1])]=='1':
				save.append((int(i[0])-1,int(i[1])))
				sum+=1
		if int(i[0])+1<big:
			if (int(i[0])+1,int(i[1])) not in save and map[int(i[0])+1][int(i[1])]=='1':
				save.append((int(i[0])+1,int(i[1])))
				sum+=1
		if int(i[1])-1>=0:
			if (int(i[0]),int(i[1])-1) not in save and map[int(i[0])][int(i[1])-1]=='1':
				save.append((int(i[0]),int(i[1])-1))
				sum+=1
		if int(i[1])+1<big:
			if (int(i[0]),int(i[1])+1) not in save and  map[int(i[0])][int(i[1])+1]=='1':
				save.append((int(i[0]),int(i[1])+1))
				sum+=1
#한칸의 사방을 확인하고 다시 그 사방을 확인함 save가 계속 커지면서 약간 리커션 느낌인데 정확히 뭐라 불러야 하는지 모르겠음


al=0
be=0
while True:
        sum=0
        if map[al][be] =='1'and (al,be) not in save:
                sum+=1
                save.append((al,be))
                checker()
                sumnum+=1
                suml.append(sum)
                
        be+=1
        if be==big:
                be=0
                al+=1
        if al==big:
                break
#칸마다 보면서 checker() 갈길곳 찾음

del suml[0]
suml.sort()
print(sumnum)
for i in range(len(suml)):
    print(suml[i])

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

백준 1003번: 피보나치 함수  (0) 2021.02.25
백준 2884번: 알람 시계  (0) 2021.02.25