Skip to content
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

[03주차 김동주] 정사각형 크로스워드 (플래티넘 III) #20

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
55 changes: 55 additions & 0 deletions 03주차/김동주/2807.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import collections
import sys


ALPHABETS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ALPHABET_LEN = len(ALPHABETS)


def countCases(adjMat: dict[str, dict[str, int]], source: str, sink: str, *routes: list[str]) -> int:
if len(routes) < 2:
raise ValueError()

isPossible = True
ans = 1

# 인접행렬의 가중치가 크로스워드에 배치할 수 있는 단어의 개수라고 할 때,
# 한 번 배치할 때 마다 하나 씩 소모처리 해둠.
for route in routes:
isPossible &= adjMat[source][route] > 0
ans *= adjMat[source][route]
adjMat[source][route] -= 1

isPossible &= adjMat[route][sink] > 0
ans *= adjMat[route][sink]
adjMat[route][sink] -= 1

# 소모시켰던 가중치를 복구함으로서 인접행렬은 원상복구.
for route in routes:
adjMat[source][route] += 1
adjMat[route][sink] += 1

# 두 route가 다르면 대칭으로 뒤집어도 가능하니, 경우의 수도 두 배로...
ans *= len(set(routes))

return ans if isPossible else 0


def solve(N: int, words: list[str]) -> int:
# Adjacent matrix
adjMat = collections.defaultdict(lambda: collections.defaultdict(lambda: 0))
answer = 0
for w in words:
adjMat[w[0]][w[-1]] += 1
for i in range(ALPHABET_LEN): # i = source
for j in range(ALPHABET_LEN): # j = sink
for k in range(ALPHABET_LEN): # k = route1
for l in range(k, ALPHABET_LEN): # l = route2
answer += countCases(adjMat, ALPHABETS[i], ALPHABETS[j], ALPHABETS[k], ALPHABETS[l])
return answer


if __name__ == "__main__":
N = int(sys.stdin.readline())
words = [sys.stdin.readline().strip() for _ in range(N)]
sys.stdout.write(str(solve(N, words)))