본문 바로가기
Programming

현대_Softeer_level2_장애물 인식 프로그램_파이썬

by WelcomeBro 2023. 1. 17.
반응형

문제 링크

 

파이썬

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
41
42
43
44
45
46
47
48
49
50
51
def sensor(row,col, tf_map,obs_size):
    if row<len(tf_map)-1:
        if tf_map[row+1][col] != True:
            tf_map[row+1][col] = True
            if whole_map[row+1][col]!=0:
                obs_size+=1
                obs_size,tf_map = sensor(row+1, col, tf_map, obs_size)
    if row>0:
        if tf_map[row-1][col] != True:
            tf_map[row-1][col] = True
            if whole_map[row-1][col]!=0:
                obs_size+=1
                obs_size,tf_map = sensor(row-1, col, tf_map, obs_size)
    if col<len(tf_map)-1:
        if tf_map[row][col+1!= True:
            tf_map[row][col+1= True
            if whole_map[row][col+1]!=0:
                obs_size+=1
                obs_size,tf_map = sensor(row, col+1, tf_map, obs_size)
    if col>0:
        if tf_map[row][col-1!= True:
            tf_map[row][col-1= True
            if whole_map[row][col-1]!=0:
                obs_size+=1
                obs_size,tf_map = sensor(row, col-1, tf_map, obs_size)
    return obs_size, tf_map
            
def check(whole_map, tf_map):
    obs_list = []
    for row in range(len(whole_map)):
        for col in range(len(whole_map)):
            if tf_map[row][col] != True:
                tf_map[row][col] = True
                if whole_map[row][col] != 0:
                    obs_size, tf_map = sensor(row, col, tf_map, 1)
                    obs_list.append(obs_size)
    return obs_list
 
map_size = int(input())
whole_map = []
tf_map = []
for i in range(map_size):
    now = list(map(int, input()))
    whole_map.append(now)
    tf_map.append([False for i in range(map_size)])
 
ans = check(whole_map, tf_map)
print(len(ans))
ans.sort()
for i in ans:
    print(i)
cs

 

Be posive!

Be rich!

Live your life!

반응형