Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[python3] ์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค - ์ƒํ•˜์ขŒ์šฐ (ch4. ๊ตฌํ˜„)

๊ฐ์ž ๐Ÿฅ” 2021. 6. 18. 16:36
๋ฐ˜์‘ํ˜•

๋‚ด๋ˆ๋‚ด์‚ฐ

์ฐธ๊ณ ) ์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹ค with ํŒŒ์ด์ฌ ์ฑ…์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์„ฑ๋œ ๋ฌธ์ œ์™€ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. 
๋”ฐ๋ผ์„œ ๋ฌธ์ œ๋Š” ์ž์„ธํ•˜๊ฒŒ ์ ์ง€ ์•Š๊ณ , ๊ฐ„๋‹จํ•œ ์„ค๋ช…๊ณผ ์ œ ์ฝ”๋“œ๋งŒ ์˜ฌ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

http://www.yes24.com/Product/Goods/91433923

 

์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค with ํŒŒ์ด์ฌ - YES24

๋‚˜๋™๋นˆ ์ €์ž์˜ ์œ ํŠœ๋ธŒ ๋ผ์ด๋ธŒ ๋ฐฉ์†ก https://www.youtube.com/c/dongbinnaIT ์ทจ์ค€์ƒ์ด๋ผ๋ฉด ๋ˆ„๊ตฌ๋‚˜ ์ž…์‚ฌํ•˜๊ณ  ์‹ถ์€ ์นด์นด์˜ค · ์‚ผ์„ฑ์ „์ž · ๋„ค์ด๋ฒ„ · ๋ผ์ธ!์ทจ์—…์˜ ์„ฑ๊ณต ์—ด์‡ ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ธํ„ฐ๋ทฐ์— ์žˆ๋‹ค!IT ์ทจ์ค€์ƒ

www.yes24.com

 

์˜ˆ์ œ 4-1) ์ƒํ•˜์ขŒ์šฐ

  • ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ์˜ ๋Œ€ํ‘œ์ ์ธ ํ˜•ํƒœ
  • ๋Œ€์ฒด๋กœ ์ฝ”ํ…Œ์˜ 1~2๋ฒˆ ๋ฌธ์ œ๋Š” ๊ทธ๋ฆฌ๋””๋‚˜ ์ด๋Ÿฐ ๊ตฌํ˜„ ๋ฌธ์ œ๋ผ๊ณ  ํ•œ๋‹ค. 

N x N ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ๊ณต๊ฐ„์ด ์žˆ๋‹ค. 
์ด๋™ ๊ณ„ํš์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ( L / R / U / D )

  • L : ์™ผ์ชฝ์œผ๋กœ ํ•œ ์นธ ์ด๋™
  • R : ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ์นธ ์ด๋™
  • U : ์œ„๋กœ ํ•œ ์นธ ์ด๋™
  • D : ์•„๋ž˜๋กœ ํ•œ ์นธ ์ด๋™

์ฒ˜์Œ์€ (1,1) ์ž๋ฆฌ์—์„œ ์‹œ์ž‘ํ•˜๊ณ , ์ด๋™ ๊ณ„ํš์— ๋”ฐ๋ผ ์ด๋™ํ•œ ํ›„์˜ ์œ„์น˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ 
๋‹จ, ์ขŒํ‘œ๋ฅผ ๋ฒ—์–ด๋‚˜๊ฒŒ ๋˜๋Š” ์ด๋™๊ณ„ํš์€ ๋ฌด์‹œ๋œ๋‹ค.

์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์ž…๋ ฅ์˜ˆ์‹œ
5
R R R U D D

์ถœ๋ ฅ์˜ˆ์‹œ
3 4

<๋ฌธ์ œ ํ’€์ด>

๋‚˜๋Š” ์šฐ์„  ์ฒซ ์œ„์น˜ (1,1)์„ place๋กœ ์ง€์ •ํ–ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ํ•จ์ˆ˜ ์†์— LRUD๋ผ๋Š” ์ด๋™ ๊ณ„ํš์„ ๋‹ด์€ ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ ์ง„ํ–‰ํ–ˆ๋‹ค.

<๋‚ด๊ฐ€ ํ‘ผ ๋ฌธ์ œ>

def solution(N, route):
    place = [1,1]
    
    def LRUD(a, place):
        temp_place = list(place)
        if a == 'R':
            temp_place[1] += 1
        elif a == 'L':
            temp_place[1] -= 1
        elif a == 'U':
            temp_place[0] -= 1
        elif a == 'D':
            temp_place[0] += 1

        if (temp_place[0] < 1) or (temp_place[1] < 1) or (temp_place[1] > N) or (temp_place[0] > N) :
            return place #์œ„์น˜๊ฐ€ ๋ฒ—์–ด๋‚˜๋ฉด ๊ฐ€์ง€์•Š๊ณ  ๋‹ค์‹œ place๋กœ ๋ฐ˜ํ™˜
        else: # ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค๋ฉด
            place = list(temp_place) # ๋ณ€ํ•œ๊ฑธ๋กœ ์—…๋ฐ์ดํŠธ ์ณ์ฃผ๊ณ 
            return place
    
    for i in range(0, len(route)):
        a = route.pop(0)
        place = LRUD(a, place)
    return place

์—ฌ๊ธฐ์„œ ์กฐ๊ธˆ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋˜๊ฒŒ, list๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๊ณผ์ •์—์„œ ์ž๊พธ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•ด์„œ ์ธํ„ฐ๋„ท ๊ฒ€์ƒ‰์„ ํ–ˆ๋‹ค. ใ…‹ใ…‹....
๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณต์‚ฌํ• ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์‚ฌํ•ญ์„ ์ฃผ์˜ํ•ด์„œ ๋ณต์‚ฌํ•ด์•ผ ํ•จ์„ ์•Œ์•˜๋‹ค!!

์—ญ์‹œ ๋‚  ๊ดด๋กญํžˆ๋Š”๊ฑด ๊ธฐ๋ณธ ๋ฌธ๋ฒ•์ด์•ผ... ์—ญ์‹œ,,, ์–ธ์ œ๋‚˜ ๊ธฐ์ดˆ๊ฐ€ ์ œ์ผ ์ค‘์š”ํ•ดใ…œใ…œ 

temp_list = list
# ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, ๋ฆฌ์ŠคํŠธ ์ž์ฒด์˜ ๋™์ผํ•œ ๊ฐ์ฒด์— ์ด๋ฆ„์ด ๋‘๊ฐœ ์ƒ๊ธด๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค. 
# ๋”ฐ๋ผ์„œ list๊ฐ€ ๋ณ€ํ•ด๋„ ๋™์‹œ์— temp_list๊ฐ€ ๋ณ€ํ•œ๋‹ค. 

#๊ทธ๋ž˜์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณต์‚ฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, 
# ์•„๋ž˜ ์ฒ˜๋Ÿผ ๋‹ค์‹œ list ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กญ๊ฒŒ ๋ถˆ๋Ÿฌ์™€์•ผํ•œ๋‹ค. 
temp_list = list(list)

 

๋‹ต์•ˆ ์ถœ๋ ฅ)

N = 5
route = ['R','R','R','U','D','D']
solution(N, route)

 

 

<์ฑ…์— ์žˆ๋Š” ์ •๋‹ต๊ณผ ๋น„๊ต>

n = 5
x, y = 1, 1
plans = ['R','R','R','U','D','D']

# LRUD ์— ๋Œ€ํ•œ ์ด๋™ ๋ฐฉํ–ฅ์„ 4์ฐจ์›์œผ๋กœ ํ‘œํ˜„
# ์ธ๋ฑ์Šค 0์ด L, 1์ด R, 2๊ฐ€ U ... 
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 # ๋”์ด์ƒ ํ•ด๋‹น ์•ˆ์˜ for ๋ฌธ์—์„œ ๋Œ์ง€ ์•Š๊ณ  ๋‚˜๊ฐ
    x, y = nx, ny

print(x,y)

์ฑ…์€ ์ด๋ ‡๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋†จ๋‹ค. ๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ๋˜ ๋ฐฉ๋ฒ•์œผ๋กœ
L R U D๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ž…๋ ฅ๋ฐ›์ง€ ์•Š๊ณ , dx, dy, move_types ์˜ ์ธ๋ฑ์Šค๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ(?)์„ ๋ฐฐ์› ๋‹ค.

์•„๋ฌด๋ž˜๋„ ์ฑ…์— ๋‚˜์˜จ ์ฝ”๋“œ๊ฐ€ ์ข€๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ์งง๊ธฐ๋•Œ๋ฌธ์— ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ๋œ ๊ฑธ๋ฆด ๊ฒƒ ๊ฐ™๋‹ค.  

๋ฐ˜์‘ํ˜•