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

33-H0ngJu #253

Merged
merged 2 commits into from
Nov 19, 2024
Merged

33-H0ngJu #253

merged 2 commits into from
Nov 19, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Nov 8, 2024

πŸ”— 문제 링크

병원 거리 μ΅œμ†Œν™”

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

50M

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

μ΄λ²ˆμ—λŠ” μ‚Όμ„± SW 기좜 문제 쀑에 ν•˜λ‚˜λ₯Ό μ„ νƒν–ˆμŠ΅λ‹ˆλ‹€!

이 문제λ₯Ό μ–΄λ–»κ²Œ ν’€κΉŒ... ν•˜λ‹€κ°€ N개 쀑에 M개 λ½‘μ•„μ„œ κ·Έλƒ₯ μ΅œμ†Œ 거리 κ°±μ‹ ν•˜λ©΄ λ˜κ² λ‹€ ν•΄μ„œ μ•„λž˜ νλ¦„μœΌλ‘œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

  1. 각 μ‚¬λžŒλ§ˆλ‹€ 각 λ³‘μ›μ˜ 거리λ₯Ό κ³„μ‚°ν•œλ‹€.
  2. 병원 K개 μ€‘μ—μ„œ M개λ₯Ό λ½‘λŠ” μ‘°ν•© 생성
  3. μ‘°ν•©μ—μ„œ ν•˜λ‚˜μ”© κΊΌλ‚΄μ„œ μ΅œμ†Œκ±°λ¦¬ κ°±μ‹ ν•˜κΈ°

2μ—μ„œ μ €λŠ” itertools의 combinations ν•¨μˆ˜λ₯Ό μ‚¬μš©ν–ˆμ–΄μš”

근데 λ­”κ°€ λ¬Έμ œμ—μ„œ μš”κ΅¬ν•˜λŠ”κ²Œ 이게 λ§žλ‚˜? μ‹Άμ–΄μ„œ λ‹€λ₯Έ 풀이λ₯Ό μ°Ύμ•„λ³΄λ˜ 쀑에
μ‚Όμ„± μ½”ν…Œμ—μ„œλŠ” itertools importν•˜λŠ” 것을 λ§‰λŠ”λ‹€κ³  ν•˜λ”λΌκ³ μš”?

κ·Έλž˜μ„œ combinations ν•¨μˆ˜λ₯Ό λ”°λ‘œ κ΅¬ν˜„ν•΄μ„œ ν•œλ²ˆ 더 ν’€μ—ˆμŠ΅λ‹ˆλ‹€.

itertools.combinations(combbination_arr, m)

μ΄λž¬λŠ”λ°

all_combinations = []

def combination(start, depth, combination_arr):
    if depth == m: # m만큼 찾으면 return
        all_combinations.append(combination_arr[:])
        return
    for i in range(start, len(hospital_arr)):
        combination_arr.append(hospital_arr[i]) # μ›μ†Œ 일단 λ„£κ³ 
        combination(i + 1, depth + 1, combination_arr) # μž¬κ·€ 돌림
        combination_arr.pop() # λ‹€μ‹œ 뽑은거 볡귀 -> [1,2,3]을 κ΅¬ν•˜λ©΄, 3λΉΌκ³  λ‹€μ‹œ λ‹€μŒ 반볡문 λ„£μ–΄μ„œ [1,2,4] κ°€λŠ₯

combination(0, 0, [])

μš”λž˜ ν’€μ–΄μ„œ...

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

slicing은 κΉŠμ€ λ³΅μ‚¬μΌκΉŒμš” 얕은 λ³΅μ‚¬μΌκΉŒμš”?

μ €λŠ” slicing이 deepcopy라고 μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€.

첫번째 μΌ€μ΄μŠ€ )

arr = [0,1,2,3,4,5,6]
new_arr = arr[3:]

arr[5] = 777

print(arr, new_arr)
print(id(arr[5]), '=', id(new_arr[2]))

# κ²°κ³Ό 
# [0, 1, 2, 3, 4, 777, 6] [3, 4, 5, 6]
# 1939064674800 = 1939063636336 

μ΄λ‹ˆ new_arrκ³Ό arr이 λ©”λͺ¨λ¦¬ μ£Όμ†Œκ°€ λ‹€λ₯΄λ‹ˆκΉŒμš”


λ‘λ²ˆμ§Έ μΌ€μ΄μŠ€ )
slicing은 얕은 λ³΅μ‚¬λ‹€λΌλŠ” λΈ”λ‘œκ·Έ 글을 보게 λ˜μ—ˆλŠ”λ°μš”

κΈ€μ˜ 예제인 print(id(a[1]), '=', id(b[1]))λ₯Ό 보면 κ²°κ΅­ λ©”λͺ¨λ¦¬ μ£Όμ†Œκ°€ κ°™κ²Œ λ‚˜μ™€μ„œ slicing은 얕은 볡사라고 λ§ν•˜λ”λΌκ³ μš”



a = [[1, 2], [3, 4, 5]]
b = a[:]

print(a, '=', b) # 같은 κ°’ 좜λ ₯
print(id(a), id(b)) # λ°”κΉ₯ list의 λ©”λͺ¨λ¦¬ μ£Όμ†ŒλŠ” 닀름
print(id(a[0]), id(b[0])) # λ‚΄λΆ€ list의 λ©”λͺ¨λ¦¬ μ£Όμ†ŒλŠ” κ°™μŒ

# κ²°κ³Ό
# [[1, 2], [3, 4, 5]] = [[1, 2], [3, 4, 5]]
# 2586361266304 2586361266368
# 2586360892928 2586360892928

2차원 배열을 slicing으둜 볡사λ₯Ό ν•˜λŠ” κ²½μš°μ—,
λ°”κΉ₯ λ¦¬μŠ€νŠΈμ—λ§Œ μ£Όμ†Œκ°€ λ‹¬λΌμ§€λŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.


λ‚΄λΆ€ λ¦¬μŠ€νŠΈλŠ” 같은 곳을 μ°Έμ‘°ν•˜λŠ” κ±°μ£ .

μ €λŠ” slicing을 얕은 볡사라고 μƒκ°ν•˜κΈ°λ‘œ ν–ˆμŠ΅λ‹ˆλ‹€. (근데 κΉŠμ€ 볡사인척 ν•˜λŠ”...)

λ‹€λ₯Έ 뢄듀은 slicing이 얕은 볡사, κΉŠμ€ 볡사 쀑 무엇이라고 μƒκ°ν•˜μ‹œλ‚˜μš”?

@9kyo-hwang
Copy link

λ¬Έμ œκ°€ λˆˆμ— 읡닀 ν–ˆλ”λ‹ˆ... 백쀀에 15686 μΉ˜ν‚¨ 배달 λ¬Έμ œλž‘ μ™„λ²½ν•˜κ²Œ λ™μΌν•˜λ„€μš” γ…‹γ…‹
μš”κ±°λ‘œ 점수 λ‹¬λ‹¬ν•˜κ²Œ μ±™κΈ°μ‹œμ£  :>

@9kyo-hwang
Copy link

μΆ”κ°€λ‘œ 리슀트 μŠ¬λΌμ΄μ‹±μ€ λ§μ”€ν•˜μ‹  κ²ƒμ²˜λŸΌ "얕은 볡사"μž…λ‹ˆλ‹€.
aλΌλŠ” 리슀트λ₯Ό μŠ¬λΌμ΄μ‹±ν•΄μ„œ b에 λŒ€μž…ν•˜κ²Œ 되면, a와 bλŠ” μ„œλ‘œ λ‹€λ₯Έ μ£Όμ†Œλ₯Ό κ°€μ§€λ‚˜ 각각의 μ›μ†ŒλŠ” 같은 μ£Όμ†Œλ₯Ό κ°€λ¦¬ν‚΅λ‹ˆλ‹€. list μ•ˆμ— mutableν•œ 개체인 listκ°€ 또 μžˆλŠ” ꡬ쑰이기 λ•Œλ¬Έμž…λ‹ˆλ‹€.
ν•΄λ‹Ή κ²Œμ‹œκΈ€μ΄ 많이 도움될 것 κ°™μŠ΅λ‹ˆλ‹€.

@tgyuuAn
Copy link
Member

tgyuuAn commented Nov 14, 2024

μŠ¬λΌμ΄μ‹±μ€ 얇은 λ³΅μ‚¬μ—μš”!
νŒŒμ΄μ¬μ—μ„œ κΉŠμ€ λ³΅μ‚¬λŠ” import copy 에 copy.deepcopy() 밖에 μ—†λŠ”κ±Έλ‘œμ••λ‹ˆλ‹€!!! (λ§žλ‚˜μš” ?)

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.

from itertools import combinations
import sys

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

N, M = map(int, input().split())
hospital = []
people = []
for row in range(N):
    _input = list(map(int, input().split()))
    for col in range(N):
        if _input[col] == 1:
            people.append((row, col))
        elif _input[col] == 2:
            hospital.append((row, col))      

answer = int(1e9)
for items in combinations(hospital, M):
    dedicate = 0
    for person in people:        
        _min = int(1e9)

        for item in items:
            temp = abs(item[0] - person[0]) + abs(item[1] - person[1])
            _min = min(_min, temp)

        dedicate += _min
    answer = min(answer, dedicate)

print(answer)

itertools... κ°œμ‚¬κΈ°κΈ΄ν•˜μ£ ....
백쀀에 Nκ³Ό M μ‹œλ¦¬μ¦ˆ λ¬Έμ œμ§‘ itertools μ•ˆμ“°κ³  ν’€λ©΄ νš¨κ³Όμ μ΄μ—μš”!
문제 μ œλŒ€λ‘œ μ•ˆμ½κ³  40λΆ„ λ‚ λ¦Ό..

@H0ngJu
Copy link
Collaborator Author

H0ngJu commented Nov 19, 2024

μΆ”κ°€λ‘œ 리슀트 μŠ¬λΌμ΄μ‹±μ€ λ§μ”€ν•˜μ‹  κ²ƒμ²˜λŸΌ "얕은 볡사"μž…λ‹ˆλ‹€. aλΌλŠ” 리슀트λ₯Ό μŠ¬λΌμ΄μ‹±ν•΄μ„œ b에 λŒ€μž…ν•˜κ²Œ 되면, a와 bλŠ” μ„œλ‘œ λ‹€λ₯Έ μ£Όμ†Œλ₯Ό κ°€μ§€λ‚˜ 각각의 μ›μ†ŒλŠ” 같은 μ£Όμ†Œλ₯Ό κ°€λ¦¬ν‚΅λ‹ˆλ‹€. list μ•ˆμ— mutableν•œ 개체인 listκ°€ 또 μžˆλŠ” ꡬ쑰이기 λ•Œλ¬Έμž…λ‹ˆλ‹€. ν•΄λ‹Ή κ²Œμ‹œκΈ€μ΄ 많이 도움될 것 κ°™μŠ΅λ‹ˆλ‹€.

저도 μ˜¬λ €μ£Όμ‹  κ²Œμ‹œκΈ€ 보고 μ•Œκ²Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€
NodKermitNodGIF

@H0ngJu
Copy link
Collaborator Author

H0ngJu commented Nov 19, 2024

μŠ¬λΌμ΄μ‹±μ€ 얇은 λ³΅μ‚¬μ—μš”! νŒŒμ΄μ¬μ—μ„œ κΉŠμ€ λ³΅μ‚¬λŠ” import copy 에 copy.deepcopy() 밖에 μ—†λŠ”κ±Έλ‘œμ••λ‹ˆλ‹€!!! (λ§žλ‚˜μš” ?)

μ˜™ 저도 κ·Έλ ‡κ²Œ μ•Œκ³  μžˆμŠ΅λ‹ˆλ‹€ ~. ~

@H0ngJu H0ngJu merged commit 3a46632 into main Nov 19, 2024
@H0ngJu H0ngJu deleted the 33-H0ngJu branch November 19, 2024 02:24
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