QA & Engineering Blog

A Blog about Quality ยท Automation ยท Engineering

๐Ÿ  ํ™ˆ์œผ๋กœ

[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)