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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

17135๋ฒˆ: ์บ์Šฌ ๋””ํŽœ์Šค

์ฒซ์งธ ์ค„์— ๊ฒฉ์žํŒ ํ–‰์˜ ์ˆ˜ N, ์—ด์˜ ์ˆ˜ M, ๊ถ์ˆ˜์˜ ๊ณต๊ฒฉ ๊ฑฐ๋ฆฌ ์ œํ•œ D๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฒฉ์žํŒ์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ์นธ, 1์€ ์ ์ด ์žˆ๋Š” ์นธ์ด๋‹ค.

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ฌธ์ œ์—์„œ ๊ฒฉ์žํŒ๊ณผ ๊ถ์ˆ˜์˜ ๊ณต๊ฒฉ ๊ฑฐ๋ฆฌ ์ œํ•œ D๊ฐ€ ์ฃผ์–ด ์งˆ ๋•Œ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ์ ์˜ ์ตœ๋Œ€ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๊ถ์ˆ˜๋Š” 3๋ช…์„ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐฐ์น˜ ๊ฐ€๋Šฅํ•œ ๋ฒ”์œ„๋Š” ์—ด์˜ ๋ฒ”์œ„์ธ M๊นŒ์ง€ ์ด๋‹ค.

 

 ๊ถ์ˆ˜์˜ ๊ฒฝ์šฐ 3๋ช…๋งŒ ๋ฐฐ์น˜ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๊ฐ ์ขŒํ‘œ์— ๋Œ€ํ•ด ์กฐํ•ฉ(Combination)์œผ๋กœ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด ํ›„๋ณด๊ตฐ์„ ๊ตฌํ•œ๋‹ค. ํ›„๋ณด๊ตฐ์„ ๊ตฌํ•˜์˜€๋‹ค๋ฉด ํ›„๋ณด๊ตฐ์„ ๋ชจ๋‘ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๊ฑฐ๋ฆฌ์— ๋”ฐ๋ผ ์ ์„ ์ œ๊ฑฐํ•˜๊ณ  ์ ์ด ์•ž์œผ๋กœ ์›€์ง์ด๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ œ๊ฑฐํ•œ ์ ์˜ ์ˆ˜๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ตœ๋Œ€ ๊ฐ’์„ ์ฐพ์œผ๋ฉด ๋œ๋‹ค. ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ , ์ƒ๊ฐํ•ด๋Š”๋ฐ ๊นŒ์ง€ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ์œผ๋ฉฐ ์ ์„ ์ด๋™์‹œํ‚ค๊ฑฐ๋‚˜ ํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ํ’€์—ˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from copy import deepcopy


# combination
def dfs(cur_idx, depth):
    global candidates

    if cur_idx > M:
        return

    if len(check) == depth:
        candidates.append([archer_position[idx] for idx in check])
        return

    for idx in range(cur_idx, M):
        if idx in check:
            continue
        check.append(idx)
        dfs(cur_idx + 1, depth)
        check.pop()


def get_distance(cur_candidate):
    ret = []

    for position in cur_candidate:
        temp = []
        for y in range(N):
            for x in range(M):
                cur_distance = abs(position[0] - y) + abs(position[1] - x)
                if cur_distance <= D:
                    temp.append((cur_distance, y, x))
        ret.append(temp)

    return ret


def forward_enemy():
    return set((y + 1, x) for y, x in enemy if y + 1 < N)


def find_enemy(archer, cur_enemy):
    # ๊ฑฐ๋ฆฌ์™€, ์ขŒ์ธก์— ์žˆ๋Š” ์ˆœ์œผ๋กœ ์ •๋ ฌ
    archer.sort(key=lambda x: (x[0], x[2]))

    for dist, y, x in archer:
        if (y, x) in cur_enemy:
            return (y, x)

    return None


if __name__ == '__main__':
    answer = -1
    N, M, D = map(int, stdin.readline().split())
    enemy = set()
    board = [[0 for _ in range(M)] for _ in range(N)]
    # ์ž…๋ ฅ ๊ฐ’ ์ค‘ 1์€ ์ ์ด ์žˆ๋Š” ์œ„์น˜
    for y in range(N):
        info = list(map(int, stdin.readline().split()))
        for x in range(M):
            if info[x]:
                enemy.add((y, x))

    check = []
    archer_position = [(N, i) for i in range(M)]
    candidates = []

    dfs(0, 3)

    for archers in candidates:
        sum_kill = 0
        cur_archer_distance = get_distance(archers)
        origin_enemy = deepcopy(enemy)

        while enemy:
            cur_kill = set()

            for cur_archer in cur_archer_distance:
                kill = find_enemy(cur_archer, enemy)

                if kill is not None:
                    cur_kill.add(kill)

            sum_kill += len(cur_kill)
            # ์ฃฝ์€ ์ ๋“ค์„ ์ œ์™ธ
            enemy -= cur_kill
            enemy = forward_enemy()

        # ๋‹ค์Œ ์กฐํ•ฉ์„ ์œ„ํ•ด ์›๋ž˜๋Œ€๋กœ ์ดˆ๊ธฐํ™”
        enemy = origin_enemy
        answer = max(answer, sum_kill)

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