본문 바로가기

📚 Algorithm/9oormthon Challenge

[구름톤 챌린지 - 9oormthon Challenge] Day 13 발전기 2 - Python 파이썬 풀이

구름톤 챌린지 13일차 - 발전기  2

 

📜  문제 

 

 

✏️ 입력 

 

✏️ 출력

 

💡 풀이 

 

그림으로 조금 더 쉽게 접근해봤다.

가장 많은 단지를 보유한 건물의 유형을 찾아야 하기 때문에, 건물의 개수도 세주고 temp와 cnt를 비교해서 가장 많은 단지가 나온 건물 유형을 찾도록 했다. 

 

N, K = map(int, input().split())
town = [list(map(int, input().split())) for _ in range(N)]

dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]


def dfs(i, j):
	stack = [(i, j)]

	#건물의 유형을 미리 저장
	building_type = town[i][j]

	cnt = 0
	
	while stack:
		y, x = stack.pop()

		if town[y][x] != building_type:
			continue
		else : 
			town[y][x] = 0
			cnt += 1
		
		for k in range(4):
			ny = y + dy[k]
			nx = x + dx[k]
			
			if ny in (-1, N) or nx in (-1, N) or town[ny][nx] != building_type:
				continue
		
			stack.append((ny, nx))
	
	return cnt

cnt = [0] * 31

# 모든 위치에 대해 탐색
for i in range(N):
	for j in range(N):
		if town[i][j]:
			building_type = town[i][j]
			if dfs(i, j) >= K:
				cnt[building_type] += 1

result, temp = 0, 0

for i in range(31):
	if temp <= cnt[i]:
		result = i
		temp = cnt[i]

print(result)

 

 

 

 

 

 

728x90