본문 바로가기

Coding Test

[소프티어] 장애물

https://softeer.ai/practice/6282

 

Softeer - 현대자동차그룹 SW인재확보플랫폼

자율주행팀 SW 엔지니어인 당신에게 장애물과 도로를 인식할 수 있는 프로그램을 만들라는 업무가 주어졌다. [그림 1] 지도 예시 우선 [그림 1]과 같이 정사각형 모양의 지도가 있다. 1은 장애물이

softeer.ai

from collections import deque

N = int(input())
visited = [[0 for col in range(N)] for row in range(N)]
ground = list()

def bfs(x, y):
    queue = deque()
    queue.append((x, y))
    visited[x][y] = 1
    cnt = 1

    while(queue):
        x, y = queue.popleft()
        dx = [0, 0, -1, 1]
        dy = [-1, 1, 0, 0]

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if nx < 0 or ny < 0 or nx >= len(ground) or ny >= len(ground[0]):
                continue
            if ground[nx][ny] == '0':
                continue
            if ground[nx][ny] == '1' and visited[nx][ny] == 0:
                visited[nx][ny] = 1
                queue.append((nx, ny))
                cnt += 1
    return cnt 

ground = [list(input()) for _ in range(N)]

result = list()
for i in range(N):
    for j in range(N):
        if ground[i][j] == '1' and visited[i][j] == 0:
            result.append(bfs(i, j))

print(len(result))
for i in range(len(result)):
    print(result[i])

'Coding Test' 카테고리의 다른 글

[Algorithm] DP(Dynamic Programming)  (1) 2023.11.23
[카카오기출] 프로그래머스_튜플_Lv2  (0) 2023.11.19
#1541  (0) 2021.05.18
#2447 별찍기 JAVA  (0) 2021.04.06
#10989 수 정렬하기3  (0) 2021.03.30