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

22-seongwon030 #80

Merged
merged 1 commit into from
Aug 6, 2024
Merged

22-seongwon030 #80

merged 1 commit into from
Aug 6, 2024

Conversation

seongwon030
Copy link
Collaborator

πŸ”— 문제 링크

μ•ŒνŒŒλ²³

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

3μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

이 λ¬Έμ œμ—μ„œλŠ” 상, ν•˜, 쒌, 우둜 νƒμƒ‰ν•˜λ©° μ§€κΈˆκΉŒμ§€ μ§€λ‚˜μ˜¨ λͺ¨λ“  칸의 μ•ŒνŒŒλ²³κ³ΌλŠ” λ‹€λ₯Έ 경둜둜 이동해야 ν•œλ‹€. μ΅œλŒ€ 이동할 수 μžˆλŠ” 칸의 수λ₯Ό κ΅¬ν•œλ‹€. 참고둜 첫 칸도 ν¬ν•¨ν•œλ‹€.

첫번째 풀이

r, c = map(int, input().split())

k = [list(input()) for _ in range(r)]

ans = 0
alpha = set()
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def dfs(x, y, count):
    global ans
    ans = max(ans, count)
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if 0 <= nx < r and 0 <= ny < c and not k[nx][ny] in alpha:
            alpha.add(k[nx][ny])
            dfs(nx, ny, count+1)
            alpha.remove(k[nx][ny])

alpha.add(k[0][0])
dfs(0, 0, 1)
print(ans)
  • alpha 집합에 첫 번째 μ’Œν‘œμ˜ μ•ŒνŒŒλ²³μ„ μΆ”κ°€
  • 상, ν•˜, 쒌, 우둜 μ΄λ™ν•˜λ©° μ’Œν‘œκ°€ μœ νš¨ν•˜λ©΄μ„œ λ‹€μŒμ— μžˆλŠ” μ•ŒνŒŒλ²³μ΄ μ€‘λ³΅λ˜μ§€ μ•ŠλŠ”λ‹€λ©΄ alpha 집합에 μΆ”κ°€
  • λ‹€μŒ μ’Œν‘œλ₯Ό κΈ°μ€€μœΌλ‘œ λ‹€μ‹œ μœ„μ˜ μž‘μ—…μ„ 반볡
  • μž¬κ·€λ₯Ό μ’…λ£Œ ν•œ ν›„μ—λŠ” ν˜„μž¬ μœ„μΉ˜μ—μ„œ μ΄λ™ν–ˆλ˜ μ•ŒνŒŒλ²³μ„ alpha μ§‘ν•©μ—μ„œ 제거

😒 이 방법은 λ°±νŠΈλž˜ν‚ΉμœΌλ‘œ λͺ¨λ“  경둜λ₯Ό νƒμƒ‰ν•˜κΈ° λ•Œλ¬Έμ— μ‹œκ°„μ΄ λ„ˆλ¬΄ 많이 κ±Έλ Έλ‹€

λ‘λ²ˆμ§Έ 풀이

def bfs(original, x, y):
    ans = 1
    q = set([(x, y, k[y][x])])  
    while q:
        x, y, alpha = q.pop()  # μ’Œν‘œμ™€ μ•ŒνŒŒλ²³
        print(x,y,alpha)
        ans = max(ans, len(alpha)) # μ΅œλŒ€κ°’ κ°±μ‹ 
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < c and 0 <= ny < r and k[ny][nx] not in alpha:
                # μ’Œν‘œλ‚΄μ— 있고 μ•ŒνŒŒλ²³μ΄ μ§‘ν•©μ•ˆμ— μ—†λ‹€λ©΄ μ’Œν‘œμ™€ κΈ°μ‘΄μ•ŒνŒŒλ²³μ„ λ”ν•˜κΈ° 
                q.add((nx, ny, alpha + original[ny][nx]))  
    return ans

r, c = map(int, input().split())
k = []
for _ in range(r):
    k.append(list(input()))

dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

print(bfs(k, 0, 0))
  • μ’Œν‘œλ‚΄μ— 있고 μ•ŒνŒŒλ²³μ΄ μ§‘ν•©μ•ˆμ— μ—†λ‹€λ©΄ "μƒˆλ‘œμš΄ μ•ŒνŒŒλ²³κ³Ό κΈ°μ‘΄ μ•ŒνŒŒλ²³μ„ λ”ν•˜λŠ”" λ°©μ‹μœΌλ‘œ λ°”κΎΈμ—ˆλ‹€
  • μž¬κ·€ν•  ν•„μš”μ—†μ΄ q집합에 (x,y, μ•ŒνŒŒλ²³λ¬Έμžμ—΄) 을 μΆ”κ°€ν•˜λ©° λ°˜λ³΅ν•œλ‹€
스크란샷 2024-07-31 00 30 22

μ‹œκ°„ 비ꡐ

스크란샷 2024-07-31 00 30 53

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

νŒŒμ΄μ¬μ—μ„œ in의 μ‹œκ°„ λ³΅μž‘λ„κ°€ μ–Όλ§ˆλ‚˜ 걸릴지 λͺ¨λ₯΄κ² μ§€λ§Œ μ•ŒνŒŒλ²³μ˜ 경우 26μžμ΄λ‹ˆ booleanν˜•μ‹μ˜ 배열을 26개 선정해두고 λ°±νŠΈλž˜ν‚Ήν• λ•Œλ§ˆλ‹€ μžλ¦¬μ— ν•΄λ‹Ήν•˜λŠ” μ•ŒνŒŒλ²³λ“€μ„ false둜 λΉ„μ›Œμ£ΌλŠ” ν˜•μ‹λ„ μ’‹μ•„λ³΄μ΄λ„€μš”!

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ°˜λ³΅ν•΄μ„œ μ‚¬μš©ν•˜λŠ”κ²Œ μ•„λ‹ˆλΌλ©΄ ꡳ이 ν•¨μˆ˜ν™” ν•  ν•„μš”κ°€ 없을 것 κ°™μ•„μš”! λ³€μˆ˜ μ‚¬μš©μ΄λ‚˜ 흐름 νŒŒμ•… μΈ‘λ©΄μ—μ„œλ„ λ³΅μž‘ν•˜κΈ°λ„ ν•˜κ³ .. λ‚˜λˆˆλ‹€κ³  ν•˜λ©΄ μž…λ ₯뢀와 ν•΄κ²°λΆ€λ‘œ 역할을 λ‚˜λˆ„λ©΄ 이해에 도움이 될 것 κ°™μ•„μš”!

@seongwon030 seongwon030 merged commit 10b377e into main Aug 6, 2024
1 check passed
@seongwon030 seongwon030 deleted the 22-seongwon030 branch August 6, 2024 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants