아직 계정이 없으신가요? 회원가입

Dev. Talk

장애물 인식 프로그램 런타임 에러가 어느 부분에서 나는지 모르겠습니다..

회원사진mooya1024
24 views2022-11-15 22:06

import sys

input = sys.stdin.readline

n = int(input())
mp = [[0]*n for i in range(n)]

for i in range(n):
    tmp = list(map(int,input().rstrip())) 
    for j in range(n):
        mp[i][j]=tmp[j]


dx=[-1,1,0,0]
dy=[0,0,-1,1]
visited = [[0]*n for i in range(n)]
stack = []
ans=[]
blocks =0

while True:
    cnt=0
    for i in range(n):
        for j in range(n):
            if visited[i][j]==0 and mp[i][j]==1:
                stack.append((i,j))
                break
        if len(stack)!=0:
            break
    if len(stack)==0:
        break
    while stack:
        x,y=stack.pop()
        for i in range(4):
            sx=x+dx[i]
            sy=y+dy[i]
            if 0<=sx<n and 0<=sy<n and mp[sx][sy]==1 and visited[sx][sy]==0:
                visited[sx][sy]=1
                cnt+=1
                stack.append((sx,sy))
    blocks+=1
    ans.append(cnt)

print(blocks)
ans.sort()
for a in ans:
    print(a)


도와주신다면 감사하겠습니다 ㅠ