Post

๐Ÿน ๊ฒŒ๋ฆฌ๋งจ๋”๋ง2

1. ๋ฌธ์ œ ๋งํฌ

17779๋ฒˆ: ์‚ฌ๋‹ค๋ฆฌ ํƒ€๊ธฐ


2. ์ฝ”๋“œ

Python3 34260KB 3588ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
N = int(input())
city = []
for _ in range(N):
    city.append(list(map(int, input().split())))
INF = int(1e9)

from itertools import product
from collections import deque

numbers = [i for i in range(N+1)]
result = INF
dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

def check_boundary(x, y, d1, d2):
    boundary1 = [(x + i, y - i) for i in range(d1+1)]
    boundary2 = [(x + i, y + i) for i in range(d2+1)]
    boundary3 = [(x + d1 + i, y - d1 + i) for i in range(d2+1)]
    boundary4 = [(x + d2 + i, y + d2 - i) for i in range(d1+1)]
    all_boundary = list(set(boundary1 + boundary2 + boundary3 + boundary4))
    for boundary in all_boundary:
        r, c = boundary
        area_info[r][c] = 5
    
    return all_boundary

def check_area5(boundary):
    boundary_dict = {}
    for b in boundary:
        x, y = b
        if x in boundary_dict:
            boundary_dict[x].append(y)
        else:
            boundary_dict[x] = [y]
    
    # ๊ฒฝ๊ณ„์„  ์•ˆ์„ 5๋กœ ์ฑ„์šฐ๊ธฐ
    for k in boundary_dict.keys():
        if len(boundary_dict[k]) == 1:
            continue
        start, end = min(boundary_dict[k]), max(boundary_dict[k])
        for i in range(start + 1, end):
            area_info[k][i] = 5

def check_other_area(sx, sy, ex, ey, area_num):
    q = deque([])
    # 4์ผ๋•Œ๋งŒ ๋์ง€์ ์—์„œ ํƒ์ƒ‰
    if area_num == 4:
        q.append((ex, ey))
        area_info[ex][ey] = area_num
    else:
        q.append((sx, sy))
        area_info[sx][sy] = area_num
    while q:
        x, y = q.popleft()
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if sx <= nx <= ex and sy <= ny <= ey and area_info[nx][ny] == 0:
                area_info[nx][ny] = area_num
                q.append((nx, ny))

def check_area_people():
    for i in range(N):
        for j in range(N):
            area_people[area_info[i][j]-1] += city[i][j]

for p in product(numbers, repeat=4):
    x, y, d1, d2 = p
    # ๊ธฐ์ค€์ ์ด ์žฌํ˜„์‹œ ๋ฐ–์ด๊ฑฐ๋‚˜ ์ด๋™ ๊ฑฐ๋ฆฌ๊ฐ€ 0์ผ ๋•Œ 
    if x == N or y == N or d1 == 0 or d2 == 0:
        continue
    # ๊ฒฝ๊ณ„ ์ค‘ ํ•œ ๊ฐ’์ด๋ผ๋„ ์žฌํ˜„์‹œ ๋ฐ–์ผ ๋•Œ
    if x + d1 >= N or y - d1 < 0 or x + d2 >= N or y + d2 >= N or x + d2 + d1 >= N or y + d2 - d1 < 0 or y + d2 - d1 >= N:
        continue
    area_info = [[0] * N for _ in range(N)]
    area_people = [0] * 5
    boundary = check_boundary(x, y, d1, d2)
    check_area5(boundary)   # 5๊ตฌ์—ญ ์ฒดํฌ
    check_other_area(0, 0, x + d1 - 1, y, 1)       # 1๊ตฌ์—ญ ์ฒดํฌ
    check_other_area(0, y + 1, x + d2, N-1, 2)      # 2๊ตฌ์—ญ ์ฒดํฌ
    check_other_area(x + d1, 0, N-1, y - d1 + d2 - 1, 3)   # 3๊ตฌ์—ญ ์ฒดํฌ
    check_other_area(x + d2 + 1, y - d1 + d2, N-1, N-1, 4) # 4๊ตฌ์—ญ ์ฒดํฌ
    check_area_people() # ๊ตฌ์—ญ๋ณ„ ์‚ฌ๋žŒ์ˆ˜ ํ™•์ธ
    area_people.sort()
    diff = area_people[4] - area_people[0]  # ์ธ๊ตฌ๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์„ ๊ฑฐ๊ตฌ์™€ ๊ฐ€์žฅ ์ ์€ ์„ ๊ฑฐ๊ตฌ์˜ ์ธ๊ตฌ ์ฐจ์ด
    result = min(result, diff)

print(result)


3. ํ•ด์„ค

area_info๋Š” ๊ฐ ๊ตฌ์—ญ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•œ๋‹ค.

์ฃผ์–ด์ง„ ๊ฒฝ๊ณ„์„  ์œ„์น˜์˜ ๊ฐ’์„ 5๋กœ ์ €์žฅํ•˜๊ณ , ๊ฒฝ๊ณ„์„  ์•ˆ์˜ ๊ฐ’๋„ 5๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์ด๋•Œ ๋‹ค๋ฅธ ๊ตฌ์—ญ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ bfs๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ์ฑ„์šฐ๋ฉด 5๋กœ ๋‘˜๋Ÿฌ์‹ธ์ธ ๊ณต๊ฐ„์—๋Š” ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๊ฒฝ๊ณ„์„ ๋งˆ๋‹ค x๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” y๊ตฌ์—ญ ๋‚ด์˜ ์œ„์น˜๋ฅผ ๋ชจ๋‘ 5๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

๋‚˜๋จธ์ง€ 1,2,3,4 ๊ตฌ์—ญ์€ bfs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ์—ญ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•œ๋‹ค. ์ด๋•Œ 1,2,3 ๊ตฌ์—ญ์€ ๊ฐ ์‹œ์ž‘ ๋ถ€๋ถ„๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜์ง€๋งŒ 4๊ตฌ์—ญ์˜ ์‹œ์ž‘๋ถ€๋ถ„์€ 5๊ตฌ์—ญ๊ณผ ๊ฒน์น˜๊ฒŒ ๋˜๋ฏ€๋กœ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„(N-1, N-1)๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•œ๋‹ค.

๊ฐ ๊ตฌ์—ญ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ–ˆ์œผ๋ฉด ๊ตฌ์—ญ๋ฒˆํ˜ธ๋ณ„๋กœ ์ธ์›์ˆ˜๋ฅผ area_people์— ์ €์žฅํ•œ๋‹ค.

area_people์„ ์ •๋ ฌํ•œ ํ›„ ์ธ๊ตฌ๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์„ ๊ฑฐ๊ตฌ์™€ ๊ฐ€์žฅ ์ ์€ ์„ ๊ฑฐ๊ตฌ์˜ ์ธ๊ตฌ ์ฐจ์ด ์ฆ‰, ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’๊ณผ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์˜ ์ฐจ๋ฅผ ๊ตฌํ•œ ํ›„ ๊ฒฐ๊ณผ๊ฐ’๊ณผ ๋น„๊ตํ•˜์—ฌ ์ตœ์†Ÿ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.

This post is licensed under CC BY 4.0 by the author.