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μ λν΄μ€μΌ νλ€λ μ§)μμ μ€μκ° λ§μμ ν λ²μ λ΅μ λ΄μ§ λͺ»νλ€
- κ·Έλλ μΈμ κ°,,, μ€κ³ ν λ²μ λ± νκ³ λ΅λ ν λ²μ λ± λμ€λ λ μ΄ μμ§ μμκΉ?!γ γ γ
- μ½κ²λ§ μ΄μκ°λ©΄ μ¬λ―Έμμ΄ λΉκ³ ,,,π₯²π΅