PS/SWEA

[SWEA] 2117. ν™ˆ λ°©λ²” μ„œλΉ„μŠ€ (Python)

gimewn 2022. 4. 10. 21:34

link πŸ”— https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5V61LqAf8DFAWu 

 

SW Expert Academy

SW ν”„λ‘œκ·Έλž˜λ° μ—­λŸ‰ 강화에 도움이 λ˜λŠ” λ‹€μ–‘ν•œ ν•™μŠ΅ 컨텐츠λ₯Ό ν™•μΈν•˜μ„Έμš”!

swexpertacademy.com

 

 

from collections import deque

def bfs(y, x):
    global Max, M
    check = [[0]*N for _ in range(N)]
    q = deque()
    q.append((y, x)) # y, x
    check[y][x] = 1
    K = 0 # 제곡 λ²”μœ„
    home = 0 # λ°©λ²” μ„œλΉ„μŠ€ λ°›λŠ” μ§‘μ˜ 개수
    if arr[y][x] == 1: # μ‹œμž‘ 지점에 집이 있으면 더해주기
        home += 1
    while q:
        ny, nx = q.popleft()
        if check[ny][nx] != K:
            K = check[ny][nx]
            cost = K * K + (K - 1) * (K - 1)
            if M * home >= cost:
                if Max < home:
                    Max = home

        direct = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 상 ν•˜ 쒌 우
        for idx in range(4):
            dy = direct[idx][0]+ny
            dx = direct[idx][1]+nx
            if 0 <= dy < N and 0 <= dx < N:
                if check[dy][dx] > 0: continue # 이미 μ„œλΉ„μŠ€ μ‚¬μš© 쀑이면 X
                check[dy][dx] = K+1
                if arr[dy][dx] == 1:
                    home += 1
                q.append((dy, dx))
    return Max

t = int(input())

for tc in range(1, t+1):
    Max = -1e9
    N, M = map(int, input().split()) # λ„μ‹œ 크기 N, μ§‘λ§ˆλ‹€ μ§€λΆˆ λΉ„μš© M
    arr = [list(map(int, input().split())) for _ in range(N)]
    for y in range(N):
        for x in range(N):
                bfs(y, x)

    print(f'#{tc} {Max}')

 

✏️ λ©”λͺ¨

  • 보자마자 floodfill둜 ν’€λ©΄ λ˜κ² λ‹€κ³  감이 μ™€μ„œ 신났닀!
  • ν•˜μ§€λ§Œ... μ—¬μ „νžˆ μ‹€μˆ˜νˆ¬μ„±μ΄...πŸ₯²
  • μžμž˜ν•œ λΆ€λΆ„ (μ‹œμž‘μ μ— 집이 μžˆμ„ λ•Œλ§Œ home에 1을 λ”ν•΄μ€˜μ•Ό ν•œλ‹€λ“ μ§€)μ—μ„œ μ‹€μˆ˜κ°€ λ§Žμ•„μ„œ ν•œ λ²ˆμ— 닡을 λ‚΄μ§„ λͺ»ν–ˆλ‹€
  • κ·Έλž˜λ„ μ–Έμ  κ°„,,, 섀계 ν•œ λ²ˆμ— λ”± ν•˜κ³  닡도 ν•œ λ²ˆμ— λ”± λ‚˜μ˜€λŠ” 날이 μžˆμ§€ μ•Šμ„κΉŒ?!γ…‹γ…‹γ…‹
  • μ‰½κ²Œλ§Œ μ‚΄μ•„κ°€λ©΄ μž¬λ―Έμ—†μ–΄ λΉ™κ³ ,,,πŸ₯²πŸŽ΅