12100-2048

www.acmicpc.net/problem/12100

  1. ๋งค ์‹œ๋„๋งˆ๋‹ค 4 ๋ฐฉํ–ฅ์„ ๋ชจ๋‘ DFS ๋กœ ์žฌ๊ท€๋ฅผ ํƒ€์ค˜์•ผ ํ•œ๋‹ค.

  2. ์ƒ, ํ•˜, ์ขŒ, ์šฐ ๋กœ ๋ชฐ์•„์ฃผ๋Š” ๋งค์—ด ์ˆ˜์ •์„ ๊ตฌํ˜„ํ•œ๋‹ค.

  3. 5๋ฒˆ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚˜๋ฉด ๋งค๋ฒˆ ๋ฐฐ์—ด ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ตœ๋Œ“๊ฐ’ ์—…๋ฐ์ดํŠธ ํ•ด์ค€๋‹ค.

n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]

# ์šฐ, ์ขŒ, ์ƒ, ํ•˜
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]

''''''
def dfs(time, a, dir):
    global max_ans, n
    if time >= 5:
        for i in range(n):
            max_ans = max(max_ans, max(a[i]))
        return

    na = [[0 for _ in range(n)] for _ in range(n)]
    # dir ์— ๋”ฐ๋ผ ์ด๋™์ˆ˜ํ–‰ํ•˜์—ฌ a ๊ธฐ์ค€์œผ๋กœ na ๋ฅผ ํ• ๋‹นํ•˜์ž
    '''
    a ๋ฅผ ๋ณด๊ณ , dir ์— ๋”ฐ๋ผ์„œ ์Šคํƒ์„ ์Œ“์•„๊ฐ€์ž
    dir ์ด ์ƒ, ํ•˜ ์ธ ๊ฒƒ๊ณผ ์ขŒ, ์šฐ ์ธ๊ฑธ ๋‘๊ฐœ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ฒ˜๋ฆฌํ•˜์ž

    '''
    if dir == 0:
        for i in range(len(a)):
            stack = []
            rst = []
            for j in range(len(a)):
                if a[i][n - 1 - j]:
                    stack.append(a[i][n - 1 - j])
                # ๋‘๋ฒˆ์งธ ๋ถ€ํ„ฐ๋Š”
                if len(stack) >= 2:
                    # ์ƒˆ๋กœ๋“ค์–ด์˜จ๊ฒŒ ๊ฐ™์œผ๋ฉด
                    if stack[-1] == stack[-2]:
                        # ๊ฐ™์€๊ฑฐ 2๋ฐฐํ•ด์ค€๊ฑฐ ๋”ฐ๋กœ ์ €์žฅํ•ด์ฃผ๊ณ 
                        together = stack[-1] * 2
                        # ์Šคํƒ์—์„œ ๋’ค์— ๋‘๊ฐœ ๋นผ์ฃผ๊ณ 
                        stack.pop()
                        stack.pop()
                        # 2๋ฐฐํ•ด์ค€๊ฑฐ ์Šคํƒ์— ๋”ํ•ด์ฃผ๊ณ 
                        stack.append(together)
                        # ๋ชฝ๋•… rst ์— ์ถ”๊ฐ€
                        rst.extend(stack[:])
                        stack = []
            # ๋ฐฐ์น˜ํ•  rst ๋ฅผ ์™„๋ฒฝํžˆ ๋งŒ๋“ค์—ˆ๋‹น
            rst.extend(stack[:])
            loc = 0
            for v in rst:
                na[i][n - loc - 1] = v
                loc += 1

    elif dir == 1:
        for i in range(len(a)):
            stack = []
            rst = []
            for j in range(len(a)):
                if a[i][j]:
                    stack.append(a[i][j])
                # ๋‘๋ฒˆ์งธ ๋ถ€ํ„ฐ๋Š”
                if len(stack) >= 2:
                    # ์ƒˆ๋กœ๋“ค์–ด์˜จ๊ฒŒ ๊ฐ™์œผ๋ฉด
                    if stack[-1] == stack[-2]:
                        # ๊ฐ™์€๊ฑฐ 2๋ฐฐํ•ด์ค€๊ฑฐ ๋”ฐ๋กœ ์ €์žฅํ•ด์ฃผ๊ณ 
                        together = stack[-1] * 2
                        # ์Šคํƒ์—์„œ ๋’ค์— ๋‘๊ฐœ ๋นผ์ฃผ๊ณ 
                        stack.pop()
                        stack.pop()
                        # 2๋ฐฐํ•ด์ค€๊ฑฐ ์Šคํƒ์— ๋”ํ•ด์ฃผ๊ณ 
                        stack.append(together)
                        # ๋ชฝ๋•… rst ์— ์ถ”๊ฐ€
                        rst.extend(stack[:])
                        stack = []
            # ๋ฐฐ์น˜ํ•  rst ๋ฅผ ์™„๋ฒฝํžˆ ๋งŒ๋“ค์—ˆ๋‹น
            rst.extend(stack[:])
            loc = 0
            for v in rst:
                na[i][loc] = v
                loc += 1

    elif dir == 2:
        for i in range(len(a)):
            stack = []
            rst = []
            for j in range(len(a)):
                if a[j][i]:
                    stack.append(a[j][i])
                # ๋‘๋ฒˆ์งธ ๋ถ€ํ„ฐ๋Š”
                if len(stack) >= 2:
                    # ์ƒˆ๋กœ๋“ค์–ด์˜จ๊ฒŒ ๊ฐ™์œผ๋ฉด
                    if stack[-1] == stack[-2]:
                        # ๊ฐ™์€๊ฑฐ 2๋ฐฐํ•ด์ค€๊ฑฐ ๋”ฐ๋กœ ์ €์žฅํ•ด์ฃผ๊ณ 
                        together = stack[-1] * 2
                        # ์Šคํƒ์—์„œ ๋’ค์— ๋‘๊ฐœ ๋นผ์ฃผ๊ณ 
                        stack.pop()
                        stack.pop()
                        # 2๋ฐฐํ•ด์ค€๊ฑฐ ์Šคํƒ์— ๋”ํ•ด์ฃผ๊ณ 
                        stack.append(together)
                        # ๋ชฝ๋•… rst ์— ์ถ”๊ฐ€
                        rst.extend(stack[:])
                        stack = []
            # ๋ฐฐ์น˜ํ•  rst ๋ฅผ ์™„๋ฒฝํžˆ ๋งŒ๋“ค์—ˆ๋‹น
            rst.extend(stack[:])
            loc = 0
            for v in rst:
                na[loc][i] = v
                loc += 1

    elif dir == 3:
        for i in range(len(a)):
            stack = []
            rst = []
            for j in range(len(a)):
                if a[n - j - 1][i]:
                    stack.append(a[n - j - 1][i])
                # ๋‘๋ฒˆ์งธ ๋ถ€ํ„ฐ๋Š”
                if len(stack) >= 2:
                    # ์ƒˆ๋กœ๋“ค์–ด์˜จ๊ฒŒ ๊ฐ™์œผ๋ฉด
                    if stack[-1] == stack[-2]:
                        # ๊ฐ™์€๊ฑฐ 2๋ฐฐํ•ด์ค€๊ฑฐ ๋”ฐ๋กœ ์ €์žฅํ•ด์ฃผ๊ณ 
                        together = stack[-1] * 2
                        # ์Šคํƒ์—์„œ ๋’ค์— ๋‘๊ฐœ ๋นผ์ฃผ๊ณ 
                        stack.pop()
                        stack.pop()
                        # 2๋ฐฐํ•ด์ค€๊ฑฐ ์Šคํƒ์— ๋”ํ•ด์ฃผ๊ณ 
                        stack.append(together)
                        # ๋ชฝ๋•… rst ์— ์ถ”๊ฐ€
                        rst.extend(stack[:])
                        stack = []
            # ๋ฐฐ์น˜ํ•  rst ๋ฅผ ์™„๋ฒฝํžˆ ๋งŒ๋“ค์—ˆ๋‹น
            rst.extend(stack[:])
            loc = 0
            for v in rst:
                na[n - loc - 1][i] = v
                loc += 1

    time += 1
    for i in range(4):
        dfs(time, na, i)
''''''

time = 0
max_ans = 2
for i in range(4):
    dfs(time, a, i)

print(max_ans)

Last updated