[BOJ 3190] ๋ฑ€ ๐Ÿ

์งฑJยท2023๋…„ 1์›” 2์ผ
0
post-thumbnail

๋ฌธ์ œ ๋งํฌ: https://www.acmicpc.net/problem/3190

๐Ÿ“‹ ๋ฌธ์ œ ์„ค๋ช…

์‚ฌ๊ณผ๊ฐ€ ์—†์„ ๋•Œ๋Š” ์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋จธ๋ฆฌ๋ฅผ ํ•œ ์นธ ์•ž์— ์œ„์น˜ํ•œ ๋’ค, ๊ผฌ๋ฆฌ๊ฐ€ ์žˆ๋˜ ์นธ์„ ๋น„์šด๋‹ค.
๋งŒ์•ฝ ์œ„ ์ƒํ™ฉ์—์„œ (1, 2)์— ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด 1๋ฒˆ๊ณผ 2๋ฒˆ์˜ ๊ณผ์ •๋งŒ ์ˆ˜ํ–‰๋œ๋‹ค. (๊ผฌ๋ฆฌ๊ฐ€ ์žˆ๋˜ ์นธ์„ ๋น„์šฐ์ง€ X)

โœ๏ธ ์˜ˆ์ œ

๐Ÿง ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ๊นŒ?

  • ์ž๋ฃŒ๊ตฌ์กฐ ๋ฑ(deque)๋ฅผ ์‚ฌ์šฉ
    • ํ•œ ์ชฝ์œผ๋กœ ์›์†Œ(์ขŒํ‘œ ๊ฐ’)๋ฅผ ์ถ”๊ฐ€ โ†’ ๋ฑ€์ด ๋ชธ์„ ๋Š˜๋ ค ๋จธ๋ฆฌ๋ฅผ ๋‹ค์Œ ์นธ์— ์œ„์น˜ํ•˜๋Š” ๊ฒƒ์„ ํ‘œํ˜„
    • ๋‹ค๋ฅธ ํ•œ ์ชฝ์—์„œ ์›์†Œ๋ฅผ ์ œ๊ฑฐ โ†’ ๊ผฌ๋ฆฌ๊ฐ€ ์œ„์น˜ํ•œ ์นธ์„ ๋น„์šฐ๋Š” ๊ฒƒ์„ ํ‘œํ˜„
  • ๊ฒŒ์ž„ ์ข…๋ฃŒ ์ƒํ™ฉ ๊ตฌํ˜„
    • ๋ฒฝ์— ๋ถ€๋”ชํž˜ โ†’ ํ˜„์žฌ ์ขŒํ‘œ๊ฐ€ 0๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜, N๋ณด๋‹ค ํผ
    • ์ž๊ธฐ์ž์‹ ์˜ ๋ชธ์— ๋ถ€๋”ชํž˜ โ†’ ๋ฑ์— ๋™์ผํ•œ ์›์†Œ๊ฐ€ ์กด์žฌ
  • ๋ฐฉํ–ฅ ๋ณ€ํ™˜ ๊ตฌํ˜„
    • BFS, DFS์—์„œ ํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ direction ๋ฐฐ์—ด์„ ํ™œ์šฉ

๐Ÿ ์ „์ฒด ์ฝ”๋“œ

import sys
from collections import deque

input = sys.stdin.readline

n = int(input()) # ๋ณด๋“œ์˜ ํฌ๊ธฐ

k = int(input()) # ์‚ฌ๊ณผ์˜ ๊ฐœ์ˆ˜
apples = []
for _ in range(k):
    x, y = map(int, input().split())
    apples.append((x, y)) # ํŠœํ”Œ ํ˜•ํƒœ๋กœ ๋ฐฐ์—ด์— ์ €์žฅ

l = int(input()) # ๋ฐฉํ–ฅ ๋ณ€ํ™˜
move_sec = [] # ๋ช‡ ์ดˆ์— ํ•˜๋Š”์ง€?
move_dir = [] # ๋ฐฉํ–ฅ (L or D)
for _ in range(l):
    s, d = input().split()
    s = int(s)
    move_sec.append(s)
    move_dir.append(d)

d = deque() # ์ž๋ฃŒ๊ตฌ์กฐ ๋ฑ์„ ํ™œ์šฉ
cnt = 0 # ์‹œ๊ฐ„
direction = [(0, 1), (1, 0), (0, -1), (-1, 0)] # ๋ฐฉํ–ฅ

# ์ดˆ๊ธฐ ์ขŒํ‘œ๋ฅผ ๋ฑ์— ๋„ฃ๊ณ  ์‹œ์ž‘, ๋ฐฉํ–ฅ๋„ ์ดˆ๊ธฐํ™”
cur = (1, 1)
dir_idx = 0
dir = direction[dir_idx]
d.append(cur)

while(True):
    cnt += 1
    x, y = cur[0], cur[1]
    dir_x, dir_y = dir[0], dir[1]
    next = (x + dir_x, y + dir_y) # ๋ฑ€์˜ ๋จธ๋ฆฌ๊ฐ€ ์œ„์น˜ํ•  ์ขŒํ‘œ

    # ๋ฒฝ์— ๋ถ€๋”ชํžˆ๊ฑฐ๋‚˜ ์ž์‹ ์˜ ๋ชธ์— ๋ถ€๋”ชํž˜
    if next[0] > n or next[0] < 1 or next[1] > n or next[1] < 1 or next in d:
        break

    # ๊ณ„์† ์ง„ํ–‰
    d.append(next) # ์˜ค๋ฅธ์ชฝ์— ์‚ฝ์ž…, pop์€ ์™ผ์ชฝ์—์„œ ํ•ด์•ผํ•จ
    if next not in apples: # ์ด๋™ํ•œ ์นธ์— ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด
        d.popleft()
    else:
        apples.remove(next)

    cur = next

    # ๋ฐฉํ–ฅ ์ „ํ™˜
    if cnt in move_sec:
        idx = move_sec.index(cnt)
        move = move_dir[idx]
        if move == 'D':
            dir_idx = (dir_idx + 1) % 4
            dir = direction[dir_idx]
        else: # move == 'L'
            dir_idx = (dir_idx - 1) % 4
            dir = direction[dir_idx]

print(cnt)

๐Ÿฅฒ ์™œ ํ‹€๋ ธ๋Š”๊ฐ€?

์ฒ˜์Œ์— ์ด๋™ํ•œ ์นธ์— ์‚ฌ๊ณผ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์‚ฌ๊ณผ๋ฅผ ์—†์• ๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜๋‹ค.

if next not in apples: # ์ด๋™ํ•œ ์นธ์— ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด
    d.popleft()
else:
    apples.remove(next)

์ฒ˜์Œ ํ’€ ๋•Œ๋Š” else ๋ฌธ์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜์—ˆ๋‹ค.
๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๋ฑ€์ด ๋ฐฉํ–ฅ ์ „ํ™˜์„ ํ•˜๋ฉฐ ์‚ฌ๊ณผ๊ฐ€ ์žˆ์—ˆ๋˜ ์ขŒํ‘œ๋ฅผ ์žฌ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๋ชธ์˜ ๊ธธ์ด๊ฐ€ ๋˜ ํ•œ ๋ฒˆ ๋” ๊ธธ์–ด์ง„๋‹ค.

๊ตฟ ^ใ……^ bb

๐Ÿชด ํ”ผ๋“œ๋ฐฑ

  • ๋ฐฉํ–ฅ ์ „ํ™˜ ๊ด€๋ จ ๋ณ€์ˆ˜๋กœ dir_x, dir_y ๋“ฑ์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ๋ณดํŽธ์ ์œผ๋กœ dx, dy๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋ฐฉํ–ฅ ์ „ํ™˜์„ ๊ตฌํ˜„ํ•˜๋ฉด์„œ ์ฒ˜์Œ์—๋Š” ์กฐ๊ฑด๋ฌธ์œผ๋กœ ๋งˆ์ง€๋ง‰ ์›์†Œ์ผ ๋•Œ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋กœ ๊ฐ€๋„๋ก ํ•˜๋“œ์ฝ”๋”ฉ์„ ํ•˜์˜€๋Š”๋ฐ, % ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋” ๊น”๋”ํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
profile
[~2023.04] ๋ธ”๋กœ๊ทธ ์ด์ „ํ–ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž https://leeeeeyeon-dev.tistory.com/

0๊ฐœ์˜ ๋Œ“๊ธ€