๐น ๊ฒ๋ฆฌ๋งจ๋๋ง2
1. ๋ฌธ์ ๋งํฌ
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์ ์ ๋ ฌํ ํ ์ธ๊ตฌ๊ฐ ๊ฐ์ฅ ๋ง์ ์ ๊ฑฐ๊ตฌ์ ๊ฐ์ฅ ์ ์ ์ ๊ฑฐ๊ตฌ์ ์ธ๊ตฌ ์ฐจ์ด ์ฆ, ๋ฆฌ์คํธ์ ๋ง์ง๋ง ๊ฐ๊ณผ ์ฒซ ๋ฒ์งธ ๊ฐ์ ์ฐจ๋ฅผ ๊ตฌํ ํ ๊ฒฐ๊ณผ๊ฐ๊ณผ ๋น๊ตํ์ฌ ์ต์๊ฐ์ ์ ์ฅํ๋ค.