ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

20058๋ฒˆ: ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ํŒŒ์ด์–ด์Šคํ†ฐ

๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๋Š” ํŒŒ์ด์–ด๋ณผ๊ณผ ํ† ๋„ค์ด๋„๋ฅผ ์กฐํ•ฉํ•ด ํŒŒ์ด์–ด์Šคํ†ฐ์„ ์‹œ์ „ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋Š˜์€ ํŒŒ์ด์–ด์Šคํ†ฐ์„ ํฌ๊ธฐ๊ฐ€ 2N × 2N์ธ ๊ฒฉ์ž๋กœ ๋‚˜๋ˆ„์–ด์ง„ ์–ผ์ŒํŒ์—์„œ ์—ฐ์Šตํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์œ„์น˜ (r, c)๋Š” ๊ฒฉ์ž์˜ rํ–‰ c

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ๋กœ, ๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ์— ์ œ์‹œ๋œ ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. `2 ^ L * 2 ^ L` ํฌ๊ธฐ๋งŒํผ์˜ ๊ฒฉ์ž๋ฅผ ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ 90๋„ ํšŒ์ „ํ•œ๋‹ค.
    • 90๋„๋กœ ํšŒ์ „ํ•˜๋Š” ๊ฒƒ์€ L์ด 1์ธ ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    • `[[1, 2], [3, 4]]`๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ `↑` ์œ— ๋ฐฉํ–ฅ์œผ๋กœ ์ฝ์œผ๋ฉด `[3, 1], [4, 2]`๊ฐ€ ๋œ๋‹ค.
    • ์ด๋ฅผ `→` ์šฐ์ธก์œผ๋กœ ์“ฐ๊ฒŒ ๋˜๋ฉด `[[3, 1], [4, 2]]`๋กœ 90๋„ ํšŒ์ „ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
  2. ๊ฐ ์–ผ์Œ์— ์ธ์ ‘ํ•œ ์–ผ์Œ(์ƒ, ํ•˜, ์ขŒ, ์šฐ)์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ ๋ฏธ๋งŒ์ด๋ผ๋ฉด ์–ผ์Œ์ด ๋…น์•„ 1 ๊ฐ์†Œํ•œ๋‹ค.
    • (0, 0)๊ณผ ๊ฐ™์ด ์ธ์ ‘ํ•œ ๊ณณ์ด 2์ธ ๊ณณ์ด๋‹ค.
    • ์ธ์ ‘ํ•œ ์–ผ์Œ์ด 0์ด๋ผ๋ฉด, ์–ผ์Œ์ด ์—†๋Š” ๊ฒƒ์ด๋‹ค.
  3. Q๋ฒˆ ์œ„์˜ 1, 2 ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
  4. ๋‚จ์•„ ์žˆ๋Š” ์–ผ์Œ์˜ ํ•ฉ๊ณผ ๋‚จ์•„ ์žˆ๋Š” ์–ผ์Œ ์ค‘ ๊ฐ€์žฅ ํฐ ๋ฉ์–ด๋ฆฌ๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    • ๊ฐ€์žฅ ํฐ ๋ฉ์–ด๋ฆฌ๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜๋Š” ์–ผ์Œ์ด ์—ฐ๊ฒฐ๋œ ํฌ๊ธฐ๋ฅผ ๋งํ•œ๋‹ค.
    • ์ด๋Š” ๋ฐฑ์ค€: 2667 ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ์˜ ์š”๊ตฌ ์‚ฌํ•ญ๊ณผ ๋™์ผํ•˜๋‹ค.

 ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์ฃผ์˜ํ•  ์‚ฌํ•ญ์€, ๋…น์—ฌ์•ผ ํ•˜๋Š” ์–ผ์Œ์„ ์ฆ‰์‹œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋˜๋ฉด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋‹ค๋ฅธ ์–ผ์Œ์˜ ํƒ์ƒ‰ ๊ณผ์ •์—์„œ ํ•ด๋‹น ์–ผ์Œ์ด 0์ด ๋˜๊ฒŒ ๋˜๋ฉด ์ง€๊ธˆ ๋…น์•„์•ผ ํ•  ์–ผ์Œ์ด ์•„๋‹Œ๋ฐ๋„ ๋…น์ด๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์ฃผ์˜ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

 

 ์‚ผ์„ฑ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ๋“ค์€, ํ–‰๋ ฌ์„ ์กฐ์ž‘ํ•˜๋Š” ๋ฌธ์ œ๋“ค์ด ๋งŽ์€ ํŽธ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋น„์Šทํ•œ ์œ ํ˜•์„ ์ ‘ํ•˜๊ณ  ํ–‰๋ ฌ์„ ์กฐ์ž‘ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์•”๊ธฐํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ผ ๊ฒƒ ๊ฐ™๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


def visitable(r, c):
    return 0 <= r < N and 0 <= c < N and board[r][c]


def rotate(loc, l):
    cur_r, cur_c = loc

    '''
    ์˜ˆ๋ฅผ ๋“ค์–ด ๊ฒฉ์ž์— ๊ฐ’์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด,
    1 2             3 1
    3 4             4 2
    ↑๋กœ ์ฝ๊ณ          →๋กœ ์“ฐ๋ฉด
    90๋„ ํšŒ์ „
    '''
    for r in range(l):
        for c in range(l):
            # read ↑
            temp[r][c] = board[cur_r + l - 1 - c][cur_c + r]

    for r in range(l):
        for c in range(l):
            # write →
            board[cur_r + r][cur_c + c] = temp[r][c]


def simulation(l):
    # 1. ๊ฒฉ์ž ํฌ๊ธฐ ๋งŒํผ 90๋„ ํšŒ์ „.
    for r in range(0, N, l):
        for c in range(0, N, l):
            rotate((r, c), l)

    check = [[0 for _ in range(N)] for _ in range(N)]

    # 2-1. ์ธ์ ‘ํ•œ ์–ผ์Œ์ด 3๊ฐœ ๋ฏธ๋งŒ์ธ ๊ฒฝ์šฐ ์ฐพ๊ธฐ.
    for r in range(N):
        for c in range(N):
            if not board[r][c]:
                continue

            adj_cnt = 0
            for dr, dc in dirs:
                next_r, next_c = r + dr, c + dc
                if visitable(next_r, next_c):
                    adj_cnt += 1

            # check ํ•˜์ง€ ์•Š๊ณ , ๋ฐ”๋กœ ๋…น์ด๊ฒŒ ๋˜๋ฉด ํƒ์ƒ‰์— ์˜ํ–ฅ์„ ๋ฐ›์Œ.
            check[r][c] = 1 if adj_cnt < 3 else 0

    # 2-2. ์ฐพ์€ ์–ผ์Œ ๋…น์ด๊ธฐ.
    for r in range(N):
        for c in range(N):
            if check[r][c]:
                board[r][c] -= 1


# ๋ฐฑ์ค€: 4963 ์„ฌ์˜ ๊ฐœ์ˆ˜์™€ ๊ฐ™์€ ๋กœ์ง
def bfs(start):
    q = deque([start])
    cnt = 1

    while q:
        cur_r, cur_c = q.popleft()
        for dr, dc in dirs:
            next_r, next_c = cur_r + dr, cur_c + dc

            if visitable(next_r, next_c) and not visited[next_r][next_c]:
                visited[next_r][next_c] = 1
                q.append((next_r, next_c))
                cnt += 1

    return cnt


if __name__ == '__main__':
    N, Q = map(int, stdin.readline().split())
    N = 2 ** N
    board = [list(map(int, stdin.readline().split())) for _ in range(N)]

    L = list(map(int, stdin.readline().split()))
    max_l = max(L)
    temp = [[0 for _ in range(2 ** max_l)] for _ in range(2 ** max_l)]
    visited = [[0 for _ in range(N)] for _ in range(N)]
    dirs = ((-1, 0), (1, 0), (0, -1), (0, 1))

    for cur_l in L:
        simulation(2 ** cur_l)

    # Answer 1: ๋‚จ์•„ ์žˆ๋Š” ์–ผ์Œ์˜ ์ดํ•ฉ.
    sum_ice = sum(sum(board, []))
    print(sum_ice)

    # Answer 2: ๋‚จ์•„ ์žˆ๋Š” ์–ผ์Œ ์ค‘ ๊ฐ€์žฅ ํฐ ๋ฉ์–ด๋ฆฌ๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜.
    max_ice = 0
    for i in range(N):
        for j in range(N):
            if board[i][j] and not visited[i][j]:
                visited[i][j] = 1
                max_ice = max(max_ice, bfs((i, j)))

    print(max_ice)

Python์œผ๋กœ ์ œ์ถœํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€