[BOJ][๐ŸŸก3][๋ฐฑ์ค€#01888] ๊ณฐํŒก์ด

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1888


๋ฌธ์ œ

๋ฒฝ์— ๊ณฐํŒก์ด๊ฐ€ ์ž๋ผ๊ณ  ์žˆ๋‹ค. ๊ณฐํŒก์ด๋“ค์€ ํ˜„์žฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฉ์–ด๋ฆฌ๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ๋Š” ์ƒํƒœ์ธ๋ฐ, ์ด๋“ค์ด ์ ์  ์ž๋ผ๋‚˜์„œ ํ•œ ๋ฉ์–ด๋ฆฌ๋กœ ๋  ๋•Œ๊นŒ์ง€ ์–ผ๋งˆ์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด์ง€ ์•Œ๊ณ  ์‹ถ๋‹ค. ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด ๋ณด์ž. ๊ณฐํŒก์ด๊ฐ€ ํ”ผ์–ด ์žˆ๋Š” ๋ฒฝ์€ mํ–‰ n์—ด์˜ ๊ฒฉ์ž๋กœ ๋‚˜๋‰˜์–ด ์žˆ๊ณ , ํ•œ ์นธ ๋‹น ํ•œ ๊ฐœ์˜ ๊ณฐํŒก์ด๊ฐ€ ์žˆ๋‹ค. ๊ณฐํŒก์ด์˜ ๋ฉ์–ด๋ฆฌ๋ผ๋Š” ๊ฒƒ์€, ๊ฒฉ์ž ์ƒ์— ๊ฐ€๋กœ์„ธ๋กœ๋กœ ์ธ์ ‘ํ•œ ๊ณฐํŒก์ด๋“ค์˜ ์ง‘ํ•ฉ์„ ๋งํ•œ๋‹ค. ๋งจ ์ฒ˜์Œ ์ƒํƒœ์—์„œ๋Š” ํ•œ ๋ฉ์–ด๋ฆฌ ์•ˆ์˜ ๊ณฐํŒก์ด๋“ค์ด ๋ชจ๋‘ ๊ฐ™์€ ์ข…์œผ๋กœ, ์ž๋ผ๋Š” ์†๋„๋„ ๊ฐ™๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฉ์–ด๋ฆฌ์— ์†ํ•œ ๊ณฐํŒก์ด๋Š” ์ข…์ด ๋‹ฌ๋ผ ์ž๋ผ๋Š” ์†๋„๋„ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค. ๋˜, ์‹œ๊ฐ„์ด ์ง€๋‚จ์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธ ์ข…์˜ ๊ณฐํŒก์ด ๋ฉ์–ด๋ฆฌ๊ฐ€ ํ•œ ๋ฉ์–ด๋ฆฌ๋กœ ํ•ฉ์ณ์ง€๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ์–ด๋Š ๊ณฐํŒก์ด์˜ ์ž๋ผ๋Š” ์†๋„๊ฐ€ k๋ผ๋ฉด, ํ•˜๋ฃจ๊ฐ€ ์ง€๋‚ฌ์„ ๋•Œ ๊ทธ ๊ณฐํŒก์ด๊ฐ€ ํ”ผ์–ด์žˆ๋˜ ์ž๋ฆฌ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ 2k+1ํ–‰ 2k+1์—ด์˜ ๊ฒฉ์ž์— ๊ฐ™์€ ์ข…์˜ ๊ณฐํŒก์ด๊ฐ€ ๋ฒˆ์ง„๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ๋งŒ์•ฝ ์„œ๋กœ ๋‹ค๋ฅธ ์ข…์˜ ๊ณฐํŒก์ด๊ฐ€ ๊ฐ™์€ ์นธ์— ๋ฒˆ์ ธ ์˜ค๋ฉด, ์ž๋ผ๋Š” ์†๋„๊ฐ€ ๋น ๋ฅธ ๊ณฐํŒก์ด๊ฐ€ ๊ทธ ์นธ์„ ์ฐจ์ง€ํ•œ๋‹ค.


์ž…๋ ฅ

์ฒซ ์ค„์— ๊ณฐํŒก์ด๊ฐ€ ํ”ผ์–ด ์žˆ๋Š” ๋ฒฝ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ m๊ณผ n์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค m, n โ‰ค100) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ๋Š” ๋ฒฝ์˜ ์ƒํ™ฉ์ด ํ•œ ์ค„์— ํ•œ ํ–‰์”ฉ ์ฃผ์–ด์ง„๋‹ค. ๊ณฐํŒก์ด๊ฐ€ ํ”ผ์–ด์žˆ๋Š” ๊ณณ์€ ๊ทธ ๊ณฐํŒก์ด์˜ ์ž๋ผ๋Š” ์†๋„๋กœ, ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ณณ์€ 0์œผ๋กœ ํ‘œ์‹œ๋˜์–ด ์žˆ๋‹ค. ์ž๋ผ๋Š” ์†๋„๋Š” 1์ด์ƒ 5์ดํ•˜์˜ ์ •์ˆ˜์ด๋‹ค. ๊ฐ ์ˆซ์ž ์‚ฌ์ด์—๋Š” ๋นˆ ์นธ์ด ์—†๋‹ค.


์ถœ๋ ฅ

์ฒซ ์ค„์— ๊ณฐํŒก์ด๊ฐ€ ํ•œ ๋ฉ์–ด๋ฆฌ๊ฐ€ ๋˜๊ธฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ํ•˜๋ฃจ ๋‹จ์œ„๋กœ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

5 15
002000000000011
022000000011111
020000000010000
000000011110111
000000011110111


์ถœ๋ ฅ

2


My Sol

def main():
    def find_positions():
        positions = dict()
        for n in range(6):
            positions[n] = set()

        for i in range(I):
            for j in range(J):
                positions[mat[i][j]].add((i, j))

        positions.pop(0)
        nums = []
        for n in range(5, 0, -1):
            if positions[n]:
                nums.append(n)
            else:
                positions.pop(n)
        return nums, positions

    def get_all_values_set():
        all_values_set = set()
        for s in positions.values():
            all_values_set |= s
        return all_values_set

    def check_is_one():
        # ๊ฐ’์ด ์žˆ๋Š” ํ•œ ์  ์ฐพ๊ธฐ
        all_values_set = get_all_values_set()
        fi, fj = all_values_set.pop()
        all_values_set.add((fi, fj))

        # ํ•ด๋‹น ์ขŒํ‘œ๋กœ๋ถ€ํ„ฐ BFS
        visit = set()
        Q = {(fi, fj)}
        while Q:
            ni, nj = Q.pop()
            if (ni, nj) in visit: continue
            visit.add((ni, nj))
            for di, dj in ((-1,0),(1,0),(0,-1),(0,1)):
                si, sj = ni+di, nj+dj
                if not (0<=si<I and 0<=sj<J): continue
                if not mat[si][sj]: continue
                if (si, sj) in visit: continue
                Q.add((si, sj))

        # visit๊ณผ all_values_set์ด ๊ฐ™์œผ๋ฉด one group
        other_group = all_values_set-visit
        return False if other_group else True

    def expand():
        for n in nums:
            expand_set = set()
            for ni, nj in positions[n]:
                for di in range(-n, n+1):
                    si = ni+di
                    for dj in range(-n, n+1):
                        sj = nj+dj
                        if not (0<=si<I and 0<=sj<J): continue
                        if mat[si][sj] >= n: continue
                        expand_set.add((si, sj))
            for ei, ej in expand_set:
                mat[ei][ej] = n

            for m in nums:
                if m==n:
                    positions[m] |= expand_set
                else:
                    positions[m] -= expand_set

    I, J = map(int, input().split())
    mat = [list(map(int, input())) for _ in range(I)]
    nums, positions = find_positions()
    day = 0
    while True:
        if check_is_one(): return day
        expand()
        day += 1

print(main())

์—ฌ๋Ÿฌ๋ชจ๋กœ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด ๊ณ ๋ฏผ์„ ๋งŽ์ด ํ–ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค. BFS๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๋ชจ๋“  ์ฝ”๋“œ๋Š” main ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ฒ˜๋ฆฌ๋œ๋‹ค.

  1. ๊ฐ€๋กœ ์„ธ๋กœ ๊ณฐํŒก์ด 2์ฐจ์›์„ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›์•„์ค€๋‹ค.
  2. ๊ณฐํŒก์ด 2์ฐจ์› ๋ฐฐ์—ด mat์˜ ๊ฐ ์ขŒํ‘œ๋“ค์„ ๋ชจ๋‘ ์ˆœํšŒํ•˜๋ฉฐ ๊ณฐํŒก์ด ์ข…๋ฅ˜์™€ ์ขŒํ‘œ๋ฅผ ํ™•์ธํ•œ๋‹ค. ๊ณฐํŒก์ด ์ขŒํ‘œ๋ฅผ ๋ชจ๋‘ ์ €์žฅํ•œ ๋’ค, ๊ฐ’์ด 0์ด๊ฑฐ๋‚˜ ์ขŒํ‘œ๊ฐ€ ์—†๋Š” ๊ฐ’๋“ค์€ dictionary์—์„œ ์ œ๊ฑฐํ•ด์ค€๋‹ค. ์ด ๊ณฐํŒก์ด ์ข…๋ฅ˜(ํ™•์žฅ์†๋„)๋ฅผ ๋ชจ์•„๋‘” ๋ฆฌ์ŠคํŠธ๋Š” nums, ๊ฐ ์ข…๋ฅ˜๋ฅผ key๋กœ ํ•˜๊ณ  ์ขŒํ‘œ๋“ค์„ set์œผ๋กœ ์ €์žฅํ•˜๋Š” positions๋กœ ๋‘์—ˆ๋‹ค. ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํ•จ์ˆ˜๋Š” find__positions ํ•จ์ˆ˜์ด๋‹ค.
  3. ํ™•์žฅ์ด ์—†์ด๋„ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์ผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 0์ผ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ํ•œ ๋ฉ์–ด๋ฆฌ์ธ์ง€ ์ฒดํฌํ•œ๋‹ค. ์ด๋ฅผ check_is_one ํ•จ์ˆ˜๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํ™•์ธํ–ˆ๋‹ค.
  4. mat์„ ๋ชจ๋‘ ์ˆœํšŒํ•˜์ง€ ์•Š๊ณ , ๋ณด๋‹ค ๋˜‘๋˜‘ํ•œ ๋ฐฉ๋ฒ•์„ ์“ฐ๊ธฐ๋กœ ํ–ˆ๋‹ค. ์šฐ์„  positions์˜ ๋ชจ๋“  ์ขŒํ‘œ๋“ค์„ ๋ชจ์•„๋‘” set์„ all_values_set์œผ๋กœ ๋‘์—ˆ๋‹ค. ์ด๋Š” set์˜ ํ•ฉ์ง‘ํ•ฉ ์—ฐ์‚ฐ์œผ๋กœ ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ดํ›„ ์ด set์—์„œ ํ•œ ์ขŒํ‘œ๋ฅผ ๊บผ๋‚ด ์ƒํ•˜์ขŒ์šฐ๋กœ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ๊ฐ’์ด ์žˆ๋Š” ์ขŒํ‘œ๋“ค์— ๋Œ€ํ•ด visit์„ ์ฒดํฌํ•œ๋‹ค.
  5. ์ด๋ ‡๊ฒŒ ํ•œ ๋ฉ์–ด๋ฆฌ์— ๋Œ€ํ•ด ์ฒดํฌํ•˜๋ฉด visit set์—๋Š” ํ•œ ์ขŒํ‘œ์—์„œ ์‹œ์ž‘ํ•œ ํ•œ ๋ฉ์–ด๋ฆฌ์˜ ๋ชจ๋“  ์ขŒํ‘œ๋“ค์ด ์ €์žฅ๋  ๊ฒƒ์ด๋‹ค. ์ด๋ฅผ ์ „์ฒด mat์˜ ๋ชจ๋“  ๊ฐ’๋“ค์„ ๋ชจ์€ all_values_set๊ณผ์˜ ์ฐจ์ง‘ํ•ฉ์„ ๊ตฌํ•œ๋‹ค. ๋งŒ์•ฝ mat ๋‚ด์˜ ๊ณฐํŒก์ด๊ฐ€ ํ•œ ๋ฉ์–ด๋ฆฌ๋ผ๋ฉด visit set์—๋Š” ๋ชจ๋“  ๊ณฐํŒก์ด์˜ ์ขŒํ‘œ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๊ณ , ์ด๋ฏธ ๋ชจ๋“  ๊ณฐํŒก์ด์˜ ์ขŒํ‘œ set์ธ all_values_set์™€์˜ ์ฐจ์ง‘ํ•ฉ์€ ๋นˆ set์ด ๋  ๊ฒƒ์ด๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํ•œ ๋ฉ์–ด๋ฆฌ์ž„์„ ํ™•์ธํ–ˆ๋‹ค.
  6. ๋งŒ์•ฝ check_is_one ์ด True๋ผ๋ฉด day๋ฅผ ๋ฐ”๋กœ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๊ณ , ์•„๋‹ˆ๋ผ๋ฉด ๊ณฐํŒก์ด์˜ ํ™•์žฅ์ด ์‹œ์ž‘๋œ๋‹ค. ์ด๋ฅผ expand ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.
  7. ๊ฐ’์ด ํฐ ๊ณฐํŒก์ด ์ข…๋ฅ˜๋ถ€ํ„ฐ ํ™•์žฅ์„ ์‹œ์ž‘ํ•˜๋Š”๋ฐ, nums์— ๊ณฐํŒก์ด ์ข…๋ฅ˜๋ฅผ ์ง€์ •ํ•  ๋•Œ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ํ™•์ธํ•˜๋ฉฐ ๊ฐ’์„ ์ถ”๊ฐ€ํ–ˆ์œผ๋ฏ€๋กœ ํ•ด๋‹นํ•˜๋Š” ๊ฐ’๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.
  8. ํ•ด๋‹น ๊ณฐํŒก์ด ์ข…๋ฅ˜์˜ ๋ชจ๋“  ์ขŒํ‘œ๋“ค์˜ ํ™•์žฅ ๋ฒ”์œ„๋ฅผ di, dj๋ฅผ ๊ตฌํ•ด์„œ ํ™•์žฅํ•˜๋ ค๋Š” expand_set์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ด๋Š” ํ•œ ๊ณฐํŒก์ด ์ข…๋ฅ˜๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๊ณ , ๊ฐ’์ด ์—†๊ฑฐ๋‚˜ ์ž๊ธฐ ์ข…๋ฅ˜๋ณด๋‹ค ์ž‘์€ ๊ฐ’์ด ์žˆ๋Š” ์ขŒํ‘œ๋ฅผ expand_set์— ์ถ”๊ฐ€ํ•œ๋‹ค.
  9. ์ดํ›„ expand_set์˜ ๊ฐ ์ขŒํ‘œ๋ฅผ ๋ชจ๋‘ mat์— ํ‘œ์‹œํ•œ๋‹ค. ๊ฒฐ๊ตญ ํ•ด๋‹นํ•˜๋Š” ์ขŒํ‘œ๋กœ ํ•ด๋‹น ์ข…๋ฅ˜์˜ ๊ณฐํŒก์ด๊ฐ€ ํ™•์žฅํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  10. positions์—์„œ ํ˜„์žฌ ํ™•์žฅํ•˜๋ ค๋Š” ๊ณฐํŒก์ด ์ข…๋ฅ˜์˜ set์—๋Š” expand_set์˜ ํ•ฉ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ๊ทธ ์™ธ์˜ ๊ณฐํŒก์ด์— ๋Œ€ํ•ด์„œ๋Š” expand_set๋ฅผ ๋นผ์„œ ์ฐจ์ง‘ํ•ฉ์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  11. ์ด๋ฅผ ๋ชจ๋“  ๊ณฐํŒก์ด ์ข…๋ฅ˜์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•œ๋‹ค.
  12. ํ™•์žฅ์„ ๋งˆ์ณค๋‹ค๋ฉด ๋‹ค์‹œ ์œ„์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ํ•œ ๊ทธ๋ฃน์ž„์„ ํ™•์ธํ•˜๋ฉด ๋ฐ˜๋ณต์„ ๋งˆ์น˜๊ณ  day๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


๊ฒฐ๊ณผ

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

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