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

14-H0ngJu #183

Merged
merged 3 commits into from
May 10, 2024
Merged

14-H0ngJu #183

merged 3 commits into from
May 10, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented May 9, 2024

πŸ”— 문제 링크

μΉ˜ν‚¨ κ²Œμž„

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

2H

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

λ°±νŠΈλž˜ν‚Ήμ΄ κ°œλ…μ μœΌλ‘œ λ­”μ§€λŠ” μ•Œκ² λŠ”λ° 문제 적용이 νž˜λ“  것 κ°™μ•„μ„œ μš” 문제λ₯Ό κ³¨λžμŠ΅λ‹ˆλ‹€ ν—ˆν—ˆ


문제 μš”μ•½
λ¬Έμ œμ—μ„œ μ œμ‹œλœ μΉ˜ν‚¨ 집 개수λ₯Ό M개만 μ‚΄λ €λ‘μ—ˆμ„ λ•Œ,
전체 μΉ˜ν‚¨ 거리가 κ°€μž₯ μž‘μ€ M개의 쑰합을 κ΅¬ν•˜μ—¬ μΉ˜ν‚¨ 거리의 합을 λ°˜ν™˜ν•˜μž@!




이 문제λ₯Ό ν’€λ©΄μ„œ κ°€μž₯ 처음 ν’€μ–΄μ•Όν•˜λŠ” λ¬Έμ œλŠ”

μ–΄λ–»κ²Œ κ°€λŠ₯ν•œ M개의 μΉ˜ν‚¨ 집 쑰합을 찾을 것인가? μž…λ‹ˆλ‹€.

μ²˜μŒμ—λŠ” Mκ°œκ°€ 될 λ•ŒκΉŒμ§€ 1κ°œμ”© μ€„μ—¬λ‚˜κ°€λ©΄μ„œ μ΅œμ†Œ μΉ˜ν‚¨ 거리λ₯Ό μ°Ύμ„κΉŒ? ν–ˆλŠ”λ°..

λ­”κ°€ 배보닀 배꼽이 μ»€μ§€λŠ” λŠλ‚Œμ΄λΌ λΉ λ₯΄κ²Œ pass ν–ˆμŠ΅λ‹ˆλ‹€.



근데 쑰합에 λŒ€ν•΄μ„œ μ°Ύμ•„λ³΄λ‹ˆκΉŒ νŒŒμ΄μ¬μ—μ„œ μ œκ³΅ν•˜λŠ” itertoolsκ°€ 있길래 importν•΄μ„œ μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€

* combinations(list, N)을 ν•˜κ²Œλ˜λ©΄ λ°˜ν™˜ 값은 tuple이 λ©λ‹ˆλ‹€.


κ·Έλ ‡κ²Œ μΉ˜ν‚¨ 집을 μ €μž₯ν•΄λ‘” list에 λŒ€ν•˜μ—¬ combinationsλ₯Ό ν•˜λ©΄ μ†μ‰½κ²Œ λͺ¨λ“  μ‘°ν•©μ˜ 경우의 수λ₯Ό ꡬ할 수 μžˆμŠ΅λ‹ˆλ‹€.

μ΄ν›„μ—λŠ” λͺ¨λ“  집듀을 돌며, λͺ¨λ“  μΉ˜ν‚¨ 집에 λŒ€ν•œ μΉ˜ν‚¨ 거리λ₯Ό κ΅¬ν•©λ‹ˆλ‹€.


μ €λŠ” μ—¬κΈ°μ„œ ꡳ이 λͺ¨λ“  μΉ˜ν‚¨μ§‘μ— λŒ€ν•œ 거리λ₯Ό ꡬ할 ν•„μš”κ°€ μžˆλ‚˜? μ–΄λŠ μˆœκ°„ ν™•μΈν•˜μ§€ μ•Šμ•„λ„ λ˜λŠ” μΉ˜ν‚¨μ§‘μ΄ μžˆμ„ 것 같은데.. 라고 μƒκ°ν•΄μ„œ λΊ‘λΊ‘ λ‘˜λŸ¬κ°”μ§€λ§Œ,,,


κ²°λ‘ λΆ€ν„° λ§ν•˜λ©΄ κ·Έλƒ₯ ν•œ 집에 λŒ€ν•˜μ—¬ λͺ¨λ“  μΉ˜ν‚¨ 집에 λŒ€ν•œ μΉ˜ν‚¨ 거리λ₯Ό κ΅¬ν•˜κ³ , min을 λ°˜ν™˜ν•˜λŠ” 것이 κ°€μž₯ κ°„λ‹¨ν–ˆμŠ΅λ‹ˆλ‹€.

κ·Έλ ‡κ²Œ 각 쑰합에 λŒ€ν•œ μΉ˜ν‚¨ 거리 합을 κ΅¬ν•˜κ³ , min을 λ°˜ν™˜ν•΄μ£Όλ©΄ 문제λ₯Ό ν’€ 수 μžˆμŠ΅λ‹ˆλ‹€!


둜직 μ§œλŠ”λ° λͺ‡λ²ˆ μ‚½μ§ˆν•˜κ³  tmp μ΄ˆκΈ°ν™”λ₯Ό μ—‰λš±ν•œ 곳에 ν•΄μ„œ μ‹œκ°„μ„ 많이 μΌλ„€μš” ν—ˆν—ˆ πŸ˜…πŸ˜…

λΆ„λͺ… μ–΄μ œλŠ” μͺΌλ§€ μ–΄λ €μ› λŠ”λ° 였늘 λ³΄λ‹ˆκΉŒ κ·Έμ •λ„λŠ” μ•„λ‹Œ 것 같기도? ν•˜λ„€μš”?

import sys
from itertools import combinations

def input(): return sys.stdin.readline().rstrip()

N, M = map(int, input().split())
house = [[] for _ in range(N)]
chicken_house = []
result = 99999

for i in range(N):
    house[i].extend(map(int, input().split()))

for r in range(N):
    for c in range(N):
        if house[r][c] == 2:
            chicken_house.append((r, c))

for chickens in combinations(chicken_house, M):
    total = 0
    for r in range(N):
        for c in range(N):
            if house[r][c] == 1:
                tmp = 9999
                for chicken in chickens:
                    tmp = min(tmp, abs(r - chicken[0]) + abs(c - chicken[1]))
                total += tmp
    
    result = min(result, total)

print(result)

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

λ°±νŠΈλž˜ν‚ΉμœΌλ‘œ κ΅¬ν˜„ν•˜λ‹€κ°€ μ–΄λ””μ„œ 잘λͺ»λλŠ”지....

홍주λ₯΄ PR 보고 combinations 보고 이마 νƒμ³€μ”λ‹ˆλ‹€.........................

image

λ¦¬λ§ˆμΈλ“œ μ‹œμΌœμ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€ ............ γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ…  😒

Comment on lines +20 to +30
for chickens in combinations(chicken_house, M):
total = 0
for r in range(N):
for c in range(N):
if house[r][c] == 1:
tmp = 9999
for chicken in chickens:
tmp = min(tmp, abs(r - chicken[0]) + abs(c - chicken[1]))
total += tmp

result = min(result, total)
Copy link
Member

Choose a reason for hiding this comment

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

와 μ§„μ§œ combinations μ•ˆλ– μ˜¬λΌκ°€μ§€κ³  λ°±νŠΈλž˜ν‚ΉμœΌλ‘œ κ΅¬ν˜„ν•˜κ³  μžˆμ—ˆλŠ”λ°..

μ‹œκ°„μ΄ˆκ³Όλ‚˜μ„œ ν™”λ”±μ§€λ‚˜μ„œ PRλ΄€λŠ”λ° μ§„μ§œ ν—ˆλ¬΄ν•˜λ„€μš”.................

μ§„μ§œ 감 λ‹€μ£½μ—ˆλ”°........... γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… γ… 

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

저어도 combinations μ•Œκ²Œλ˜κΈ° μ „κΉŒμ§€ λ§Žμ€ μ‚½μ§ˆμ΄ μžˆμ—ˆλ‹€μ£  .. γ…‹..γ…‹.γ…‹..

@H0ngJu H0ngJu merged commit 2810ea2 into main May 10, 2024
@H0ngJu H0ngJu deleted the 14-H0ngJu branch May 10, 2024 14:43
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.

2 participants