인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

cykpig0615's profile image
cykpig0615

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

15. Path Search (Graph DFS: Depth First Search)

이런 식으로 짜도 괜찮을까요?

Written on

·

158

0

n,m = map(int, input().split())

gp=[[0]*n for _ in range(n)]

for _ in range(m):

    a,b = map(int, input().split())

    gp[a-1][b-1]=1

def DFS(L):

  global cnt

  if res[L]==n:

    cnt+=1

  else:

    for i,x in enumerate(gp[res[L]-1]):

      if x==1 and tmp[i]==0:

        res.append(i+1)

        tmp[i]=1

        DFS(L+1)

        res.pop()

        tmp[i]=0

cnt=0

res=[1]

tmp=[0]*n

tmp[0]=1

DFS(0)

print(cnt)

python코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

네 append와 pop으로 해도 좋습니다.

cykpig0615's profile image
cykpig0615

asked

Ask a question