https://school.programmers.co.kr/learn/courses/30/lessons/172928

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์ง€๋‚˜๋‹ค๋‹ˆ๋Š” ๊ธธ์„ 'O', ์žฅ์• ๋ฌผ์„ 'X'๋กœ ๋‚˜ํƒ€๋‚ธ ์ง์‚ฌ๊ฐํ˜• ๊ฒฉ์ž ๋ชจ์–‘์˜ ๊ณต์›์—์„œ ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ์‚ฐ์ฑ…์„ ํ•˜๋ คํ•ฉ๋‹ˆ๋‹ค. ์‚ฐ์ฑ…์€ ๋กœ๋ด‡ ๊ฐ•์•„์ง€์— ๋ฏธ๋ฆฌ ์ž…๋ ฅ๋œ ๋ช…๋ น์— ๋”ฐ๋ผ ์ง„ํ–‰ํ•˜๋ฉฐ, ๋ช…๋ น์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

["๋ฐฉํ–ฅ ๊ฑฐ๋ฆฌ", "๋ฐฉํ–ฅ ๊ฑฐ๋ฆฌ" … ]
์˜ˆ๋ฅผ ๋“ค์–ด "E 5"๋Š” ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ํ˜„์žฌ ์œ„์น˜์—์„œ ๋™์ชฝ์œผ๋กœ 5์นธ ์ด๋™ํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๋กœ๋ด‡ ๊ฐ•์•„์ง€๋Š” ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— ๋‹ค์Œ ๋‘ ๊ฐ€์ง€๋ฅผ ๋จผ์ € ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์–ด์ง„ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ๋•Œ ๊ณต์›์„ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
์ฃผ์–ด์ง„ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ ์ค‘ ์žฅ์• ๋ฌผ์„ ๋งŒ๋‚˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
์œ„ ๋‘ ๊ฐ€์ง€์ค‘ ์–ด๋Š ํ•˜๋‚˜๋ผ๋„ ํ•ด๋‹น๋œ๋‹ค๋ฉด, ๋กœ๋ด‡ ๊ฐ•์•„์ง€๋Š” ํ•ด๋‹น ๋ช…๋ น์„ ๋ฌด์‹œํ•˜๊ณ  ๋‹ค์Œ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
๊ณต์›์˜ ๊ฐ€๋กœ ๊ธธ์ด๊ฐ€ W, ์„ธ๋กœ ๊ธธ์ด๊ฐ€ H๋ผ๊ณ  ํ•  ๋•Œ, ๊ณต์›์˜ ์ขŒ์ธก ์ƒ๋‹จ์˜ ์ขŒํ‘œ๋Š” (0, 0), ์šฐ์ธก ํ•˜๋‹จ์˜ ์ขŒํ‘œ๋Š” (H - 1, W - 1) ์ž…๋‹ˆ๋‹ค.

๊ณต์›์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด park, ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ์ˆ˜ํ–‰ํ•  ๋ช…๋ น์ด ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด routes๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ๋ชจ๋“  ๋ช…๋ น์„ ์ˆ˜ํ–‰ ํ›„ ๋†“์ธ ์œ„์น˜๋ฅผ [์„ธ๋กœ ๋ฐฉํ–ฅ ์ขŒํ‘œ, ๊ฐ€๋กœ ๋ฐฉํ–ฅ ์ขŒํ‘œ] ์ˆœ์œผ๋กœ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 3 ≤ park์˜ ๊ธธ์ด ≤ 50
    • 3 ≤ park[i]์˜ ๊ธธ์ด ≤ 50
      • park[i]๋Š” ๋‹ค์Œ ๋ฌธ์ž๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ ์‹œ์ž‘์ง€์ ์€ ํ•˜๋‚˜๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
        • S : ์‹œ์ž‘ ์ง€์ 
        • O : ์ด๋™ ๊ฐ€๋Šฅํ•œ ํ†ต๋กœ
        • X : ์žฅ์• ๋ฌผ
    • park๋Š” ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ž…๋‹ˆ๋‹ค.
  • 1 ≤ routes์˜ ๊ธธ์ด ≤ 50
    • routes์˜ ๊ฐ ์›์†Œ๋Š” ๋กœ๋ด‡ ๊ฐ•์•„์ง€๊ฐ€ ์ˆ˜ํ–‰ํ•  ๋ช…๋ น์–ด๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • ๋กœ๋ด‡ ๊ฐ•์•„์ง€๋Š” routes์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    • routes์˜ ์›์†Œ๋Š” "op n"๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, op๋Š” ์ด๋™ํ•  ๋ฐฉํ–ฅ, n์€ ์ด๋™ํ•  ์นธ์˜ ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
      • op๋Š” ๋‹ค์Œ ๋„ค ๊ฐ€์ง€์ค‘ ํ•˜๋‚˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
        • N : ๋ถ์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
        • S : ๋‚จ์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
        • W : ์„œ์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
        • E : ๋™์ชฝ์œผ๋กœ ์ฃผ์–ด์ง„ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
      • 1 ≤ n ≤ 9

 

์ž…์ถœ๋ ฅ ์˜ˆ

park routes result
["SOO","OOO","OOO"] ["E 2","S 2","W 1"] [2,1]
["SOO","OXX","OOO"] ["E 2","S 2","W 1"] [0,1]
["OSO","OOO","OXO","OOO"] ["E 2","S 3","W 1"] [0,0]

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(park, routes):
    # x = ์—ด ๋ฒˆํ˜ธ / y = ํ–‰ ๋ฒˆํ˜ธ
    # ํ–‰์—ด์˜ ์ตœ๋Œ€๊ฐ’ ์„ค์ •
    x_max = len(park[0])
    y_max = len(park)
    
    # ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
    x = 0
    y = 0
    for i in range(y_max):          # i = ํ–‰ ๋ฒˆํ˜ธ = y๊ฐ’
        for j in range(x_max):      # j = ์—ด ๋ฒˆํ˜ธ = x๊ฐ’
            if park[i][j] == 'S':
                x = j
                y = i
                break
    
    # ์ขŒํ‘œ ์ด๋™
    for route in routes:
        # ๋ฐฉํ–ฅ๊ณผ ์ด๋™๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅ
        direction, distance = route.split(" ")

        # ํ˜„์žฌ์˜ ์ขŒํ‘œ๋ฅผ ์ž„์‹œ๋กœ ์ด๋™
        xx = x
        yy = y

        # ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜๋ฉด์„œ ๊ณต์›์„ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜ ์žฅ์• ๋ฌผ์„ ๋งŒ๋‚˜๋Š”์ง€ ํ™•์ธ
        # step์ด ์ด๋™๊ฑฐ๋ฆฌ์™€ ๊ฐ™์•„์ง€๋ฉด ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ์ž„์‹œ ์ขŒํ‘œ๋กœ ๋ณ€๊ฒฝ
        for step in range(1, int(distance)+1):
            # ๋™์ชฝ์œผ๋กœ ์ด๋™
            if direction == 'E' and xx != x_max-1 and park[yy][xx+1] != 'X':
                xx += 1
                if step == int(distance):
                    x = xx
            # ์„œ์ชฝ์œผ๋กœ ์ด๋™
            elif direction == 'W' and xx != 0 and park[yy][xx-1] != 'X':
                xx -= 1
                if step == int(distance):
                    x = xx
            # ๋‚จ์ชฝ์œผ๋กœ ์ด๋™
            elif direction == 'S' and yy != y_max-1 and park[yy+1][xx] != 'X':
                yy += 1
                if step == int(distance):
                    y = yy
            # ๋ถ์ชฝ์œผ๋กœ ์ด๋™
            elif direction == 'N' and yy != 0 and park[yy-1][xx] != 'X':
                yy -= 1
                if step == int(distance):
                    y = yy

    return [y, x]

 

ํ’€์ด # 1

  • ์ขŒํ‘œ์—์„œ ๊ณ ๋ ค์‚ฌํ•ญ
    • x = ์—ด ๋ฒˆํ˜ธ / y = ํ–‰ ๋ฒˆํ˜ธ
    • x_max = ์—ด์˜ ๊ธธ์ด / y_max = ํ–‰์˜ ๊ธธ์ด
  • ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
    • i = ํ–‰ ๋ฒˆํ˜ธ = y๊ฐ’ / j = ์—ด ๋ฒˆํ˜ธ / x๊ฐ’
    • ์œ„์˜ ๋‚ด์šฉ์„ ๊ณ ๋ คํ•˜์—ฌ ์‹œ์ž‘์  S ์ฐพ๊ธฐ
  • ์ขŒํ‘œ ์ด๋™
    • ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•ž์˜ ๊ฐ’์€ ๋ฐฉํ–ฅ(direction), ๋’ค์˜ ๊ฐ’์€ ์ด๋™๊ฑฐ๋ฆฌ(distance)
    • ํ˜„์žฌ ์ขŒํ‘œ์™€ ๋™์ผํ•œ ์ž„์‹œ ์ขŒํ‘œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ด๋™ (์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”)
      • ๋ฐฉํ–ฅ ํ™•์ธ
        • E = ๋™์ชฝ : x์ขŒํ‘œ + 1
        • W = ์„œ์ชฝ : x์ขŒํ‘œ - 1
        • S = ๋‚จ์ชฝ : y์ขŒํ‘œ + 1
        • N = ๋ถ์ชฝ : y์ขŒํ‘œ -1
      • ํ˜„์žฌ ์ขŒํ‘œ๊ฐ€ ํ–‰์ด๋‚˜ ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฒ—์–ด๋‚˜๋Š”์ง€ ํ™•์ธ
      • ํ•œ ์นธ ์ด๋™ํ–ˆ์„ ๋•Œ ์žฅ์• ๋ฌผ์ด ์žˆ๋Š”์ง€ ํ™•์ธ
    • ๊ฐ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•œ ๊ฒฝ์šฐ ์ž„์‹œ ์ขŒํ‘œ๋ฅผ ํ•œ ์นธ ์ด๋™
    • step์ด ์ด๋™ ๊ฑฐ๋ฆฌ์— ๋„๋‹ฌํ•˜๋ฉด ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ์ž„์‹œ ์ขŒํ‘œ๋กœ ๋ฐ”๊พธ๊ธฐ
    • x๋Š” ์—ด ๋ฒˆํ˜ธ, y๋Š” ํ–‰ ๋ฒˆํ˜ธ์ด๋ฏ€๋กœ [y,x] ๋ฅผ ๋ฐ˜ํ™˜

 

# 2
def solution(park, routes):
    # ํ–‰์—ด์˜ ์ตœ๋Œ€๊ฐ’ ์„ค์ •
    w, h = len(park[0]), len(park)

    # ๋ฐฉํ–ฅ๋ณ„๋กœ ์ด๋™ํ•ด์•ผํ•˜๋Š” ๊ฑฐ๋ฆฌ๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ์— ์ €์žฅ
    move = {"N":(-1,0), "S":(1,0), "W":(0,-1), "E":(0,1)}
    
    # ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
    x, y = 0, 0
    for i in range(h):
        for j in range(w):
            if park[i][j] == "S":
                x, y = i, j
                break
        
    # ์ขŒํ‘œ ์ด๋™
    for route in routes:
        # ๋ฐฉํ–ฅ๊ณผ ์ด๋™๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅ
        direction, distance = route.split(" ")

        # ํ˜„์žฌ์˜ ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•ด์„œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๋ฉด ํ•ด๋‹น๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
        dx, dy = x, y
        
        for idx in range(int(distance)):
            # ์ด๋™ํ•  ์œ„์น˜
            nx = x + move[direction][0]
            ny = y + move[direction][1]
        
            if 0 <= nx <= h-1 and 0 <= ny <= w-1 and (park[nx][ny]!="X"):
                x, y = nx, ny                    
            else:
                x, y = dx, dy
                break

    return [x,y]

 

ํ’€์ด #2

  • ์œ„์˜ ํ’€์ด์™€ ๋ฐ˜๋Œ€๋กœ x๋ฅผ ํ–‰ ๋ฒˆํ˜ธ, y๋ฅผ ํ–‰ ๋ฒˆํ˜ธ๋กœ ์ƒ๊ฐํ•œ ํ’€์ด
  • ํ‚ค๊ฐ€ ์ด๋™ ๋ฐฉํ–ฅ, ๊ฐ’์ด ์ด๋™ํ•ด์•ผํ•˜๋Š” ๊ฑฐ๋ฆฌ์ธ ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ
    • E = ๋™์ชฝ : y์ขŒํ‘œ + 1
    • W = ์„œ์ชฝ : y์ขŒํ‘œ - 1
    • S = ๋‚จ์ชฝ : x์ขŒํ‘œ + 1
    • N = ๋ถ์ชฝ : x์ขŒํ‘œ -1
  • ์‹œ์ž‘ ์œ„์น˜ ์ฐพ๊ธฐ
  • ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ dx, dy์— ์ €์žฅ (์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œํ‚ค์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”)
  • ์ž„์‹œ ์ขŒํ‘œ nx, ny๋ฅผ ๋งŒ๋“ค์–ด ๋ชจ๋“  ์กฐ๊ฑด์„ ๋งŒ์กฑํ–ˆ์„ ๊ฒฝ์šฐ์— ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ๋ณ€๊ฒฝ

 

 

x์ขŒํ‘œ, y์ขŒํ‘œ ๋‚˜์˜ค๋ฉด ์ผ๋‹จ ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‹œ์ž‘....๋‹ค์‹œ ์ฐจ๊ทผ์ฐจ๊ทผ ์ƒ๊ฐํ•ด๋ณด๊ฒ ๋‹ค๋ฉฐ ์ฝ”๋“œ 12938091๋ฒˆ ์ˆ˜์ •ํ–ˆ๋Š”๋ฐ ๊ฐœ์ธ์ ์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ํ’€์ด๊ฐ€ ์ข€ ๋” ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์› ๋‹ค. ๋‘ ๋ฒˆ์งธ ํ’€์ด๋Š” ์†๋„๊ฐ€ ์กฐ๊ธˆ ๋” ๋น ๋ฅด์ง€๋งŒ ๋จธ๋ฆฌ๊ฐ€ ๋”ฐ๋ผ์ฃผ์ง€ ์•Š์•„์„œ ํž˜๋“ค์—ˆ๋‹ค...ํœด...์–ด์จŒ๋“  ํ’€์—ˆ์œผ๋‹ˆ๊นŒ ์‰ฌ์–ด์•ผ๊ฒ ๋‹ค.๊ณต์› ์‚ฐ์ฑ…์„ ์™œ์ด๋ ‡๊ฒŒ ๋นก์„ธ๊ฒŒ ํ•˜์‹œ๋Š” ๊ฑฐ์—์š”... ๐Ÿƒ๐Ÿป ๐Ÿƒ๐Ÿป

+ Recent posts