[BOJ][๐ŸŸก4][๋ฐฑ์ค€#17135] ์บ์Šฌ ๋””ํŽœ์Šค

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #17135


๋ฌธ์ œ

์บ์Šฌ ๋””ํŽœ์Šค๋Š” ์„ฑ์„ ํ–ฅํ•ด ๋ชฐ๋ ค์˜ค๋Š” ์ ์„ ์žก๋Š” ํ„ด ๋ฐฉ์‹์˜ ๊ฒŒ์ž„์ด๋‹ค. ๊ฒŒ์ž„์ด ์ง„ํ–‰๋˜๋Š” ๊ณณ์€ ํฌ๊ธฐ๊ฐ€ Nร—M์ธ ๊ฒฉ์žํŒ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๊ฒฉ์žํŒ์€ 1ร—1 ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๊ณ , ๊ฐ ์นธ์— ํฌํ•จ๋œ ์ ์˜ ์ˆ˜๋Š” ์ตœ๋Œ€ ํ•˜๋‚˜์ด๋‹ค. ๊ฒฉ์žํŒ์˜ N๋ฒˆํ–‰์˜ ๋ฐ”๋กœ ์•„๋ž˜(N+1๋ฒˆ ํ–‰)์˜ ๋ชจ๋“  ์นธ์—๋Š” ์„ฑ์ด ์žˆ๋‹ค. ์„ฑ์„ ์ ์—๊ฒŒ์„œ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด ๊ถ์ˆ˜ 3๋ช…์„ ๋ฐฐ์น˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๊ถ์ˆ˜๋Š” ์„ฑ์ด ์žˆ๋Š”ย ์นธ์— ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๊ณ , ํ•˜๋‚˜์˜ ์นธ์—๋Š” ์ตœ๋Œ€ 1๋ช…์˜ ๊ถ์ˆ˜๋งŒ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.ย ๊ฐ๊ฐ์˜ ํ„ด๋งˆ๋‹ค ๊ถ์ˆ˜๋Š” ์  ํ•˜๋‚˜๋ฅผ ๊ณต๊ฒฉํ•  ์ˆ˜ ์žˆ๊ณ , ๋ชจ๋“  ๊ถ์ˆ˜๋Š” ๋™์‹œ์— ๊ณต๊ฒฉํ•œ๋‹ค. ๊ถ์ˆ˜๊ฐ€ ๊ณต๊ฒฉํ•˜๋Š” ์ ์€ ๊ฑฐ๋ฆฌ๊ฐ€ D์ดํ•˜์ธ ์  ์ค‘์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ ์ด๊ณ , ๊ทธ๋Ÿฌํ•œ ์ ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ์ ์„ ๊ณต๊ฒฉํ•œ๋‹ค. ๊ฐ™์€ ์ ์ด ์—ฌ๋Ÿฌ ๊ถ์ˆ˜์—๊ฒŒ ๊ณต๊ฒฉ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ณต๊ฒฉ๋ฐ›์€ ์ ์€ ๊ฒŒ์ž„์—์„œ ์ œ์™ธ๋œ๋‹ค. ๊ถ์ˆ˜์˜ ๊ณต๊ฒฉ์ด ๋๋‚˜๋ฉด, ์ ์ด ์ด๋™ํ•œ๋‹ค. ์ ์€ ์•„๋ž˜๋กœ ํ•œ ์นธ ์ด๋™ํ•˜๋ฉฐ, ์„ฑ์ด ์žˆ๋Š” ์นธ์œผ๋กœ ์ด๋™ํ•œ ๊ฒฝ์šฐ์—๋Š” ๊ฒŒ์ž„์—์„œ ์ œ์™ธ๋œ๋‹ค. ๋ชจ๋“  ์ ์ด ๊ฒฉ์žํŒ์—์„œ ์ œ์™ธ๋˜๋ฉด ๊ฒŒ์ž„์ด ๋๋‚œ๋‹ค.ย  ๊ฒŒ์ž„ ์„ค๋ช…์—์„œ ๋ณด๋‹ค์‹œํ”ผ ๊ถ์ˆ˜๋ฅผ ๋ฐฐ์น˜ํ•œ ์ดํ›„์˜ ๊ฒŒ์ž„ ์ง„ํ–‰์€ ์ •ํ•ด์ ธ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ, ์ด ๊ฒŒ์ž„์€ ๊ถ์ˆ˜์˜ ์œ„์น˜๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. ๊ฒฉ์žํŒ์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ถ์ˆ˜์˜ ๊ณต๊ฒฉ์œผ๋กœ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ์ ์˜ ์ตœ๋Œ€ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด๋ณด์ž. ๊ฒฉ์žํŒ์˜ ๋‘ ์œ„์น˜ (r1, c1), (r2, c2)์˜ ๊ฑฐ๋ฆฌ๋Š” |r1-r2| + |c1-c2|์ด๋‹ค.


์ž…๋ ฅ

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


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๊ถ์ˆ˜์˜ ๊ณต๊ฒฉ์œผ๋กœ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋Š” ์ ์˜ ์ตœ๋Œ€ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์ œํ•œ

3 โ‰ค N, M โ‰ค 15 1 โ‰ค D โ‰ค 10


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

5 5 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1


์ถœ๋ ฅ

3


์˜ˆ์ œ 2

์ž…๋ ฅ

5 5 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0


์ถœ๋ ฅ

3


์˜ˆ์ œ 3

์ž…๋ ฅ

5 5 2
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0


์ถœ๋ ฅ

5


์˜ˆ์ œ 4

์ž…๋ ฅ

5 5 5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1


์ถœ๋ ฅ

15


์˜ˆ์ œ 5

์ž…๋ ฅ

6 5 1
1 0 1 0 1
0 1 0 1 0
1 1 0 0 0
0 0 0 1 1
1 1 0 1 1
0 0 1 0 0


์ถœ๋ ฅ

9


์˜ˆ์ œ 6

์ž…๋ ฅ

6 5 2
1 0 1 0 1
0 1 0 1 0
1 1 0 0 0
0 0 0 1 1
1 1 0 1 1
0 0 1 0 0


์ถœ๋ ฅ

14


My Sol

def archor(i, s):
    global archors, used, W, D, H, Hdec
    if i==3:           # ๊ถ์ˆ˜ 3์ž๋ฆฌ ์„ ํƒ ํ•˜๋ฉด 
        War(archors)   # War() ์‹œ์ž‘
        Hdec = H       # War ์ดํ›„ Hdec ์ดˆ๊ธฐํ™”
        return

    for k in range(s, W):
        if not used[k]:
            used[k] = 1
            archors.append(k)
            archor(i+1, k+1)
            used[k] = 0
            archors.pop()


def War(archors):
    global mat, Hdec, matdec, max_kill

    # mat์œผ๋กœ๋ถ€ํ„ฐ matdec ์ƒ์„ฑ
    matdec = []                            
    for lst in mat:
        matdec.append(lst[::])

    # ํ•œ wave๋งˆ๋‹ค sum_kill ๋ˆ„์ 
    sum_kill = 0
    while Hdec > 0:
        setArchors(archors)            # matdec์— ๋ฐฐ์น˜๋Œ€๋กœ ๊ถ์ˆ˜ ๋ฐฐ์น˜
        sum_kill += attack(archors)    # ํ•œ wave ๋งˆ๋ฌด๋ฆฌํ•œ ๊ณต๊ฒฉ ์ˆ˜์ธ attack() ํ•จ์ˆ˜ ๋ฐ˜ํ™˜๊ฐ’์„ sum_kill ์— ๋ˆ„์ 
        matdec.pop()                   # ๊ถ์ˆ˜ ๋ฐฐ์น˜ ๋นผ๊ณ 
        matdec.pop()                   # ์ ๋“ค์€ ํ•œ ์นธ ๋‚ด๋ ค์˜ด
        Hdec -= 1                      # ์ตœํ•˜๋‹จ ํ–‰ ์ธ๋ฑ์Šค์ธ Hdec 1 ๊ฐ์†Œ

    # ํ˜„์žฌ ์ตœ๊ณ  ๊ณต๊ฒฉ์ˆ˜์™€ ์ด๋ฒˆ ๋ฐฐ์น˜์˜ ๋ˆ„์  ๊ณต๊ฒฉ์ˆ˜ ๋น„๊ตํ•ด ์ตœ๋Œ“๊ฐ’์„ max_kill์— ์ €์žฅ
    max_kill = max(max_kill, sum_kill)


def setArchors(archors):
    global W, matdec

    # ์„ฑ์€ 2๋กœ ๋‘๊ณ  ๊ถ์ˆ˜์˜ ์œ„์น˜๋Š” 3์œผ๋กœ ๋‘์–ด matdec์— ์ถ”๊ฐ€
    lst = [2]*W
    for archor in archors:
        lst[archor] = 3
    matdec.append(lst)


def attack(archors):
    global matdec
    global D
    attack_lst = []

    # ๊ถ์ˆ˜๋ณ„ ์ตœ์ ์˜ ์  ์ขŒํ‘œ ๋ฐ˜ํ™˜๊ฐ’์„ attack_lst์— ์ €์žฅ
    for archor in archors:
        # ์ด๋ฒˆ ๊ถ์ˆ˜์˜ ์ตœ์ ์˜ ์  ์ขŒํ‘œ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜๊ฐ’์„ attackXY์— ์ €์žฅ
        attackXY = selectEnemy(archor, D)
        
        # ์ด๋ฒˆ ๊ถ์ˆ˜ ๋ฒ”์œ„ ๋‚ด ์  ์—†์œผ๋ฉด ๋‹ค์Œ ๊ถ์ˆ˜ ์กฐํšŒ
        if not attackXY:  
            continue
        
        # attack_lst์— ๊ฐ™์€ ์  ์ขŒํ‘œ๊ฐ€ ์—†์œผ๋ฉด ์  ์ขŒํ‘œ ์ถ”๊ฐ€
        if not attackXY in attack_lst:
            attack_lst.append(attackXY)

    # ์  ์ขŒํ‘œ ๋ฆฌ์ŠคํŠธ ๊ฐ๊ฐ ์กฐํšŒํ•ด ์  ์ œ๊ฑฐ ํ›„ ์ˆซ์ž ์„ธ๊ณ  ์ด๋ฅผ ๋ฐ˜ํ™˜
    attackCnt = 0
    for attack in attack_lst:
        ai, aj = attack
        matdec[ai][aj] = 0
        attackCnt += 1

    return attackCnt


# ๊ถ์ˆ˜๋งˆ๋‹ค ์ตœ์ ์˜ ์ ์„ ์„ ํƒํ•˜์—ฌ ์ขŒํ‘œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
def selectEnemy(archor, D):
    global Hdec, W

    # x, y ์ขŒํ‘œ์™€ ๊ถ์ˆ˜๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋‹ด์€ Queue ์ด์šฉ
    areaQ = [(Hdec, archor, 0)]
    start = -1
    end = 1
    visited = [[0]*W for _ in range(Hdec+1)]

    while start < end-1:
        start += 1
        ni, nj, dis = areaQ[start]        # Queue์˜ ํŠน์ • ์ขŒํ‘œ ์‚ฌ์šฉ

        if dis == D:                      # ํŠน์ • ์ขŒํ‘œ์™€ ๊ถ์ˆ˜๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๊ฐ€ D๋ผ๋ฉด ์ด ์ขŒํ‘œ ์ฃผ๋ณ€ ํƒ์ƒ‰ ๋ถˆ๊ฐ€
            if matdec[ni][nj]==1:         # ์ด ์ขŒํ‘œ์˜ ๊ฐ’์ด 1์ด๋ผ๋ฉด ์ด ์ขŒํ‘œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค. (์™ผ์ชฝ๋ถ€ํ„ฐ Queue์— ์ €์žฅ)
                return ni, nj
            else:
                continue

        didj = [(0,-1), (-1,0), (0,1)]                     # ์ขŒ์ƒ์šฐ ์ˆœ์œผ๋กœ ์กฐํšŒ
        for di, dj in didj:
            if 0<=ni+di and 0<=nj+dj<W:                    # ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๊ณ 
                if matdec[ni+di][nj+dj]==1:                # ์ฃผ๋ณ€ ํƒ์ƒ‰๊ฐ’์ด 1์ด๋ผ๋ฉด ๋ฐ”๋กœ ํ•ด๋‹น ์ขŒํ‘œ ๋ฐ˜ํ™˜
                    return ni+di, nj+dj

                if not visited[ni+di][nj+dj]:              # ํ™•์ธํ•˜์ง€ ์•Š์•˜๋˜ ๊ณณ์ด๋ผ๋ฉด
                    visited[ni+di][nj+dj] = 1              # ํ•ด๋‹น ์ขŒํ‘œ๋ฅผ ํ™•์ธํ–ˆ๋‹ค๊ณ  ์ฒดํฌ
                    areaQ.append((ni+di, nj+dj, dis+1))    # ํ˜„์žฌ ์ขŒํ‘œ๊ฑฐ๋ฆฌ์—์„œ ๊ฑฐ๋ฆฌ๋ฅผ 1 ์ถ”๊ฐ€ํ•˜๊ณ  ์ขŒํ‘œ์™€ ํ•จ๊ป˜ areaQ์— ์‚ฝ์ž…
                    end += 1

H, W, D = map(int, input().split())
mat = [list(map(int, input().split())) for _ in range(H)]
Hdec = H
matdec = []

archors = []
used = [0]*W
enemies = []
max_kill = 0
archor(0, 0)

print(max_kill)

๊ณต๊ฒฉํ•  ๋Œ€์ƒ์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ๋„, ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ๊ถ์ˆ˜๊ฐ€ ํ•œ ๋ฒˆ์— ๊ณต๊ฒฉ์„ ํ•ด์•ผํ•˜๋Š” ๊ฒƒ๋„, ์ ์ด ๋‚ด๋ ค์˜ค๊ธฐ ๋•Œ๋ฌธ์— mat ์ž์ฒด๋ฅผ ๋ฐ”๊ฟ”์ค˜์•ผํ•˜๋Š” ๊ฒƒ๋„ ๊ฝค๋‚˜ ๊นŒ๋‹ค๋กœ์šด ๋ฌธ์ œ์˜€๋‹ค. ํ•จ์ˆ˜๋ฅผ ๋‚˜๋ˆ„์–ด ๋ชจ๋“ˆํ™”ํ•˜์˜€๊ณ , ์ฝ”๋“œ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์ž์ž˜ํ•œ ์‹ค์ˆ˜๋„ ์ฃผ์˜ํ•ด์•ผ ํ–ˆ๋‹ค.

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

๊ฐ ์ฝ”๋“œ๋ณ„ ์„ค๋ช…์„ ํ•˜์œ„์— ํ•˜๊ธฐ์—๋Š” ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ์–ด์„œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋งˆ๋‹ค ์ฃผ์„์œผ๋กœ ์„ค๋ช…์„ ๋‹ฌ๊ฒ ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ