Skip to content

Instantly share code, notes, and snippets.

@inspirit941
Last active December 17, 2019 05:18
Show Gist options
  • Save inspirit941/a6b1c06bf662bde73880e3d665fd2b9c to your computer and use it in GitHub Desktop.
Save inspirit941/a6b1c06bf662bde73880e3d665fd2b9c to your computer and use it in GitHub Desktop.
from collections import defaultdict, deque
def can_reach(start, des, table):
visited = set()
queue = deque()
queue.append(start)
while queue:
nxt = queue.popleft()
visited.add(nxt)
for i in table[nxt]:
# 목적지와 일치하는 경우
if i == des:
return 1
elif i not in visited:
queue.append(i)
visited.add(i)
# 도착할 수 없는 경우
return 0
def solution(n,signs):
table = defaultdict(set)
for y in range(n):
for x in range(n):
if signs[y][x] == 1:
table[y].add(x)
answer = [[0 for _ in range(n)] for _ in range(n)]
for y in range(n):
for x in range(n):
# y에서 x까지 도착할 수 있는지 검사한다.
answer[y][x] = can_reach(y, x, table)
return answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment