18809.โ Gaaaaaaaaaarden
์ ๋ฐ์ดํธ ์๊ฐ : 2023-11-23 08:57:24 +0000[Gold I] Gaaaaaaaaaarden - 18809
์ฑ๋ฅ ์์ฝ
๋ฉ๋ชจ๋ฆฌ: 146996 KB, ์๊ฐ: 3292 ms
๋ถ๋ฅ
๋๋น ์ฐ์ ํ์, ๋ธ๋ฃจํธํฌ์ค ์๊ณ ๋ฆฌ์ฆ, ๊ทธ๋ํ ์ด๋ก , ๊ทธ๋ํ ํ์, ๊ตฌํ, ์๋ฎฌ๋ ์ด์
์ ์ถ ์ผ์
2023๋ 11์ 23์ผ 17:53:29
๋ฌธ์ ์ค๋ช
๊ธธ๊ณ ๊ธธ์๋ ๊ฒจ์ธ์ด ๋๋๊ณ BOJ ๋ง์์๋ ๋ด์ด ์ฐพ์์๋ค. BOJ ๋ง์์์๋ ๊ฝ์ ๋ง์ ์์ ์ ์ ์์ ํผ์ฐ๋ ค๊ณ ํ๋ค. ์ ์์ ๋ ๊ณผ ํธ์๋ก ์ด๋ฃจ์ด์ ธ ์๊ณ 2์ฐจ์ ๊ฒฉ์ํ ๋ชจ์์ด๋ค.
์ธ๊ฑด๋น ์ ๊ฐ์ ์ํด BOJ ๋ง์์์๋ ์ง์ ์ฌ๋์ด ์จ์์ ์ฌ๋ ๋์ ์ด๋ก์ ๋ฐฐ์์ก๊ณผ ๋นจ๊ฐ์ ๋ฐฐ์์ก์ ๋ ์ ์ ์ ํ๊ฒ ๋ฟ๋ ค์ ๊ฝ์ ํผ์ธ ๊ฒ์ด๋ค. ์ด ๋ ๋ฐฐ์์ก์ ๋ฟ๋ฆด ์ ์๋ ๋ ์ ๋ฏธ๋ฆฌ ์ ํด์ ธ์๋ค.
๋ฐฐ์์ก์ ๋งค ์ด๋ง๋ค ์ด์ ์ ๋ฐฐ์์ก์ด ๋๋ฌํ ์ ์ด ์๋ ์ธ์ ํ ๋ ์ผ๋ก ํผ์ ธ๊ฐ๋ค.
์๋๋ ์ด๋ก์ ๋ฐฐ์์ก 2๊ฐ๋ฅผ ๋ฟ๋ ธ์ ๋์ ์์์ด๋ค. ํ์์ ์นธ์ ๋ฐฐ์์ก์ ๋ฟ๋ฆด ์ ์๋ ๋ ์, ํฉํ ์ ์นธ์ ๋ฐฐ์์ก์ ๋ฟ๋ฆด ์ ์๋ ๋ ์, ํ๋์ ์นธ์ ํธ์๋ฅผ ์๋ฏธํ๋ค.
์ด๋ก์ ๋ฐฐ์์ก๊ณผ ๋นจ๊ฐ์ ๋ฐฐ์์ก์ด ๋์ผํ ์๊ฐ์ ๋๋ฌํ ๋ ์์๋ ๋ ๋ฐฐ์์ก์ด ํฉ์ณ์ ธ์ ๊ฝ์ด ํผ์ด๋๋ค. ๊ฝ์ด ํผ์ด๋ ๋ ์์๋ ๋ฐฐ์์ก์ด ์ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์ ๋ ์ด์ ์ธ์ ํ ๋ ์ผ๋ก ๋ฐฐ์์ก์ ํผํธ๋ฆฌ์ง ์๋๋ค.
์๋๋ ์ด๋ก์ ๋ฐฐ์์ก 2๊ฐ์ ๋นจ๊ฐ์ ๋ฐฐ์์ก 2๊ฐ๋ฅผ ๋ฟ๋ ธ์ ๋์ ์์์ด๋ค.
๋ฐฐ์์ก์ ๋ด์ด ์ง๋๋ฉด ์ฌ์ฉํ ์ ์๊ฒ ๋๋ฏ๋ก ์ฃผ์ด์ง ๋ชจ๋ ๋ฐฐ์์ก์ ๋จ๊น์์ด ์ฌ์ฉํด์ผ ํ๋ค. ์๋ฅผ ๋ค์ด ์ด๋ก์ ๋ฐฐ์์ก 2๊ฐ์ ๋นจ๊ฐ์ ๋ฐฐ์์ก 2๊ฐ๊ฐ ์ฃผ์ด์ก๋๋ฐ ์ด๋ก์ ๋ฐฐ์์ก 1๊ฐ๋ฅผ ๋ ์ ๋ฟ๋ฆฌ์ง ์๊ณ , ์ด๋ก์ ๋ฐฐ์์ก 1๊ฐ์ ๋นจ๊ฐ์ ๋ฐฐ์์ก 2๊ฐ๋ง์ ์ฌ์ฉํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํ๋ค.
๋ํ ๋ชจ๋ ๋ฐฐ์์ก์ ์๋ก ๋ค๋ฅธ ๊ณณ์ ๋ฟ๋ ค์ ธ์ผ ํ๋ค.
์ ์๊ณผ ๋ ๋ฐฐ์์ก์ ๊ฐ์๊ฐ ์ฃผ์ด์ ธ์์ ๋ ํผ์ธ ์ ์๋ ๊ฝ์ ์ต๋ ๊ฐ์๋ฅผ ๊ตฌํด๋ณด์.
์ ๋ ฅ
์ฒซ์งธ ์ค์ ์ ์์ ํ์ ๊ฐ์์ ์ด์ ๊ฐ์๋ฅผ ๋ํ๋ด๋ N(2 โค N โค 50)๊ณผ M(2 โค M โค 50), ๊ทธ๋ฆฌ๊ณ ์ด๋ก์ ๋ฐฐ์์ก์ ๊ฐ์ G(1 โค G โค 5)์ ๋นจ๊ฐ์ ๋ฐฐ์์ก์ ๊ฐ์ R(1 โค R โค 5)์ด ํ ์นธ์ ๋น์นธ์ ์ฌ์ด์ ๋๊ณ ์ฃผ์ด์ง๋ค.
๊ทธ ๋ค์ N๊ฐ์ ์ค์๋ ๊ฐ ์ค๋ง๋ค ์ ์์ ๊ฐ ํ์ ๋ํ๋ด๋ M๊ฐ์ ์ ์๊ฐ ํ ๊ฐ์ ๋น ์นธ์ ์ฌ์ด์ ๋๊ณ ์ฃผ์ด์ง๋ค. ๊ฐ ์นธ์ ๋ค์ด๊ฐ๋ ๊ฐ์ 0, 1, 2์ด๋ค. 0์ ํธ์, 1์ ๋ฐฐ์์ก์ ๋ฟ๋ฆด ์ ์๋ ๋ , 2๋ ๋ฐฐ์์ก์ ๋ฟ๋ฆด ์ ์๋ ๋ ์ ์๋ฏธํ๋ค.
๋ฐฐ์์ก์ ๋ฟ๋ฆด ์ ์๋ ๋ ์ ์๋ R+G๊ฐ ์ด์์ด๊ณ 10๊ฐ ์ดํ์ด๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ํผ์ธ ์ ์๋ ๊ฝ์ ์ต๋ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ค.
๐ก Solutions
๐ Gaaaaaaaaaarden.py
import sys
from collections import deque
from itertools import combinations
# N = int(sys.stdin.readline())
def selectLand(position, Green_Red, now, sp):
global answer
if now == G + R:
answer = max(answer, simulation(position, sp))
return
if Green_Red[0] :
Green_Red[0] -= 1
seletedColor[now] = True
selectLand(position, Green_Red, now + 1, sp)
Green_Red[0] += 1
if Green_Red[1] :
Green_Red[1] -= 1
seletedColor[now] = False
selectLand(position, Green_Red, now + 1, sp)
Green_Red[1] += 1
def simulation(position, start_point):
for i in range(R+G):
# G๋ 3๋ฒ R์ 4๋ฒ์ผ๋ก ๋ฐฐ์์ก ๋ฟ๋ฆฌ๊ธฐ
matrix[pos[i][0]][pos[i][1]] = 3 if position[i] else 4
result = bfs(matrix, start_point)
for i in range(R+G):
# G๋ 3๋ฒ R์ 4๋ฒ์ผ๋ก ๋ฐฐ์์ก ๋ฟ๋ฆฌ๊ธฐ
matrix[pos[i][0]][pos[i][1]] = 2
return result
def bfs(ground, start_point):
result = 0
is_visited = [[0 for _ in range(M)] for _ in range(N)]
que = deque(start_point)
for x, y in start_point:
is_visited[x][y] = matrix[x][y]
next_current = []
while que or next_current:
# ๋ ์ด ์ง๋ ๊ฒฝ์ฐ ๊ฝ์ด ํ์ง ์ํ์ง ํ์ธ
if not que:
finishGround = set()
nxt_curt = set()
for gx, gy, color in next_current:
if is_visited[gx][gy] == 0 and (ground[gx][gy] == 1 or ground[gx][gy] == 2):
is_visited[gx][gy] = color
nxt_curt.add((gx, gy))
elif color != is_visited[gx][gy] :
is_visited[gx][gy] = 7
finishGround.add((gx, gy))
que = deque(list(nxt_curt - finishGround))
result += len(finishGround)
next_current = []
continue
x, y = que.pop()
for dx, dy in ((0,1), (0,-1), (1,0), (-1,0)):
nx = x + dx
ny = y + dy
if nx < 0 or ny < 0 or nx >= N or ny >= M : continue
if is_visited[nx][ny] or ground[nx][ny] == 0: continue
if ground[nx][ny] != 1 and ground[nx][ny] != 2 : continue
next_current.append([nx, ny, is_visited[x][y]])
return result
# seg_tree = [0]*(4*N)
N, M, G, R = list(map(int,sys.stdin.readline().split()))
matrix = [list(map(int,sys.stdin.readline().split())) for _ in range(N)]
answer = 0
ok_land = []
for n in range(N):
for m in range(M):
if matrix[n][m] == 2: ok_land.append((n,m))
# ๋ฐฐ์์ก G ๋ฐฐ์ R ๋ฐฐ์์ก ๋๋ ์์น ๊ฒฐ์ ํด์ผํจ
# G๋ True R๋ False ๋ก ์์
ํด์ ์ ๊ตฌ๋ถ
seletedColor = [False for _ in range(G+R)]
for pos in combinations(ok_land, G+R):
selectLand(seletedColor, [G, R], 0, pos)
print(answer)