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

18-H0ngJu #199

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions H0ngJu/DFS/κ³„λž€μœΌλ‘œ κ³„λž€μΉ˜κΈ°.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
sys.setrecursionlimit(10**6)

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

N = int(input())
eggs = [list(map(int, input().split())) for _ in range(N)]

def break_egg(idx):

Choose a reason for hiding this comment

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

λ°±νŠΈλž˜ν‚Ή ν•¨μˆ˜μ˜ 인자둜 "κ³„λž€μ„ λͺ‡ 개 κΉΌλŠ”κ°€" 정보λ₯Ό 같이 λ„˜κ²¨μ£Όκ²Œ 되면 쑰금 더 효율적으둜 λ™μž‘ν•˜κ²Œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€

def backtracking(current: int = 0, broken: int = 0) -> int:
    if broken == N - 1 or current == N:  # λͺ¨λ“  κ³„λž€μ΄ κΉ¨μ‘Œκ±°λ‚˜ λκΉŒμ§€ μ™”λ‹€λ©΄
        return broken
        
    if S[current] <= 0:  # 이미 깨짐
        return backtracking(current + 1, broken)

    count: int = 0
    for nxt in range(N):
        if current == nxt or S[nxt] <= 0:  # 같은 κ³„λž€μ΄κ±°λ‚˜ 이미 κΉ¨μ‘Œλ‹€λ©΄ skip
            continue
        
        S[current] -= W[nxt]
        S[nxt] -= W[current]
        
        count = max(count, backtracking(current + 1, broken + (S[current] <= 0) + (S[nxt] <= 0)))
        
        S[current] += W[nxt]
        S[nxt] += W[current]
    
    return count

broken 인자λ₯Ό λ“€κ³  있기 λ•Œλ¬Έμ— 리슀트λ₯Ό μˆœνšŒν•΄μ„œ 깨진 κ³„λž€ 개수λ₯Ό μΉ΄μš΄νŒ…ν•˜λŠ” κ³Όμ • μžμ²΄κ°€ μ‚¬λΌμ§‘λ‹ˆλ‹€.

Choose a reason for hiding this comment

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

제 μ½”λ“œμ˜ κ²½μš°μ—” μž¬κ·€ μ œν•œμ„ μ•ˆν’€μ–΄μ€˜λ„ λ˜λ”λΌκ΅¬μš” :)

cnt = 0

# idxκ°€ λ„˜μ–΄κ°„ 경우
if idx == N:
for i in range(N):
if eggs[i][0] <= 0:
cnt += 1
return cnt

if eggs[idx][0] <= 0: # λ“€κ³  μžˆλŠ” κ³„λž€μ΄ 깨진 경우
return break_egg(idx+1)

broken = False
for j in range(N): # κ³„λž€ κΉ¨κΈ°
if j != idx and eggs[j][0] > 0:
Comment on lines +23 to +24
Copy link
Member

Choose a reason for hiding this comment

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

λ”°λ‘œ μ‚¬μ†Œν•œ ꢁ금점인데...
ν˜Ήμ‹œ j둜 λ°˜λ³΅λ¬Έμ„ λŒλ¦¬μ‹  μ΄μœ κ°€ μžˆμœΌμ‹€κΉŒμš”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

음 μ™œ j냐고 λ¬ΌμœΌμ‹ λ‹€λ©΄ .... i λ‹€μŒμ— j μ“°λŠ”κ²Œ μŠ΅κ΄€μ΄μ–΄μš”
큰 μ˜λ―ΈλŠ” μ—†μŠ΄λ‹€!

broken = True
eggs[idx][0] -= eggs[j][1]
eggs[j][0] -= eggs[idx][1]

cnt = max(cnt, break_egg(idx+1))

eggs[idx][0] += eggs[j][1]
eggs[j][0] += eggs[idx][1]
Comment on lines +31 to +32
Copy link
Collaborator

Choose a reason for hiding this comment

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

μ €λŠ” μ—¬κΈ°μ„œ λŒλ €μ£Όμ§€μ•Šκ³  κ·Έλƒ₯ 맀번 μƒˆλ‘œμš΄ 리슀트λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€!

Copy link
Member

Choose a reason for hiding this comment

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

@SeongHoonC

맀번 μƒˆλ‘œμš΄ 리슀트λ₯Ό μƒμ„±ν•˜λ©΄ $O(N)$의 μ‹œκ°„λ³΅μž‘λ„λ‘œ μƒμ„±λ˜λŠ”λ°,

μœ„μ™€ 같이 λ°±νŠΈλž˜ν‚Ή λ°©μ‹μœΌλ‘œ κ°”λ‹€κ°€ λ‹€μ‹œ λ˜λŒλ €λ†“μœΌλ©΄ $O(1)$둜 ν’€ 수 μžˆμ–΄μ„œ 훨씬 λΉ¨λΌμš”!


if not broken:
return break_egg(idx+1)

return cnt

print(break_egg(0))
1 change: 1 addition & 0 deletions H0ngJu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
| 15μ°¨μ‹œ | 2024.05.14 | 그리디 | [A와 B](https://www.acmicpc.net/problem/12904) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/188 |
| 16μ°¨μ‹œ | 2024.05.14 | BFS | [λ±€κ³Ό 사닀리 κ²Œμž„](https://www.acmicpc.net/problem/16928) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/191 |
| 17μ°¨μ‹œ | 2024.05.22 | BFS | [적둝색약](https://www.acmicpc.net/problem/10026) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/195 |
| 17μ°¨μ‹œ | 2024.05.26 | DFS | [κ³„λž€μœΌλ‘œ κ³„λž€μΉ˜κΈ°](https://www.acmicpc.net/problem/16987) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/199 |

---
Loading