ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 12100 2048(easy) with Python
    PS 2021. 10. 25. 23:34
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 12100 2048(easy)

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ๋ณด๋“œ์˜ ํฌ๊ธฐ๋Š” N * N (1 โ‰ค N โ‰ค 20)
      0 ์€ ๋นˆ์นธ, ์ด์™ธ์˜ ๊ฐ’์€ ๋ธ”๋ก์˜ ๊ฐ’๋“ค์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
      ๋ธ”๋ก์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๋Š” 2๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1024๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ 2์˜ ์ œ๊ณฑ๊ผด์ด๋‹ค.
      ๋ธ”๋ก์€ ์ ์–ด๋„ ํ•˜๋‚˜ ์ฃผ์–ด์ง„๋‹ค.

    2. ๊ฐ™์€ ๊ฐ’์„ ๊ฐ–๋Š” ๋‘ ๋ธ”๋ก์ด ์ถฉ๋Œํ•˜๋ฉด ๋‘ ๋ธ”๋ก์€ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ง€๊ฒŒ ๋œ๋‹ค.

    3. ํ•œ ๋ฒˆ์˜ ์ด๋™์—์„œ ์ด๋ฏธ ํ•ฉ์ณ์ง„ ๋ธ”๋ก์€ ๋˜ ๋‹ค๋ฅธ ๋ธ”๋ก๊ณผ ๋‹ค์‹œ ํ•ฉ์ณ์งˆ ์ˆ˜ ์—†๋‹ค.

    4. ์ตœ๋Œ€ ๋‹ค์„ฏ๋ฒˆ ์ด๋™ ์‹œ์ผœ์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ๋ธ”๋ก์˜ ๊ฐ’์„ ์ถœ๋ ฅ.

    5. ๋ฐฑํŠธ๋ž˜ํ‚น ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin, setrecursionlimit
    from collections import deque
    
    setrecursionlimit(int(1e9))
    
    n = int(stdin.readline())
    arr = [list(map(int, stdin.readline().split())) for _ in range(n)]
    answer, q = 0, deque()
    
    
    def get(i, j):
        if arr[i][j]:  
            q.append(arr[i][j])  
            arr[i][j] = 0  
    
    
    def merge(i, j, di, dj):  
        while q:
            x = q.popleft()  
            if not arr[i][j]:  
                arr[i][j] = x
            elif arr[i][j] == x:  
                arr[i][j] = x * 2  
                i, j = i + di, j + dj
            else:  
                i, j = i + di, j + dj
                arr[i][j] = x
    
    
    def move(k):
    
        if k == 0:  
            for j in range(n):
                for i in range(n):
                    get(i, j)
                merge(0, j, 1, 0)  
        elif k == 1:  
            for j in range(n):
                for i in range(n - 1, -1, -1):
                    get(i, j)
                merge(n - 1, j, -1, 0)  
        elif k == 2:  
            for i in range(n):
                for j in range(n):
                    get(i, j)
                merge(i, 0, 0, 1)  
        else:  
            for i in range(n):
                for j in range(n - 1, -1, -1):
                    get(i, j)
                merge(i, n - 1, 0, -1)  
    
    
    def recursive(count):
        global arr, answer
        if count == 5:  
            for i in range(n):
                answer = max(answer, max(arr[i]))  
            return
        b = [x[:] for x in arr]  
    
        for k in range(4):  
            move(k)  
            recursive(count + 1)  
            arr = [x[:] for x in b]
    
    
    recursive(0)
    print(answer)

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    3
    2 2 2
    4 4 4
    8 8 8

    ์‹คํ–‰๊ฒฐ๊ณผ

    16

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ๋ฐฑํŠธ๋ž˜ํ‚น์€ ์žฌ๊ท€๊ฐ€ ๊ฐ€์žฅ ํ•ต์‹ฌ.
      ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ ์žฌ๊ท€๋ฅผ ์ง„ํ–‰ํ•  recursiveํ•จ์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ cnt ๋กœ ์ฃผ๊ณ , ์ฒซ ์‹คํ–‰์‹œ์—๋Š” 0์„ ์ฃผ๊ณ  ์‹คํ–‰์‹œํ‚จ๋‹ค.
    1. N์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘๊ธฐ ๋•Œ๋ฌธ์— deep copy๋ฅผ ์ด์šฉํ•œ ๋ฐฐ์—ด๋ณต์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
      ์ด๋ฅผ ํ†ตํ•ด ๋ฐฉํ–ฅ์„ ๋ฐ”๊พธ๊ธฐ ์ „, ์ด์ „ ๋ณด๋“œ์˜ ์ƒํƒœ๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•œ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
      ์›€์ง์ด๊ธฐ ์ „ ๋ณด๋“œ์˜ ์ƒํ™ฉ์„ ๊ธฐ์–ตํ•ด๋‘์ง€ ์•Š์œผ๋ฉด, ๋ฐฑํŠธ๋ž˜ํ‚น์ด ์ˆ˜ํ–‰๋  ์ˆ˜ ์—†๋‹ค.
      b = [x[:] for x in arr]
    1. ๋„ค ๋ฐฉํ–ฅ์œผ๋กœ ๋ธ”๋ก์„ ์›€์ง์ผ ์ˆ˜ ์žˆ์œผ๋‹ˆ, ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋„ค ๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์—ฌ์ค€๋‹ค.
      move() ํ•จ์ˆ˜์—์„œ k ์˜ ๊ฐ’์— ๋”ฐ๋ผ ๋„ค ๋ฐฉํ–ฅ์œผ๋กœ ์›€์ง์—ฌ ์ค€๋‹ค.
      def move(k):
       # arr[i][j]
       if k == 0:  # ์œ„๋กœ ์ด๋™, ๋ธ”๋ฝ๋“ค์ด ์œ„๋กœ ๋ชจ๋‘ ์ด๋™ํ•˜๋ฉด row index๋Š” 0
           for j in range(n):
               for i in range(n):
                   get(i, j)
               merge(0, j, 1, 0)  # row index 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ์•„๋ž˜์ชฝ ๋ธ”๋ฝ๋“ค์„ ํ•ฉ์ณ๊ฐ
       elif k == 1:  # ์•„๋ž˜๋กœ ์ด๋™, ๋ธ”๋ฝ๋“ค์ด ์•„๋ž˜๋กœ ๋ชจ๋‘ ์ด๋™ํ•˜๋ฉด row index๋Š” n-1
           for j in range(n):
               for i in range(n - 1, -1, -1):
                   get(i, j)
               merge(n - 1, j, -1, 0)  # row ์ธ๋ฑ์Šค 1์”ฉ ๊ฐ์†Œํ•˜๋ฉด์„œ ์œ„์ชฝ๋“ค์„ ํ•ฉ์ณ๊ฐ
       elif k == 2:  # ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™, column index๋Š” 0
           for i in range(n):
               for j in range(n):
                   get(i, j)
               merge(i, 0, 0, 1)  # column ์ธ๋ฑ์Šค ์ฆ๊ฐ€ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
       else:  # ์™ผ์ชฝ์œผ๋กœ ์ด๋™, column index๋Š” n-1
           for i in range(n):
               for j in range(n - 1, -1, -1):
                   get(i, j)
               merge(i, n - 1, 0, -1)  # column ์ธ๋ฑ์Šค ๊ฐ์†Œ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
    1. get() ํ•จ์ˆ˜์—์„œ ์›€์ง์ผ ๋ณด๋“œ๋“ค์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๋ฉด์„œ ์ˆœ์ฐจ์ ์œผ๋กœ ์ˆœํšŒํ•ด์ฃผ๊ณ ,
      ์ˆœํšŒํ•˜๋ฉด์„œ ๋ฐฐ์—ด ์›์†Œ์˜ ๊ฐ’์ด 0์ด ์•„๋‹Œ ๊ฒฝ์šฐ, ์›์†Œ์˜ ๊ฐ’์„ ํ์— ๋„ฃ์–ด์ค€๋‹ค.
      ๋ฐฐ์—ด์— ๋„ฃ์€ ๊ฐ’์ด ์žˆ๋˜ ํ•ด๋‹น ์ž๋ฆฌ๋Š” 0์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
      def get(i, j):
       if arr[i][j]:  # 0์ด ์•„๋‹Œ ๊ฐ’์ด๋ผ๋ฉด
           q.append(arr[i][j])  # queue์— arr์˜ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.
           arr[i][j] = 0  # ์ฒ˜๋ฆฌ๊ฐ€ ๋œ ๋นˆ ์ž๋ฆฌ๋Š” 0์œผ๋กœ ๊ฐ’ ์—…๋ฐ์ดํŠธ
    1. merge() ํ•จ์ˆ˜์—์„œ ๊ฐ ์ด๋™ํ•˜๋ ค๋Š” ๋ฐฉํ–ฅ์— ์•Œ๋งž๊ฒŒ ์ธ๋ฑ์Šค๋ฅผ ์กฐ์ ˆํ•˜๋ฉฐ ํ๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉฐ ํ•ฉ์ณ์ค€๋‹ค.
      ์›€์ง์ด๋ ค๋Š” ๊ฐ’์˜ ๋ธ”๋ก์„ ํ์—์„œ ๊บผ๋‚ด์˜จ ํ›„, ๋†“์„ ๊ณณ์˜ ๋ธ”๋Ÿญ์˜ ๊ฐ’์ด 0์ด๋ผ๋ฉด ๊ทธ๋ƒฅ ๋‘๊ณ ,
      ๊ฐ’์ด ์ผ์น˜ํ•œ๋‹ค๋ฉด 2์˜ ์ œ๊ณฑ ๊ผด์ด๊ธฐ์— 2๋ฐฐ๋ฅผ ํ•ด์ค€๋‹ค.
      ๊ฐ’์ด ์ผ์น˜ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ทธ ์ž๋ฆฌ์— ๊ทธ๋Œ€๋กœ ๋‘”๋‹ค.
    def merge(i, j, di, dj):  # row index, column index, y๋ฐฉํ–ฅ, x๋ฐฉํ–ฅ
        while q:
            x = q.popleft()  # ์›€์ง์ด๋ ค๋Š” ๋ธ”๋ก ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค. FIFO
            if not arr[i][j]:  # 0์ด๋ผ๋ฉด ๊ทธ๋Œ€๋กœ ๋†“๋Š”๋‹ค.
                arr[i][j] = x
            elif arr[i][j] == x:  # ๊ฐ’์ด ์ผ์น˜ํ•œ๋‹ค๋ฉด
                arr[i][j] = x * 2  # ํ•ฉ์ณ์ง€๋ฏ€๋กœ 2๋ฐฐ๋กœ ์ฆ๊ฐ€
                i, j = i + di, j + dj
            else:  # ๊ฐ’์ด ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด
                i, j = i + di, j + dj
                arr[i][j] = x
    1. move() ํ•จ์ˆ˜์˜ ์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚ฌ๋‹ค๋ฉด, cnt + 1 ์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ฃผ๊ณ , recursive ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœ ํ•ด์ค€๋‹ค.
    1. 3๋ฒˆ๋ถ€ํ„ฐ ๋‹ค์‹œ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ, cnt ๊ฐ’์ด 5, ์ฆ‰ ๋‹ค์„ฏ๋ฒˆ ์›€์ง์˜€์„ ๋•Œ, ๋ณด๋“œํŒ์„ ์ˆœํšŒํ•œ๋‹ค.
      ๊ฐ ๋ฐฐ์—ด์˜ ์ตœ๋Œ“๊ฐ’๊ณผ ์ •๋‹ต์œผ๋กœ ์ถœ๋ ฅํ•  ans ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ๊ฐฑ์‹ ํ•œ๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์–ผ๋งˆ๋‚˜ ์‘์šฉ์„ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ๋ฌธ์ œ์˜€๋‹ค.
    2. get(), move() ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์–ด๋ ค์› ์œผ๋ฉฐ, ์ด ๋ถ€๋ถ„์€ ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ์—์„œ ์•„์ด๋””์–ด๋ฅผ ์–ป์–ด ํ•ด๊ฒฐํ–ˆ๋‹ค.
    3. ๋ช‡ ๋ฒˆ์ด๊ณ  ๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ ์ข‹์€ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋‹ค. ์ฃผ๋ง์— ๋‹ค์‹œ ํ•œ ๋ฒˆ ํ’€์–ด๋ณด๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.