๐Ÿ™‚TIL from lecture


Implementation(๊ตฌํ˜„)

๋จธ๋ฆฌ ์†์— ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์†Œ์Šค ์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •

  • ํ•ด๋‹น ์œ ํ˜• ๋ฌธ์ œ
    • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ„๋‹จ, ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๋Š” ๋ฌธ์ œ
    • ์‹ค์ˆ˜ ์—ฐ์‚ฐ, ์†Œ์ˆ˜์  ์ž๋ฆฌ๊นŒ์ง€ ์ปจํŠธ๋กคํ•˜๋Š” ๋ฌธ์ œ
    • ๋ฌธ์ž์—ด์„ ํŠน์ • ๊ธฐ์ค€์— ๋”ฐ๋ผ ๋Š์–ด ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฌธ์ œ
    • ์ ์ ˆํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ฐพ์•„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ

Tools

**# Matrix**
for i in range(5):
	for j in range(5):
		print('(',i,',',j,')', end=' ')
	print()

**# direct vector**
dx = [0, -1, 0, 1]
dy = [1, 0, -1, 0]

x, y # ํ˜„์žฌ ์œ„์น˜

# ์œ„์น˜ ์ด๋™
for i in range(4):
	nx = x + dx[i]
	ny = y + dy[i]
  • ํ•ด๋‹น ์ฝ”๋“œ Matrix Untitled

๐Ÿ†Today Code Test


๐Ÿ› Problem Approach

๋ฌธ์ œ

์—ฌํ–‰๊ฐ€ A๋Š” NxN ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ๊ณต๊ฐ„ ์œ„์— ์„œ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ณต๊ฐ„์€ 1x1 ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์™ผ์ชฝ ์œ„ ์ขŒํ‘œ๋Š” (1,1)์ด๋ฉฐ, ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ์ขŒํ‘œ๋Š” (N,N)์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

์—ฌํ–‰๊ฐ€ A๋Š” ์ƒ,ํ•˜,์ขŒ,์šฐ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‹œ์ž‘์ขŒํ‘œ๋Š” ํ•ญ์ƒ (1,1) ์ž…๋‹ˆ๋‹ค.

L : ์™ผ์ชฝ์œผ๋กœ ํ•œ ์นธ

R : ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ

U : ์œ„์ชฝ์œผ๋กœ ํ•œ ์นธ

D : ์•„๋ž˜๋กœ ํ•œ ์นธ

์ด ๋•Œ, ์—ฌํ–‰๊ฐ€์˜ ๊ณ„ํš์„œ๋Š” ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ ํ˜€์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. NxN์„ ๋ฒ—์–ด๋‚˜๋Š” ์›€์ง์ž„์€ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค. ์—ฌํ–‰์ž๊ฐ€ ๋„์ฐฉํ•  ์œ„์น˜๋Š” ์–ด๋””์ž…๋‹ˆ๊นŒ?

๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ 128MB

๐Ÿ”‘Solution

์ƒ๊ฐ์—†์ด ์ง  ์ฝ”๋“œ

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

dx = [-1,1,0,0]
dy = [0,0,-1,1]

x, y = 1, 1
for i in plan:
    p_x, p_y = x, y
    if i == 'L':
        x += dx[0]
        y += dy[0]
    elif i == 'R':
        x += dx[1]
        y += dy[1]
    elif i == 'U':
        x += dx[2]
        y += dy[2]
    else:
        x += dx[3]
        y += dy[3]
    if x < 1 or x > n or y < 1 or y > n:
        x, y =p_x, p_y

print(x,y)

๊ฐœ์„  ์ฝ”๋“œ

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

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

x, y = 1, 1

for i in plan:
	for j in range(4):
		if i == set_p[j]:
        		nx = x + dx[j]
			ny = y + dy[j]

	if nx < 1 or nx > n or ny < 1 or ny > n:
		continue
        
	x, y = nx, ny
print(x,y)
profile
Always, Better than.

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