DevYoon

[SWEA] 2117. ํ™ˆ ๋ฐฉ๋ฒ” ์„œ๋น„์Šค (Python) ๋ณธ๋ฌธ

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์„ ๋”ํ•ด์ค˜์•ผ ํ•œ๋‹ค๋“ ์ง€)์—์„œ ์‹ค์ˆ˜๊ฐ€ ๋งŽ์•„์„œ ํ•œ ๋ฒˆ์— ๋‹ต์„ ๋‚ด์ง„ ๋ชปํ–ˆ๋‹ค
  • ๊ทธ๋ž˜๋„ ์–ธ์  ๊ฐ„,,, ์„ค๊ณ„ ํ•œ ๋ฒˆ์— ๋”ฑ ํ•˜๊ณ  ๋‹ต๋„ ํ•œ ๋ฒˆ์— ๋”ฑ ๋‚˜์˜ค๋Š” ๋‚ ์ด ์žˆ์ง€ ์•Š์„๊นŒ?!ใ…‹ใ…‹ใ…‹
  • ์‰ฝ๊ฒŒ๋งŒ ์‚ด์•„๊ฐ€๋ฉด ์žฌ๋ฏธ์—†์–ด ๋น™๊ณ ,,,๐Ÿฅฒ๐ŸŽต