-
Notifications
You must be signed in to change notification settings - Fork 2
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
35-Munbin-Lee #140
35-Munbin-Lee #140
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import sys
word_length, word_count = map(int,sys.stdin.readline().split())
answer = []
word_list = []
for _ in range(word_count):
word_list.append(sys.stdin.readline())
def is_magic_square(idx_bundle):
for col in range(len(idx_bundle)):
for row in range(len(idx_bundle)):
if word_list[idx_bundle[row]][col] != word_list[idx_bundle[col]][row]: return False
return True
def dfs(idx_bundle, visited, word_count, word_length):
global answer
if len(idx_bundle) == word_length:
if is_magic_square(idx_bundle):
magic_square = ""
for idx in idx_bundle:
magic_square += word_list[idx]
answer.append(magic_square)
return
for idx in range(word_count):
if idx in visited: continue
visited.add(idx)
idx_bundle.append(idx)
dfs(idx_bundle, visited,word_count, word_length)
visited.discard(idx)
idx_bundle.pop()
return
dfs(list(), set(), word_count, word_length)
answer.sort()
if answer: print(answer[0])
else: print("NONE")
μ λ μνμΌλ‘ νμμλλ€.
κ·Όλ° μκ°μ΄ λ무 μ€λ κ±Έλ €μ λ¬ΈλΉλ μ½λ 보λκΉ μ μ΄ μ©μ© λ²μ΄μ§λ€μ
λ¬Έμ μλλ μνμΈ κ² κ°μλ° νμ΄μ¬ permutations
μΌλ‘ μ κ·Όνλ μμ΄λμ΄ λ¬΄λ¦ ν μ³€κ³
μ λ ¬ν΄μ 맨 μ²μ κΊΌ μ μΆνλ κ±°μμ 무λ¦μ νν μ³€λ€μ γ γ·γ·;;
words = sorted(stdin.read().splitlines()) | ||
|
||
# λ¨μ΄ μμ΄ xκ° λ¨μ΄ λ§λ°©μ§μΈμ§ νμΈνλ ν¨μ | ||
def isValid(x): | ||
for i in range(L): | ||
for j in range(L): | ||
if x[i][j] != x[j][i]: return False | ||
|
||
return True | ||
|
||
for perm in permutations(words, L): | ||
if not isValid(perm): continue | ||
|
||
print(*perm, sep='\n') | ||
exit() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ¨μ΄λ₯Ό μ λ ¬ν λ€μ
permutationsμΌλ‘ μ²μ λμ€λ κ±°λ₯Ό μ μΆνλ©΄ μνν νμλ μμ΄
μ²μ 걸리λ κ±°μμ λ°λ‘ μ μΆνλ©΄ λλκΉ
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
μ§λ¦¬λλ°μ..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ·Όλ° μ μ²μ κ²μ΄ λ΅μΈμ§λ λͺ¨λ¦μ.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ·Όλ° μ μ²μ κ²μ΄ λ΅μΈμ§λ λͺ¨λ¦μ.
μλλ λͺ¨λ κ²½μ°μ μλ₯Ό ꡬν΄μ ꡬν΄μ§ μ λ΅ ν보μλ€μμ μ λ ¬ν΄μ 첫 λ²μ§Έ κΊΌλ₯Ό μ μΆν΄μΌνλλ° (μ¬μ μμΌλ‘)
κ·Όλ° μ΄μ°¨νΌ μ λ ¬ ν λ€μ κ°μ₯ 첫 λ²μ§Έ λμ€λ μΉκ΅¬λ μ λ ¬λ μνμμ λ½μκΈ° λλ¬Έμ μ΄μ°¨νΌ μ¬μ μ 첫 λ²μ§Έ μμμΈ κ±°μ£ ...
κ°λλνλ°μ?
μ΄ λ¬Έμ μ¬λ°λ€μ λΉμ·ν λ¬Έμ λ°λ‘ νμ΄λ³΄λ¬ κ°λλ€ from sys import *
from collections import *
stdin = open('λ¨μ΄ λ§λ°©μ§.txt')
L, N = map(int, stdin.readline().split())
words = [stdin.readline().strip() for _ in range(N)]
words.sort()
def check(arr):
length = len(arr)
if length == 2:
if arr[0][1] == arr[1][0]:
return True
return False
for x in range(length):
for c in range(x + 1, length):
if arr[x][c] != arr[c][x]:
return False
return True
def dfs(result, indices, words):
if len(indices) == L:
for r in range(L):
for c in range(L):
if result[r][c] != result[c][r]:
return
for word in result:
print(word)
exit()
for i in range(N):
if i not in indices:
indices.add(i)
result.append(words[i])
dfs(result, indices, words)
result.pop()
indices.remove(i)
t_result = deque()
dfs(t_result, set(), words)
print('NONE') |
|
π λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/24891
βοΈ μμλ μκ°
2μκ°
β¨ μλ μ½λ
λ¨Όμ λΈλ£¨νΈν¬μ€ νμ΄λ₯Ό μκ°ν΄λ³΄μ.
λͺ¨λ κ²½μ°μ λ¨μ΄ μμ΄μ ꡬνλ €λ©΄$_N\mathrm{P}_L$ μ μκ°μ΄ νμνλ€.
κ° λ¨μ΄ μμ΄μ λνμ¬, λ¨μ΄ λ§λ°©μ§μΈμ§ νμΈνλ €λ©΄$L^2$ μ μκ°μ΄ νμνλ€.
λ°λΌμ, λΈλ£¨νΈν¬μ€ μκ³ λ¦¬μ¦μ μκ° λ³΅μ‘λλ$O(_N\mathrm{P}_L L^2)$ = 20 * 19 * 18 * 17 * 16 * 5 * 5 = 46512000 μ΄λ―λ‘ μκ° λ΄μ ν΅κ³Όν μ μλ€.
νμ΄μ¬μ
itertools.permutaions
λ‘ λ λ¨Ήν΄λ³΄μ.무μ§μ± λΈλ£¨νΈν¬μ€λ‘λ μΆ©λΆν ν΅κ³Όν μ μλ€.
μ¬μ€ λ μ²μμλ λ°±νΈλνΉμΌλ‘ λ¨Όμ νλ €κ³ νλ€.
λ¨μ΄λ€μ μ¬μ μμΌλ‘ μ λ ¬νκ³ , BFSλ‘ μ μΌ λ¨Όμ νμμ μ±κ³΅ν λ¨μ΄ λ§λ°©μ§μ΄ λ΅μΌ κ²μ΄λΌκ³ μκ°νλ€.
νμ§λ§ νλ Έλ€.
κ·Έλμ BFSλ‘ κ·Έλ₯ λͺ¨λ λ¨μ΄ λ§λ°©μ§μ λ°±νΈλνΉ νμνμλ€.
C++μΈ κ²μ κ°μνμ λ, λΈλ£¨νΈν¬μ€(pypy3)μ λ°±νΈλνΉ(C++)μ μκ° μ°¨μ΄κ° μΌλ§ λμ§ μλλ€.
κ·ΈλΌ μ΄λ»κ² ν¨μ¨μ μΌλ‘ ν μ μμκΉ?
λ¨μ΄λ₯Ό μ λ ¬νκ³ , bfs λμ dfsλ₯Ό λλ €μ μ²μμ λμ€λ λ¨μ΄ λ§λ°©μ§μ μΆλ ₯νμλλ λ§μλ€.
μκ°μ΄ μ λ° κ°λμΌλ‘ μ€μ΄λ€μλ€.
indexκ° λ§μ§λ§μΌ λλ§ μ²΄ν¬νμ§ λ§κ³ , μ€κ° μ€κ°μ λ¨μ΄λ§λ°©μ§μΈμ§ 체ν¬νμ¬ λΈλ£¨νΈν¬μ€->λ°±νΈλνΉμΌλ‘ λ°κΎΈλ©΄ ν¨μ¬ λ λΉ¨λΌμ§ κ²μ΄λ€.
μ BFSκ° μλλΌ DFSλ₯Ό λλ €μΌν κΉ?
λͺ¨λ₯΄κ² λ€.
κ·Έλ°λ°,
itertools.permutations
μ νμ μμλ₯Ό μκ°ν΄λ³΄λ dfsμ νμ μμμ κ°λ€.κ·ΈλΌ κ·Έλ₯ dfsλ₯Ό ꡬννμ§ λ§κ³ 첫λ²μ§Έ permμ μΆλ ₯νλ©΄ λλ κ² μλκ°?
permutations νΉμ± μ μ΅μ νκ° λμ΄μμ΄μ μΈ λ²μ§Έ νμ΄λ³΄λ€λ μ΄μ§ λΉ λ₯΄λ€.