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

38-pknujsp #154

Merged
merged 3 commits into from
May 3, 2024
Merged

38-pknujsp #154

merged 3 commits into from
May 3, 2024

Conversation

pknujsp
Copy link
Collaborator

@pknujsp pknujsp commented Mar 7, 2024

πŸ”— 문제 링크

μžλ¬Όμ‡ μ™€ μ—΄μ‡ 

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

2μ‹œκ°„, λ„μ €νžˆ μ•ˆ λ– μ˜¬λΌμ„œ 풀이보고 곡뢀

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

NxN 크기의 μžλ¬Όμ‡ μ™€ MxM 크기의 μ—΄μ‡ κ°€ 있고, μ—΄μ‡ λ₯Ό νšŒμ „μ‹œν‚€κ±°λ‚˜ ν•œμΉΈ μ”© μ΄λ™μ‹œμΌœλ³΄λ©΄μ„œ μžλ¬Όμ‡ μ— μ—΄μ‡ κ°€ λΌμ›Œμ§€λŠ”μ§€ ν™•μΈν•˜λŠ” 문제

μžλ¬Όμ‡ μ™€ μ—΄μ‡ μ˜ μ΅œλŒ€ν¬κΈ°κ°€ 20x20 μ΄ν•˜μ΄λ―€λ‘œ, μ™„μ „ νƒμƒ‰μœΌλ‘œ ν’€ 수 μžˆλ‹€.

1. μžλ¬Όμ‡ μ˜ 크기λ₯Ό 3λ°° ν‚€μš΄λ‹€

  • μ—΄μ‡ λ₯Ό μƒν•˜μ’Œμš°λ‘œ μ΄λ™μ‹œμΌ°μ„ λ•Œ μžλ¬Όμ‡ λ₯Ό λ²—μ–΄λ‚˜λŠ” κ²½μš°κ°€ 생긴닀
  • 이 λ•Œ λ„€ λ°©ν–₯에 λŒ€ν•΄μ„œ λ²—μ–΄λ‚œ μƒνƒœμΈμ§€ 확인을 ν•˜λŠ” λ‘œμ§μ„ μž‘μ„±ν•΄μ•Ό ν•˜λŠ”λ° ꡉμž₯히 λ³΅μž‘ν•΄μ§„λ‹€
  • μˆ˜μ›”ν•˜κ²Œ μ½”λ“œλ₯Ό μž‘μ„±ν•˜κΈ° μœ„ν•΄μ„œ 3λ°° 만큼 ν‚€μ›Œμ„œ 진행할 수 μžˆλ‹€.
  • 1ν–‰ 1μ—΄ λΆ€ν„° N * 2ν–‰ N * 2μ—΄ κΉŒμ§€ μ—΄μ‡ λ₯Ό μ΄λ™μ‹œμΌœλ³΄λ©΄μ„œ μ™„μ „ 탐색을 μˆ˜ν–‰ν•œλ‹€.

κ·Έλ¦Ό1

  • μ „ν˜€ λ– μ˜¬λ¦¬μ§€ λͺ»ν•œ λΆ€λΆ„
  • μ—„μ²­ λ³΅μž‘ν•œ 반볡문과 뢄기문을 μ‚¬μš©ν•΄μ„œ μ—΄μ‡ λ₯Ό λŒλ¦¬λŠ” κ²ƒλ§Œ μƒκ°ν–ˆλ‹€

2. λ„€ 번 반볡 : ν•œλ²ˆ νšŒμ „ν•˜κ³  μ—΄μ‡ κ°€ λΌμ›Œμ§€λŠ”μ§€ λͺ¨λ“  경우 탐색

  • 90도 νšŒμ „ν•œλ‹€
  • μ—΄μ‡ λ₯Ό μžλ¬Όμ‡ μ™€ 맞물릴 λ•Œ κΉŒμ§€ ν•œ μΉΈμ”© μ΄λ™μ‹œμΌœλ³Έλ‹€
    • νŠΉμ • μΉΈ 만큼 μ΄λ™ν•˜λŠ” 방법은 μ•Œλ§žλŠ” μžλ¬Όμ‡  λ°°μ—΄ μœ„μΉ˜μ—
    • μ—΄μ‡  λ°°μ—΄μ˜ μ›μ†Œ 값을 각각 λ”ν•΄λ³΄λŠ” μ‹μœΌλ‘œ ν•  수 μžˆλ‹€
    • μžλ¬Όμ‡  λ°°μ—΄κ³Ό μ—΄μ‡  λ°°μ—΄μ˜ μ›μ†Œ 각각의 합이 λͺ¨λ‘ 1 이라면 λΌμ›Œμ§„ 것이닀
    • 열리면 True λ₯Ό λ°˜ν™˜ν•˜μ—¬ μ’…λ£Œν•œλ‹€
    • μžλ¬Όμ‡ κ°€ 열리지 μ•ŠμœΌλ©΄ μžλ¬Όμ‡  λ°°μ—΄ μ›μ†Œλ₯Ό λͺ¨λ‘ λ³΅κ΅¬μ‹œν‚¨λ‹€

μ—΄μ‡  νšŒμ „ 방법

  • C μ—΄μ˜ λͺ¨λ“  μ›μ†Œλ₯Ό κ·ΈλŒ€λ‘œ C ν–‰μœΌλ‘œ λ§Œλ“€λ©΄ λœλ‹€.
  • μ—΄μ‡  λ°°μ—΄μ˜ 행을 λ°˜λŒ€λ‘œ λ’€μ§‘μ–΄μ„œ 일일이 νšŒμ „ν•œ μ—΄μ‡  배열에 값을 λŒ€μž…ν•˜λ„λ‘ κ΅¬ν˜„ν–ˆλŠ”λ°
  • 파이썬의 zip ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λ©΄ ν•œ μ€„λ‘œ κ΅¬ν˜„ν•  수 μžˆμŒμ„ μ•Œκ²Œλ˜μ—ˆλ‹€
rev_key = key[::-1]
key = []
for c in range(k):
    row = []
    for r in range(k):
        row.append(rev_key[r][c])
    key.append(row)

"""
μ—΄μ‡ λ₯Ό λŒλ¦¬λŠ” λ‘œμ§μ€ ν•œ μ€„λ‘œλ„ κ΅¬ν˜„κ°€λŠ₯ ν•©λ‹ˆλ‹€
key = [row for row in zip(*reversed(key))]
"""

κ·Έλ¦Ό2

3. μ–΄λ– ν•œ κ²½μš°μ—λ„ μ•ˆ 열리면 False λ₯Ό λ°˜ν™˜ν•œλ‹€

# μžλ¬Όμ‡ μ˜ 쀑간 뢀뢄이 λͺ¨λ‘ 1인지 확인
def is_valid(new_lock):
    length = len(new_lock) // 3
    
    for r in range(length, length * 2):
        for c in range(length, length * 2):
            if new_lock[r][c] != 1:
                return False
            
    return True

def solution(key, lock):
    n = len(lock)
    k = len(key)
    new_lock = [[0] * (n * 3) for _ in range(n * 3)]
    
    for r in range(n):
        for c in range(n):
            new_lock[r + n][c + n] = lock[r][c]
            
    for _ in range(4):
        rev_key = key[::-1]
        key = []
        for c in range(k):
            row = []
            for r in range(k):
                row.append(rev_key[r][c])
            key.append(row)
        
        """
        μ—΄μ‡ λ₯Ό λŒλ¦¬λŠ” λ‘œμ§μ€ ν•œ μ€„λ‘œλ„ κ΅¬ν˜„κ°€λŠ₯ ν•©λ‹ˆλ‹€
        key = [row for row in zip(*reversed(key))]
        """

        for r in range(n * 2):
            for c in range(n * 2):
                # μžλ¬Όμ‡ μ— μ—΄μ‡ λ₯Ό λΌμš΄λ‹€
                for i in range(k):
                    for j in range(k):
                        new_lock[r + i][c + j] += key[i][j]
                        
                # μžλ¬Όμ‡ μ— μ—΄μ‡ κ°€ λ”± λ“€μ–΄κ°”λŠ”μ§€ 확인
                if is_valid(new_lock):
                    return True
                
                # μžλ¬Όμ‡ μ—μ„œ μ—΄μ‡ λ₯Ό λΉΌμ„œ λ³΅κ΅¬μ‹œν‚¨λ‹€
                for i in range(k):
                    for j in range(k):
                        new_lock[r + i][c + j] -= key[i][j]
    return False

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

Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

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

3λ°°λ₯Ό 더 ν‚€μ›Œμ„œ μ ‘κ·Όν•œλ‹€λŠ” 건 생각도 λͺ»ν–ˆλŠ”데 ν™•μ‹€νžˆ ν‚€μš°λ‹ˆκΉŒ λ°°μ—΄μ—μ„œ λ²—μ–΄λ‚œ 경우의 μ²˜λ¦¬κ°€ μˆ˜μ›”ν•˜λ„€μš”.. λ²—μ–΄λ‚œ 경우, 배열을 μΆ”κ°€λ‘œ ν™•μž₯ν•  ν•„μš” μ—†κ³ , 움직일 λ•Œλ§ˆλ‹€ μžλ¬Όμ‡ +μ—΄μ‡ μ˜ 값이 λͺ¨λ‘ 1이면 λ˜λ‹ˆκΉŒμš”

문제 ν‘ΈλŠλΌ μˆ˜κ³ λ§ŽμœΌμ…¨μŠ΅λ‹ˆλ‹€ πŸ”₯πŸ”₯


"""
μ—΄μ‡ λ₯Ό λŒλ¦¬λŠ” λ‘œμ§μ€ ν•œ μ€„λ‘œλ„ κ΅¬ν˜„κ°€λŠ₯ ν•©λ‹ˆλ‹€
key = [row for row in zip(*reversed(key))]
Copy link
Collaborator

Choose a reason for hiding this comment

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

μžλ‘λ‚˜λ¬΄ 문제보고 zip이 list끼리 indexλ₯Ό λ¬Άμ–΄μ„œ tuple둜 λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜κ΅¬λ‚˜ ν–ˆλŠ”λ° *reversed(key)둜 각 index μ›μ†Œμ— μ ‘κ·Όν•΄μ„œ tuple둜 λ¬Άμ–΄μ£Όλ©΄ νšŒμ „μ΄ λ˜λŠ”κ΅°μš” .... μ‘μš©μ΄ μ΄λ ‡κ²Œ λ˜λ„€μš”

μ½”λ“œκ°€ μ§„μ§œ μ†Œλ¦„μ΄ λ‹λ„€μš” πŸ«’πŸ‘

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.

def make_new_lock(lock):
    new_lock = []

    for _ in range(len(lock)):
        new_lock.append([2 for _ in range(len(lock[0])*3)])

    for row_lock in lock:
        temp = []
        temp.extend([2 for _ in range(len(row_lock))])
        temp.extend(row_lock)
        temp.extend([2 for _ in range(len(row_lock))])
        new_lock.append(temp)

    for _ in range(len(lock)):
        new_lock.append([2 for _ in range(len(lock[0])*3)])

    return new_lock

def rotate_key(key):
    rotated_key = []

    for col in range(len(key[0])):
        
        temp = []
        for row in range(len(key)):
            temp.append(key[len(key)-row-1][col])

        rotated_key.append(temp)

    return rotated_key

def solution(key, lock):
    new_lock = make_new_lock(lock)

    hole_count = 0

    for row in range(len(new_lock)):
        for col in range(len(new_lock)):
            if new_lock[row][col] == 0:
                hole_count += 1

    for _ in range(4):
        
        for row in range(len(lock)-len(key), len(lock)*2+1):
            for col in range(len(lock)-len(key), len(lock)*2+1):
                temp = 0

                for inner_row in range(row, row+len(key)):
                    for inner_col in range(col, col+len(key)):
                        if key[inner_row-row][inner_col-col] == 1 and new_lock[inner_row][inner_col] == 0:
                            temp += 1
                            

                        elif key[inner_row-row][inner_col-col] == 1 and new_lock[inner_row][inner_col] == 1:
                            temp += int(1e9)

                if hole_count == temp: return True

        key = rotate_key(key)

    return False

μ˜€λ§ˆμ΄κ°€μ‰¬...

완탐이면 쑰금만 더 λ…Έλ ₯ν•˜λ©΄ ν’€ 수 μžˆμ—ˆλŠ”λ°...

완탐 문제의 문제점 : κ³Όμ—° 이 풀이가 λ§žλŠ” 걸까? 라고 μƒκ°ν•˜λ©° μ‹œλ„λ„ μ•ˆν•΄λ΄„

λ°˜μ„±ν•˜κ³  κ°‘λ‹ˆλ‹€. (__)

@MunbinLee MunbinLee merged commit 66ea60f into main May 3, 2024
1 check passed
@MunbinLee MunbinLee deleted the 38-pknujsp branch May 3, 2024 15:34
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.

4 participants