๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โญ Problem_Solving/๋ฐฑ์ค€

[๋ฐฑ์ค€] 27942 :danceplant: (Python/ํŒŒ์ด์ฌ)

by ํฌ์ŠคํŠธ์‰์ดํฌ 2023. 4. 12.

<๋ฌธ์ œ๋งํฌ>

https://www.acmicpc.net/problem/27942

 

27942๋ฒˆ: :danceplant:

์ฒซ์งธ ์ค„์— ๊ฐ€์ง€๊ฐ€ ๋ชธ์„ ๋Š˜์ด๋ฉฐ ๋จน์€ ์–‘๋ถ„์˜ ์ด๋Ÿ‰์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋‘˜์งธ ์ค„์— ๊ฐ€์ง€๊ฐ€ ๋ชธ์„ ๋Š˜์ธ ๋ฐฉํ–ฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž๋ฅผ ๋Š˜์ธ ์ˆœ์„œ๋Œ€๋กœ ํ•œ ์ค„์— ์ถœ๋ ฅํ•œ๋‹ค. ์ƒํ•˜์ขŒ์šฐ๋Š” ๊ฐ๊ฐ UDLR์— ๋Œ€์‘๋œ๋‹ค.

www.acmicpc.net

 

<ํ’€์ด ์ „๋žต>

1. ์‹œ๋ฎฌ๋ ˆ์ด์…˜ / ๋ˆ„์ ํ•ฉ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

2. java๋‚˜ c++์€ ๋‹จ์ˆœ ๊ตฌํ˜„์œผ๋กœ๋„ ํ†ต๊ณผ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์€๋ฐ python๊ธฐ์ค€์œผ๋กœ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  pypy๋กœ ํ†ต๊ณผ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

3. ์ €๋Š” ๋‹จ์ˆœ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์œผ๋กœ ํ’€์—ˆ์ง€๋งŒ, ๋ˆ„์ ํ•ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•ด ์ดˆ๊ธฐ์— ์ž…๋ ฅ ๋ฐ›์€ ๋ฐฐ์—ด์„ ๋ˆ„์ ํ•ฉ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ์˜์–‘๋ถ„ ์„ญ์ทจ๋ฅผ ์ผ์ผํžˆ ๋”ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ํ•ด๋‹น ํ–‰ / ์—ด์—์„œ ๊ตฌ๊ฐ„ํ•ฉ๋งŒ ๋”ํ•ด์ฃผ๋ฉด ํŒŒ์ด์ฌ์œผ๋กœ๋„ ์‹œ๊ฐ„ ๋‚ด์— ํ†ต๊ณผ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

<์ •๋‹ต ์ฝ”๋“œ>

import sys, os, io, atexit

input = lambda: sys.stdin.readline().rstrip('\r\n')
stdout = io.BytesIO()
sys.stdout.write = lambda s: stdout.write(s.encode("ascii"))
atexit.register(lambda: os.write(1, stdout.getvalue()))

# 230411 27942 :danceplant:

# ์ •๋‹ต์ฝ”๋“œ

dir_dict = {
    "U": [(-1, 0), (0, 0)],
    "D": [(0, 0), (1, 0)],
    "L": [(0, -1), (0, 0)],
    "R": [(0, 0), (0, 1)]
}



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

points = [[N // 2 - 1, N // 2 - 1], [N // 2, N // 2]]
ans = 0
path = ''

flag = True

while True:

    dir = ""
    total = 0
    
    # ์œ„
    if points[0][0] - 1 >= 0:
        up_total = 0
        for i in range(points[0][1], points[1][1] + 1):
            up_total += arr[points[0][0] - 1][i]

        if up_total > 0:
            total = up_total
            dir = "U"

    # ์•„๋ž˜
    if points[1][0] + 1 < N:
        down_total = 0
        for i in range(points[0][1], points[1][1] + 1):
            down_total += arr[points[1][0] + 1][i]

        if down_total > total:
            total = down_total
            dir = "D"

    # ์™ผ
    if points[0][1] - 1 >= 0:
        left_total = 0
        for i in range(points[0][0], points[1][0] + 1):
            left_total += arr[i][points[0][1] - 1]

        if left_total > total:
            total = left_total
            dir = "L"

    # ์˜ค
    if points[1][1] + 1 < N:
        right_total = 0
        for i in range(points[0][0], points[1][0] + 1):
            right_total += arr[i][points[1][1] + 1]

        if right_total > total:
            total = right_total
            dir = "R"   

    if total == 0:
        break
    
    ans += total
    path += dir
    for i in range(2):
        for j in range(2):
            points[i][j] += dir_dict[dir][i][j]


print(ans)
print(path)

๋Œ“๊ธ€