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

4-wjdheesp44 #15

Merged
merged 3 commits into from
May 23, 2024
Merged

4-wjdheesp44 #15

merged 3 commits into from
May 23, 2024

Conversation

wjdheesp44
Copy link
Collaborator

πŸ”— 문제 링크

2468번 μ•ˆμ „ μ˜μ—­

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

30λΆ„

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

μ–΄λ–€ μ§€μ—­μ˜ 물에 μž κΈ°μ§€ μ•Šμ€ μ•ˆμ „ν•œ μ˜μ—­μ˜ μ΅œλŒ“κ°’μ„ κ΅¬ν•˜λŠ” bfs λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€. μ•ˆμ „ν•œ 칸의 개수λ₯Ό κ΅¬ν•˜λŠ” 것이 μ•„λ‹ˆλΌ λ­‰ν……μ΄λ‘œ 물에 μž κΈ°μ§€ μ•ŠλŠ” 지점듀이 μœ„, μ•„λž˜, 였λ₯Έμͺ½, μ™Όμͺ½μœΌλ‘œ 인접해 μžˆλŠ” μ˜μ—­μ˜ 개수λ₯Ό κ΅¬ν•˜λŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€. λ°±μ€€μœΌλ‘œ 보면 μ„¬μ˜ 개수λ₯Ό κ΅¬ν•˜λŠ” λ¬Έμ œμ™€ ν‘μ‚¬ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€.

κ³ λ €ν•  점

  • 2차원 리슀트의 κ°’λ“€ 쀑 높이가 κ°€μž₯ 큰 κ°’κΉŒμ§€ 물에 μž κΈ΄λ‹€κ³  μƒκ°ν•˜κ³  ν’‰λ‹ˆλ‹€.
  • 아무 μ˜μ—­λ„ μž κΈ°μ§€ μ•Šμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄, λ‹€μŒκ³Ό 같이 높이가 4 μ΄ν•˜μΈ λͺ¨λ“  지점이 물에 μž κ²Όλ‹€λ©΄
image


μ΄λ ‡κ²Œ μ•ˆμ „ μ˜μ—­μ΄ 5κ°œκ°€ λœλ‹€.
image


μˆœμ„œ

1. 2차원 리슀트의 κ°’λ“€ 쀑 κ°€μž₯ 큰 κ°’ 만큼 λ°˜λ³΅λ¬Έμ„ λŒλ¦½λ‹ˆλ‹€.
2. 2차원 리슀트λ₯Ό 돌렀 λ°©λ¬Έν•˜μ§€ μ•Šμ€ μ˜μ—­μ΄λ©΄μ„œ μž κΈ°μ§€ μ•Šμ€ μ˜μ—­μ„ bfs ν•¨μˆ˜μ— λ„£μ–΄ μ•ˆμ „ μ˜μ—­μ„ 개수λ₯Ό κ΅¬ν•©λ‹ˆλ‹€.
3. μ•ˆμ „ μ˜μ—­ κ°œμˆ˜κ°€ κ°€μž₯ λ§Žμ€ 값을 좜λ ₯ν•©λ‹ˆλ‹€.

import sys
from collections import deque
input = sys.stdin.readline

sum = 0
cnt = 0
n = int(input())
max_height = 0
board = [list(map(int, input().split())) for _ in range(n)]

max_height = max(map(max, board))

def bfs(x, y, k):
    dx, dy = [-1, 0, 1, 0], [0, 1, 0, -1]
    q = deque()
    q.append((x, y))
    
    while q:
        x, y = q.popleft()
        for i in range(4):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] == 0 and board[nx][ny] > k:
                visited[nx][ny] = 1
                q.append((nx, ny))


for k in range(max_height):
    visited = [[0] * n for _ in range(n)]
    cnt = 0

    for i in range(n):
        for j in range(n):
            if visited[i][j] == 0 and board[i][j] > k:
                bfs(i, j, k)
                cnt += 1
    sum = max(sum, cnt)

print(sum)

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

νŒŒμ΄μ¬μ—μ„œ 2차원 λ¦¬μŠ€νŠΈμ—μ„œ κ°€μž₯ 큰 값을 ꡬ할 λ•ŒλŠ” max(map(max, board)) 이런 μ‹μœΌλ‘œ ν‘œν˜„ν•  수 μžˆμŒμ„ μ•Œκ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

bfs(i, j, k)
cnt += 1
sum = max(sum, cnt)

Copy link
Collaborator

Choose a reason for hiding this comment

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

μ‚¬μ†Œν•˜κΈ΄ ν•˜μ§€λ§Œ visited λ¦¬μŠ€νŠΈλŠ” boolean으둜 ν•˜λ©΄ 쒋을 것 κ°™λ‹€λŠ” 생각을 ν•΄λ΄€μŠ΅λ‹ˆλ‹€.

Copy link
Collaborator

@erase-jeong erase-jeong left a comment

Choose a reason for hiding this comment

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

μ²˜μŒμ— 높이 정보가 μ—†μ–΄μ„œ μ–΄λ–»κ²Œ 풀어야할지 κ³ λ―Όν–ˆμ—ˆλŠ”λ° λͺ¨λ“  경우λ₯Ό λ‹€ κ³ λ €ν•΄μ•Όν•˜κ΅°μš”. 문제 꼼꼼히 읽어야겠닀고 λ‹€μ‹œ λŠλΌλ„€μš”.

@wjdheesp44 wjdheesp44 merged commit 15c8858 into main May 23, 2024
1 check passed
@wjdheesp44 wjdheesp44 deleted the 4-wjdheesp44 branch May 23, 2024 14:53
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