๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค LV1 ๊ณต์› ์‚ฐ์ฑ…

๐ŸŽฎinspirer9 2023. 6. 23. 00:54
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์ง€๋‚˜๋‹ค๋‹ˆ๋Š” ๊ธธ์„ '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
    • ์ž…๋ ฅ๋œ ๋ช…๋ น๋Œ€๋กœ ๋™์ชฝ์œผ๋กœ 2์นธ, ๋‚จ์ชฝ์œผ๋กœ 2์นธ, ์„œ์ชฝ์œผ๋กœ 1์นธ ์ด๋™ํ•˜๋ฉด [0,0] -> [0,2] -> [2,2] -> [2,1]์ด ๋ฉ๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #2
    • ์ž…๋ ฅ๋œ ๋ช…๋ น๋Œ€๋กœ๋ผ๋ฉด ๋™์ชฝ์œผ๋กœ 2์นธ, ๋‚จ์ชฝ์œผ๋กœ 2์นธ, ์„œ์ชฝ์œผ๋กœ 1์นธ ์ด๋™ํ•ด์•ผํ•˜์ง€๋งŒ ๋‚จ์ชฝ์œผ๋กœ 2์นธ ์ด๋™ํ•  ๋•Œ ์žฅ์• ๋ฌผ์ด ์žˆ๋Š” ์นธ์„ ์ง€๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋ช…๋ น์„ ์ œ์™ธํ•œ ๋ช…๋ น๋“ค๋งŒ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ์ ์œผ๋กœ๋Š” [0,0] -> [0,2] -> [0,1]์ด ๋ฉ๋‹ˆ๋‹ค.
  • ์ž…์ถœ๋ ฅ ์˜ˆ #3
    • ์ฒ˜์Œ ์ž…๋ ฅ๋œ ๋ช…๋ น์€ ๊ณต์›์„ ๋‚˜๊ฐ€๊ฒŒ ๋˜๊ณ  ๋‘ ๋ฒˆ์งธ๋กœ ์ž…๋ ฅ๋œ ๋ช…๋ น ๋˜ํ•œ ์žฅ์• ๋ฌผ์„ ์ง€๋‚˜๊ฐ€๊ฒŒ ๋˜๋ฏ€๋กœ ๋‘ ์ž…๋ ฅ์€ ์ œ์™ธํ•œ ์„ธ ๋ฒˆ์งธ ๋ช…๋ น๋งŒ ๋”ฐ๋ฅด๋ฏ€๋กœ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. [0,1] -> [0,0]

ํ’€์ด

  • ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉํ•˜๊ธฐ ์ข‹์€ ๋ฌธ์ œ๋„ค...
  • ๋ฐฐ์—ด๋กœ ์ง€๋„ ๋งŒ๋“ค์–ด์„œ ํ•œ์นธ์”ฉ ์›€์ง์ด๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋งŒ๋“ค์ž.
def solution(park, routes):
    height = len(park)
    width = len(park[0])
    dic = {"S":(0,1), "N":(0,-1), "W":(-1,0), "E":(1,0)}
    
    for i in range(height):
        for j in range(width):
            if park[i][j] == "S":
                pos = (j, i)
                break
    
    for s in routes:
        d, r = s.split()
        for i in range(int(r)):
            tmp = (pos[0] + dic[d][0], pos[1] + dic[d][1])
            if tmp[0] < 0: break
            if tmp[0] >= width: break
            if tmp[1] < 0: break
            if tmp[1] >= height: break
            if park[tmp[1]][tmp[0]] == "X": break
            pos = tmp
        print(pos)
    return 1
  • ๋กœ ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ. ๋ช…๋ น์„ ๋จผ์ € ์ฒดํฌํ•˜๊ณ  ๋ฌธ์ œ ์—†์œผ๋ฉด ๋‚˜์ค‘์— ์›€์ง์ด๋Š” ๋ฐฉ์‹์ด๋ฏ€๋กœ... ใ…‹ใ…‹ใ…‹
  • ๊ทธ๋ƒฅ ํ•ด๋‹น ๋ฐฉํ–ฅ์œผ๋กœ for๋ฌธ ๋Œ๋ ค์„œ ์ฒดํฌํ•˜๋Š” ๊ฑธ๋กœ ํ•ด์•ผํ•œ๋‹ค.
def solution(park, routes):
    height = len(park)
    width = len(park[0])
    dic = {"S":(0,1), "N":(0,-1), "W":(-1,0), "E":(1,0)}
    
    for i in range(height):
        for j in range(width):
            if park[i][j] == "S":
                pos = (j, i)
                break
    
    for s in routes:
        d, r = s.split()
        r = int(r)
        success = 0
        for i in range(r+1):
            tmp = (pos[0] + dic[d][0]*i, pos[1] + dic[d][1]*i)
            if tmp[0] < 0:
                success = 0
                break
            elif tmp[0] >= width:
                print(tmp[0])
                success = 0
                break
            elif tmp[1] < 0:
                success = 0
                break
            elif tmp[1] >= height:
                success = 0
                break
            elif park[tmp[1]][tmp[0]] == "X":
                success = 0
                break
            else:
                success = 1
        if success == 1:
            pos = pos[0] + dic[d][0]*r, pos[1] + dic[d][1]*r
            
    return [pos[1], pos[0]]
  • ๋ฌด์ง€์„ฑ์œผ๋กœ ๋ฌธ์ œํ•ด๊ฒฐ!

728x90
๋ฐ˜์‘ํ˜•