본문 바로가기
코딩테스트/softeer

[python] Level2_장애물 인식 프로그램

by brown_board 2023. 1. 1.
728x90

https://softeer.ai/practice/info.do?idx=1&eid=409 

 

Softeer

연습문제를 담을 Set을 선택해주세요. 취소 확인

softeer.ai

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sys
 
# 정사각형 n개 갯수받기
#sys.stdin = open("input1.txt","r")
= int(sys.stdin.readline())
 
# map 정보 저장
graph = []
for i in range(n):
    graph.append(list(map(int,input())))
 
# 모든 경로에 대해 탐색해야 하므로 dfs가 좋다.
total_obstacle = 0 # 총 장애물 갯수
cnt_obstacle = 0 # 각각 장애물의 수
array = [] # 각각 장애물의 수 를 저장시킬 빈 리스트
def dfs(x,y):
    # 경로 끝에 가면 멈춘다.
    global cnt_obstacle
    if x<=-1 or y<= -1 or x>=or y>=n:
        return False
    # 자신이 도로라면
    if graph[x][y]==1:
        graph[x][y]=0
        dfs(x+1,y)
        dfs(x, y+1)
        dfs(x-1, y)
        dfs(x, y-1)
        cnt_obstacle += 1
        return True
 
for i in range(n):
    for j in range(n):
        if dfs(i,j) == True:
            total_obstacle += 1
            array.append(cnt_obstacle)
            cnt_obstacle = 0
print(total_obstacle)
array.sort()
for i in range(total_obstacle):
    print(array[i])
cs
728x90

'코딩테스트 > softeer' 카테고리의 다른 글

[python] Level2_비밀 메뉴  (0) 2023.01.03
[python] Level2_지도 자동 구축  (0) 2023.01.01
[python] Level2_8단 변속기  (0) 2023.01.01
[python] Level1_A+B  (0) 2023.01.01
[python] Level1_최단거리  (0) 2022.12.31

댓글