python 생태계 행렬 문제

심지혜·2024년 4월 16일
0



1

import copy
from collections import deque
length = 10

names = {
    0: '독수리',
    1: '뱀',
    2: '쥐',
    3: '휘파람새',
    4: '개구리',
    5: '나방',
    6: '메뚜기',
    7: '다람쥐',
    8: '과실',
    9: '나무'
}

matrix = [
    [0, 1, 1, 1, 1, 0, 0, 1, 0, 0],
    [0, 0, 1, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0, 1, 0],
    [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]

p = dict()

for i in range(10):
    for j in range(10):
        if matrix[i][j] == 1:
            if i in p: p[i].append(j)
            else: p[i] = [j]

s = copy.deepcopy(p)
for k, v in p.items():
    for i in v:
        if i in p: s[k] += s[i]
    
for i in range(len(matrix)):
    if i in s: print(f"{names[i]}: {len(s[i])}")
    else: print(f"{names[i]}: 0")

0개의 댓글