[BOJ][๐ŸŸก3][๋ฐฑ์ค€#20058] ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ํŒŒ์ด์–ด์Šคํ†ฐ

์ž‘์„ฑ:    

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

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

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #20058


๋ฌธ์ œ

๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๋Š”ย ํŒŒ์ด์–ด๋ณผ๊ณผ ํ† ๋„ค์ด๋„๋ฅผ ์กฐํ•ฉํ•ดย ํŒŒ์ด์–ด์Šคํ†ฐ์„ ์‹œ์ „ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋Š˜์€ ํŒŒ์ด์–ด์Šคํ†ฐ์„ ํฌ๊ธฐ๊ฐ€ 2Nย ร— 2N์ธ ๊ฒฉ์ž๋กœ ๋‚˜๋ˆ„์–ด์ง„ ์–ผ์ŒํŒ์—์„œ ์—ฐ์Šตํ•˜๋ ค๊ณ  ํ•œ๋‹ค.ย ์œ„์น˜ (r,ย c)๋Š” ๊ฒฉ์ž์˜ rํ–‰ c์—ด์„ ์˜๋ฏธํ•˜๊ณ , A[r][c]๋Š” (r, c)์— ์žˆ๋Š” ์–ผ์Œ์˜ ์–‘์„ ์˜๋ฏธํ•œ๋‹ค. A[r][c]๊ฐ€ 0์ธ ๊ฒฝ์šฐ ์–ผ์Œ์ด ์—†๋Š” ๊ฒƒ์ด๋‹ค. ํŒŒ์ด์–ด์Šคํ†ฐ์„ ์‹œ์ „ํ•˜๋ ค๋ฉด ์‹œ์ „ํ•  ๋•Œ๋งˆ๋‹ค ๋‹จ๊ณ„ L์„ ๊ฒฐ์ •ํ•ด์•ผ ํ•œ๋‹ค. ํŒŒ์ด์–ด์Šคํ†ฐ์€ย ๋จผ์ € ๊ฒฉ์ž๋ฅผ 2Lย ร— 2Lย ํฌ๊ธฐ์˜ ๋ถ€๋ถ„ ๊ฒฉ์ž๋กœ ๋‚˜๋ˆˆ๋‹ค. ๊ทธ ํ›„, ๋ชจ๋“  ๋ถ€๋ถ„ ๊ฒฉ์ž๋ฅผ ์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ 90๋„ ํšŒ์ „์‹œํ‚จ๋‹ค. ์ดํ›„ ์–ผ์Œ์ด ์žˆ๋Š” ์นธ 3๊ฐœ ๋˜๋Š” ๊ทธ ์ด์ƒ๊ณผ ์ธ์ ‘ํ•ด์žˆ์ง€ ์•Š์€ย ์นธ์€ย ์–ผ์Œ์˜ย ์–‘์ด 1 ์ค„์–ด๋“ ๋‹ค. (r, c)์™€ ์ธ์ ‘ํ•œ ์นธ์€ (r-1, c), (r+1, c), (r, c-1), (r, c+1)์ด๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์˜ ์นธ์— ์ ํžŒ ์ •์ˆ˜๋Š” ์นธ์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ์ ์€ ์ •์ˆ˜์ด๋‹ค.

๋งˆ๋ฒ•์„ ์‹œ์ „ํ•˜๊ธฐ ์ „ L = 1 L = 2

๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๋Š” ํŒŒ์ด์–ด์Šคํ†ฐ์„ ์ด Q๋ฒˆ ์‹œ์ „ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋ชจ๋“  ํŒŒ์ด์–ด์Šคํ†ฐ์„ ์‹œ์ „ํ•œ ํ›„, ๋‹ค์Œ 2๊ฐ€์ง€๋ฅผ ๊ตฌํ•ด๋ณด์ž.

๋‚จ์•„์žˆ๋Š” ์–ผ์Œ A[r][c]์˜ ํ•ฉ ๋‚จ์•„์žˆ๋Š” ์–ผ์Œ ์ค‘ ๊ฐ€์žฅ ํฐ ๋ฉ์–ด๋ฆฌ๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜

์–ผ์Œ์ด ์žˆ๋Š” ์นธ์ดย ์–ผ์Œ์ด ์žˆ๋Š” ์นธ๊ณผ ์ธ์ ‘ํ•ด ์žˆ์œผ๋ฉด, ๋‘ ์นธ์„ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๋ฉ์–ด๋ฆฌ๋Š” ์—ฐ๊ฒฐ๋œ ์นธ์˜ ์ง‘ํ•ฉ์ด๋‹ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N๊ณผ Q๊ฐ€ย ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ 2N๊ฐœ์˜ ์ค„์—๋Š” ๊ฒฉ์ž์˜ ๊ฐ ์นธ์— ์žˆ๋Š” ์–ผ์Œ์˜ ์–‘์ด ์ฃผ์–ด์ง„๋‹ค. r๋ฒˆ์งธ ์ค„์—์„œ c๋ฒˆ์งธ ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š”ย A[r][c] ์ด๋‹ค. ๋งˆ์ง€๋ง‰ ์ค„์—๋Š” ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๊ฐ€ ์‹œ์ „ํ•œ ๋‹จ๊ณ„ L1, L2, โ€ฆ, LQ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋‚จ์•„์žˆ๋Š” ์–ผ์Œ A[r][c]์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•˜๊ณ , ๋‘˜์งธ ์ค„์— ๊ฐ€์žฅ ํฐ ๋ฉ์–ด๋ฆฌ๊ฐ€ ์ฐจ์ง€ํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.ย ๋‹จ, ๋ฉ์–ด๋ฆฌ๊ฐ€ ์—†์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.


์ œํ•œ

2 โ‰ค N โ‰ค 6 1 โ‰ค Q โ‰ค 1,000 0 โ‰ค A[r][c] โ‰ค 100 0 โ‰ค Li โ‰ค N


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

3 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1


์ถœ๋ ฅ

284
64


์˜ˆ์ œ 2

์ž…๋ ฅ

3 2
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2


์ถœ๋ ฅ

280
64


์˜ˆ์ œ 3

์ž…๋ ฅ

3 5
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 0 3 2


์ถœ๋ ฅ

268
64


์˜ˆ์ œ 4

์ž…๋ ฅ

3 10
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 0 3 2 1 2 3 2 3


์ถœ๋ ฅ

248
62


์˜ˆ์ œ 5

์ž…๋ ฅ

3 10
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1 2 3 1 2 3 1 2 3 1


์ถœ๋ ฅ

246
60


์˜ˆ์ œ 6

์ž…๋ ฅ

3 10
1 0 3 4 5 6 7 0
8 0 6 5 4 3 2 1
1 2 0 4 5 6 7 0
8 7 6 5 4 3 2 1
1 2 3 4 0 6 7 0
8 7 0 5 4 3 2 1
1 2 3 4 5 6 7 0
0 7 0 5 4 3 2 1
1 2 3 1 2 3 1 2 3 1


์ถœ๋ ฅ

37
9


My Sol

import sys
input = sys.stdin.readline

def main():
    def rotate():
        for i in range(0, TL, SL):
            for j in range(0, TL, SL):
                rotate_square(i, j)

    def rotate_square(si, sj):
        square_new = [[0]*SL for _ in range(SL)]
        for i in range(SL):
            for j in range(SL):
                square_new[i][j] = mat[si+SL-1-j][sj+i]

        for i in range(SL):
            for j in range(SL):
                mat[si+i][sj+j] = square_new[i][j]

    def remove_ice():
        del_arr = []
        for i in range(TL):
            for j in range(TL):
                if not mat[i][j]: continue
                ice_cnt = 0
                for di, dj in ((-1,0),(1,0),(0,1),(0,-1)):
                    si, sj = i+di, j+dj
                    if not (0<=si<TL and 0<=sj<TL): continue
                    if not mat[si][sj]: continue
                    ice_cnt += 1

                if ice_cnt < 3:
                    del_arr.append((i, j))

        for ki, kj in del_arr:
            mat[ki][kj] -= 1

    # ์ „์ฒด ์–ผ์Œ ์–‘ ๊ณ„์‚ฐ
    def calc_sum_ice():
        sum_ice = 0
        for i in range(TL):
            for j in range(TL):
                sum_ice += mat[i][j]
        return sum_ice

    # ์ตœ๋Œ€ ์–ผ์Œ ํฌ๊ธฐ ๊ณ„์‚ฐ
    def calc_max_ice():
        def calc_ice(i, j):
            Q = set()
            Q.add((i, j))
            cur_ice = 0
            while Q:
                i, j = Q.pop()
                if not (0<=i<TL and 0<=j<TL): continue
                if visit[i][j] or not mat[i][j]: continue
                visit[i][j] = 1
                cur_ice += 1
                for di, dj in ((-1,0),(1,0),(0,1),(0,-1)):
                    Q.add((i+di, j+dj))
            return cur_ice

        visit = [[0]*TL for _ in range(TL)]
        max_ice = 0
        for i in range(TL):
            for j in range(TL):
                if not mat[i][j]: continue
                cur_ice = calc_ice(i, j)
                if max_ice < cur_ice:
                    max_ice = cur_ice
        return max_ice

    N, Q = map(int, input().split())
    TL = 2**N
    mat = [list(map(int, input().split())) for _ in range(TL)]
    qs = list(map(int, input().split()))
    for q in qs:
        SL = 2**q
        if q: rotate()
        remove_ice()

    print(calc_sum_ice())
    print(calc_max_ice())

main()

๋‹จ์ˆœ ๊ตฌํ˜„๋ฌธ์ œ์˜€๋‹ค. 6๋‹ฌ ์ „์— ํ•ด๊ฒฐ์— ์‹คํŒจํ–ˆ๋Š”๋ฐ, ๋‹ค์‹œ ๋ณด๋‹ˆ input์˜ ํฌ๊ธฐ๊ฐ€ ์ตœ๋Œ€ 1000x1000์œผ๋กœ ํฌ๊ธฐ๊ฐ€ ์ปค์„œ sys.stdin.readline์˜ ์ž…๋ ฅ ์ฒ˜๋ฆฌ๋งŒ ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

  1. ์œ„์—์„œ ํ–ˆ๋“ฏ, ์ž…๋ ฅ ํฌ๊ธฐ๊ฐ€ ํฌ๋ฏ€๋กœ readline์„ ์‚ฌ์šฉํ•ด ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. 2์˜ N์ œ๊ณฑ๋งŒํผ ์ „์ฒด 2์ฐจ์› ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง€๋ฏ€๋กœ ๊ทธ ์ „์ฒด ๊ธธ์ด๋ฅผ TL๋กœ, 2์ฐจ์› ๋ฐฐ์—ด์„ mat์œผ๋กœ ๋ช…๋ช…ํ•ด ์ฒ˜๋ฆฌํ–ˆ๋‹ค. ๊ฐ q๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ qs๋กœ ๋ช…๋ช…ํ•ด ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›์•˜๋‹ค.
  2. q๋Š” 2์˜ q์ œ๊ณฑ์„ ๋‹จ์œ„๋กœ 90๋„ ์‹œ๊ณ„๋ฐฉํ–ฅ ํšŒ์ „ํ•˜๋ฏ€๋กœ ์ด ๊ฐ q๋งˆ๋‹ค ๋ชจ๋“  ๋ฃจํ‹ด์„ ์ง„ํ–‰ํ•˜๋ฉด ๋˜๊ฒ ๋‹ค. ์ด๋•Œ 2์˜ q์ œ๊ณฑ์„ SL๋กœ ๋ช…๋ช…ํ–ˆ๋‹ค.
  3. q๊ฐ€ 0์ด๋ฉด ํšŒ์ „ ๋‹จ์œ„๋Š” 1์ด๋ฏ€๋กœ ํšŒ์ „ ์ „ํ›„๊ฐ€ ๊ฐ™๋‹ค. ๋•Œ๋ฌธ์— q๊ฐ€ 1 ์ด์ƒ์ผ ๋•Œ๋งŒ ํšŒ์ „์‹œํ‚ค๋Š” rotate ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ–ˆ๋‹ค.
  4. rotate ํ•จ์ˆ˜๋Š” mat์˜ ๊ฐ SL ๋‹จ์œ„ ๊ฒฉ์ž๋งˆ๋‹ค rotate_square ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์‹œ์ž‘์ ์„ ๊ธฐ์ค€์œผ๋กœ ๊ธฐ์กด mat์˜ ํ•ด๋‹น ๊ฒฉ์ž๋ฅผ 90๋„ ํšŒ์ „์‹œ์ผœ mat์— ๋‹ค์‹œ ์ ์šฉ์‹œ์ผœ์ค€๋‹ค.
  5. ์ด๋ ‡๊ฒŒ ๋ชจ๋“  ๋‹จ์œ„๊ฒฉ์ž๋งˆ๋‹ค rotate_square์„ ์‹คํ–‰ํ•˜๋Š” rotate ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋งˆ์น˜๋ฉด, ์ฃผ๋ณ€์˜ ์–ผ์Œ์„ ํ™•์ธํ•ด ๋…น์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ remove_ice๋ผ๊ณ  ๋ช…๋ช…ํ–ˆ๋‹ค.
  6. remove_ice ํ•จ์ˆ˜์—์„œ ๋ชจ๋“  mat์˜ ์–ผ์Œ์ด ์žˆ๋Š” ํฌ์ธํŠธ์˜ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ํ™•์ธํ•ด ๊ฐ’์ด ์žˆ๋Š” ์ƒํ•˜์ขŒ์šฐ ํฌ์ธํŠธ๊ฐ€ 3์ดํ•˜์ด๋ฉด ์–ผ์Œ์„ ๋…น์—ฌ์ฃผ๋Š” del_arr์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ด๋•Œ ๋ฐ”๋กœ ํ•ด๋‹น ํฌ์ธํŠธ์˜ ๊ฐ’์„ ๊นŽ์ง€ ์•Š๋Š” ์ด์œ ๋Š” ํ•ด๋‹น ํฌ์ธํŠธ๊ฐ€ 1์—์„œ 0์œผ๋กœ, ์™„์ „ํžˆ ๋ฌผ์ด ๋˜์—ˆ์„ ๋•Œ ๋‹ค๋ฅธ ๊ฒฉ์ž์˜ ์ฃผ๋ณ€ ์ฒดํฌ์— ์˜ํ–ฅ์„ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋•Œ๋ฌธ์— ํ˜„ ์‹œ์ ์„ ์™„์ „ํžˆ ๊ณ ์ •ํ•˜๊ณ , ํ˜„ ์‹œ์ ์—์„œ ์–ผ์Œ์„ ๋…น์—ฌ์ฃผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ชจ๋‘ ๋ชจ์€ ๋’ค, ํ•œ ๋ฒˆ์— 1์”ฉ ๊นŽ์•„์ฃผ๊ฒŒ ๋œ๋‹ค.
  7. ์œ„์˜ 2๋ถ€ํ„ฐ 6์˜ ๋ฃจํ‹ด์„ ๋ชจ๋“  q์— ๋Œ€ํ•˜์—ฌ ์ง„ํ–‰ํ•ด์ฃผ๋ฉด, ๋‚จ์€ mat์œผ๋กœ๋ถ€ํ„ฐ ์ „์ฒด ์–ผ์Œ ์–‘๊ณผ ์ตœ๋Œ€ ์–ผ์Œ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค. ์ด๋ฅผ ๊ฐ๊ฐ calc_sum_ice, calc_max_ice ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.
  8. calc_sum_ice๋Š” mat์„ ๋ชจ๋‘ ๋‹จ์ˆœ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ’์„ ๋”ํ•ด ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  9. calc_max_ice๋Š” ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์–ผ์Œ ์นธ์— ๋„๋‹ฌํ•˜๋ฉด ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์–ผ์Œ๋“ค์„ ํ™•์ธํ•˜๋ฉฐ ์ „์ฒด ์–ผ์Œ๋ฉ์–ด๋ฆฌ์˜ ํฌ๊ธฐ๋ฅผ ๋ณด๋Š” calc_ice BFSํ•จ์ˆ˜๋ฅผ ๋งค๋ฒˆ ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ์ด๋•Œ calc_ice ํ•จ์ˆ˜๋กœ ๊ฐ ๋ฉ์–ด๋ฆฌ๋“ค์˜ ํฌ๊ธฐ๋ฅผ ํŒŒ์•…ํ•˜๋ฉด, ์ด ๊ฐ’๋“ค์„ ๋ˆ„์  ์ตœ๋Œ“๊ฐ’๊ณผ ๋Œ€์กฐํ•˜์—ฌ ์ „์ฒด ์–ผ์Œ๋ฉ์–ด๋ฆฌ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์ด๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  10. ์ด๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค.


๊ฒฐ๊ณผ

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

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