Algorithm Problem/Python

[python] λ°±μ€€ - 1347. 미둜 λ§Œλ“€κΈ°

deo2kim 2021. 9. 14. 22:24
λ°˜μ‘ν˜•

πŸ€”λ¬Έμ œ ν•΄κ²°

  • κ΅¬ν˜„
  1. μ’Œν‘œλ₯Ό λ¨Όμ € κ΅¬ν•˜μž.
  2. (0, 0) μ—μ„œ μ‹œμž‘ν•΄μ„œ μ œμ‹œλœ ν–‰λ™λŒ€λ‘œ μ’Œν‘œλ‘œ λ‹΄μ•„μ€€λ‹€.
  3. xμ’Œν‘œ, yμ’Œν‘œ 각각의 μ΅œμ†Œμ™€ μ΅œλŒ€λ₯Ό κ΅¬ν•œλ‹€.
  4. λͺ¨λ“  μ’Œν‘œλ“€μ— λŒ€ν•΄μ„œ μ΅œμ†Œκ°’μ„ 더해쀀닀. ( μŒμˆ˜κ°€ λ‚˜μ˜¬ 수 있기 λ•Œλ¬Έμ— μ–‘μˆ˜λ‘œ λ°”κΏ”μ£ΌκΈ° μœ„ν•΄μ„œ )
  5. μ΅œλŒ€ μ΅œμ†Œλ‘œ 2차원 배열을 λ§Œλ“€κ³ , κ΅¬ν•œ μ’Œν‘œλ‘œ 길을 λ§Œλ“€μ–΄μ€€λ‹€.

 

 

πŸ’»μ†ŒμŠ€ μ½”λ“œ

import sys

input = sys.stdin.readline

N = int(input())
action = input().strip()

# 0,0 λΆ€ν„° μ’Œν‘œ κ΅¬ν•˜κΈ°
loc_list = [(0, 0)] 
dx, dy = [-1, 0, 1, 0], [0, 1, 0, -1]  # 상 우 ν•˜ 쒌/ 뢁 동 남 μ„œ
status = 2
for a in action:
    if a == 'R':
        # 였λ₯Έμͺ½
        status = (status + 1) % 4
    elif a == 'L':
        # μ™Όμͺ½
        status = (status - 1) if status != 0 else 3
    else:
        x = loc_list[-1][0] + dx[status]
        y = loc_list[-1][1] + dy[status]
        loc_list.append((x, y))
        
# x, y 의 μ΅œλŒ€ μ΅œμ†Œ κ΅¬ν•˜κΈ°
x_sort = sorted(loc_list, key=lambda x: x[0])
y_sort = sorted(loc_list, key=lambda x: x[1])
x_min, x_max = x_sort[0][0], x_sort[-1][0]
y_min, y_max = y_sort[0][1], y_sort[-1][1]

# 2차원 λ°°μ—΄ λ§Œλ“€κΈ°
maze = [['#' for y in range(y_min, y_max + 1)] for x in range(x_min, x_max + 1)]

# μŒμˆ˜κ°€ λ‚˜μ˜¬ 수 μžˆμœΌλ―€λ‘œ μ΅œμ†Œκ°’λ§ŒνΌ 더해주기 ( μ–‘μˆ˜λ‘œ λ°”κΎΈκΈ° )
for i in range(len(loc_list)):
    loc_list[i] = (loc_list[i][0] - x_min, loc_list[i][1] - y_min)

# μ’Œν‘œλ‘œ κΈΈ λ§Œλ“€κΈ°
for i, j in loc_list:
    maze[i][j] = '.'

# λ‹΅
for row in maze:
    print(''.join(row))

 

πŸ“•λ¬Έμ œ 확인

좜처: BACKJOON ONLINE JUDGE

λ°˜μ‘ν˜•