Skip to content

TIL – 2024/10/25 Implementation(๊ตฌํ˜„)

  • by

๊ตฌํ˜„์ด๋ž€?

์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŒ๋“ค ๋•Œ, ๋จธ๋ฆฟ์†์— ์žˆ๋Š” ์ ˆ์ฐจ๋ฅผ ์†Œ์Šค์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •์„ ์˜๋ฏธํ•œ๋‹ค. ๊ตฌํ˜„ ์œ ํ˜•์˜ ๋ฌธ์ œ๋Š” ๋ณดํ†ต ํ’€์ด๋ฅผ ๋– ์˜ฌ๋ฆฌ๋Š” ๊ฒƒ์€ ์‰ฝ์ง€๋งŒ, ๊ทธ๊ฑธ ์‹ค์ œ ์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๊ธฐ์—” ์–ด๋ ค์šด ๋ฌธ์ œ๋“ค์„ ๋งŽ์ด ์ถœ์ œํ•œ๋‹ค.

์ฃผ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ตฌํ˜„ ๋ฌธ์ œ๋“ค์€ ์™„์ „ ํƒ์ƒ‰ (๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‹ค ํƒ์ƒ‰ํ•œ๋‹ค) ์ด๊ฑฐ๋‚˜ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ (๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ•œ ๋‹จ๊ณ„์”ฉ ์ฐจ๋ก€๋กœ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋งŒ๋“ฆ) ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

๊ตฌํ˜„ ์˜ˆ์‹œ

N*N ๊ฒฉ์ž ํ˜•์‹์˜ ๊ฒฉ์ž๊ฐ€ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. L, R, U, D๋Š” ๊ฐ๊ฐ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ, ์œ„, ์•„๋ž˜๋กœ ์ด๋™ํ•˜๋ผ๋Š” ๋ช…๋ น์ด ๋‹ด๊ธด ๋ช…๋ น์–ด๋“ค์ด๋‹ค. ๊ทธ๋Ÿผ ๋ช…๋ น์–ด๊ฐ€ ์ฃผ์–ด์กŒ๊ณ , ์ถœ๋ฐœ์ ์€ (1,1)์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด, ๋ช…๋ น ์ˆ˜ํ–‰ ํ›„ ๋‚˜์˜ ์œ„์น˜๋Š” ์–ด๋””์ธ์ง€ ์–ด๋–ป๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ?

n = int(input())
steps = input().split()

x, y = 1, 1
for step in steps:
    if step == "R":
        if y + 1 <= n:
            y += 1
    elif step == "U":
        if x - 1 > 0:
            x -= 1
    elif step == "L":
        if y - 1 > 0:
            y -= 1
    else: # D
        if x + 1 <= n:
            x += 1

print(x, y)

์ด๊ฑธ ์กฐ๊ธˆ ๋” ํ™•์ •์„ฑ ์žˆ๊ฒŒ ๊ตฌํ˜„ํ•œ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

n = int(input())
x, y = 1, 1
plans  = input().split()

dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]
move_types = ['L', 'R', 'U', 'D']

for plan in plans:
    for i in range(len(move_types)):
        if plan == move_types[i]:
            nx = x + dx[i]
            ny = y + dy[i]
            if nx < 1 or ny < 1 or nx > n  or ny > n:
                continue
            x, y = nx, ny

print(x, y)

์ด๋Š” ๊ตฌํ˜„ ์ค‘ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์œ ํ˜•์˜ ๋ฌธ์ œ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ฃผ์–ด์ง€๋Š” ์ง€ ๋ณด์—ฌ์ค€๋‹ค.

Leave a Reply

Your email address will not be published. Required fields are marked *