8000 [23-03-02] dain.py by da-in · Pull Request #141 · da-in/algorithm-study · GitHub
[go: up one dir, main page]

Skip to content

[23-03-02] dain.py #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
📝 Create [그래프] 방의 개수 dain.py
  • Loading branch information
da-in committed Mar 1, 2023
commit 7785d62363affb86ee59a5d2bc3bb5ca11041ee1
34 changes: 34 additions & 0 deletions Programmers - 고득점 Kit/[그래프] 방의 개수/dain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def solution(arrows):
answer = 0
graph = {(0, 0): set()}
dx = [0, 1, 1, 1, 0, -1, -1, -1]
dy = [1, 1, 0, -1, -1, -1, 0, 1]
x = y = nx = ny = 0

def isCross(a, x, y):
if a == 1 and (x, y + 1) in graph.keys() and (x + 1, y) in graph[(x, y + 1)]:
return True
elif a == 3 and (x, y - 1) in graph.keys() and (x + 1, y) in graph[(x, y - 1)]:
return True
elif a == 5 and (x, y - 1) in graph.keys() and (x - 1, y) in graph[(x, y - 1)]:
return True
elif a == 7 and (x, y + 1) in graph.keys() and (x - 1, y) in graph[(x, y + 1)]:
return True
return False

for a in arrows:
nx = x + dx[a]
ny = y + dy[a]
graph[(x, y)].add((nx, ny))
if (nx, ny) in graph.keys():
if (x, y) not in graph[(nx, ny)]:
graph[(nx, ny)].add((x, y))
answer += 1
if isCross(a, x, y):
answer += 1
else:
graph[(nx, ny)] = {(x, y)}
if isCross(a, x, y):
answer += 1
x, y = nx, ny
return answer
0