๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐˜ผ๐™ก๐™œ๐™ค๐™ง๐™ž๐™ฉ๐™๐™ข/๐™‹๐™ง๐™ค๐™œ๐™ง๐™–๐™ข๐™ข๐™š๐™ง๐™จ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค '๊ณต์› ์‚ฐ์ฑ…' ํŒŒ์ด์ฌ ํ’€์ด

by beomcoder 2023. 4. 26.
728x90
๋ฐ˜์‘ํ˜•
https://school.programmers.co.kr/learn/courses/30/lessons/172928
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

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

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

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

def solution(park, routes):
    dog, width, height, block = {'y': 0, 'x': 0}, len(park[0]), len(park), []
            
    for y in range(len(park)):
        for x in range(len(park[0])):
            if park[y][x] == 'X': block.append([y, x])
            elif park[y][x] == 'S': dog = {'y': y, 'x': x}
            
    for route in routes:
        op, n = route.split()
        n = int(n)
        move = {'E': {'y': 0, 'x': n}, 'S': {'y': n, 'x': 0},'W': {'y': 0, 'x': -n}, 'N': {'y': -n, 'x': 0}}
        
        if 0 <= (dog['y'] + move[op]['y']) < height and 0 <= (dog['x'] + move[op]['x']) < width:
            check_block = True
            
            for y, x in block:
                if min(dog['y'], dog['y'] + move[op]['y']) <= y <= max(dog['y'], dog['y'] + move[op]['y']):
                    if min(dog['x'], dog['x'] + move[op]['x']) <= x <= max(dog['x'], dog['x'] + move[op]['x']):
                        check_block = False
                        break
                        
            if check_block:
                dog['y'] += move[op]['y']
                dog['x'] += move[op]['x']
        
    return [dog['y'],dog['x']]
 
 """
 ์ฝ”๋“œ ๋ฆฌ๋ทฐ
 def solution(park, routes):
    dog, width, height, block = {'y': 0, 'x': 0}, len(park[0]), len(park), []
    # ๊ฐœ์˜ ์ขŒํ‘œ๋ฅผ ๋‹ด์„ ๋”•์…”๋„ˆ๋ฆฌ dog, ๊ณต์›์˜ ๊ฐ€๋กœ, ์„ธ๋กœ, block์˜ ์œ„์น˜๋ฅผ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ์–ธ
    
    for y in range(len(park)): # ๊ณต์›์„ ์ด์ค‘for๋ฌธ์œผ๋กœ ๋Œ๋ฉด์„œ
        for x in range(len(park[0])):
            if park[y][x] == 'X': block.append([y, x]) 
            # ์žฅ์• ๋ฌผ์ด ์žˆ๋‹ค๋ฉด block๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
            
            elif park[y][x] == 'S': dog = {'y': y, 'x': x} 
            # ๊ฐœ์˜ ํ˜„์žฌ์œ„์น˜๋ฅผ ์ฐพ์•˜๋‹ค๋ฉด dog์˜ ์ขŒํ‘œ๋ฅผ ๋ณ€๊ฒฝ
            
    for route in routes: ๋ฃจํŠธ๋“ค์„ ๋Œ๋ฉด์„œ
        op, n = route.split() # ๋™์„œ๋‚จ๋ถ ์œ„์น˜๋ฅผ ๋‹ด์„ op, ์–ผ๋งˆ๋‚˜ ์ด๋™ํ• ์ง€ n
        n = int(n)
        move = {'E': {'y': 0, 'x': n}, 'S': {'y': n, 'x': 0},'W': {'y': 0, 'x': -n}, 'N': {'y': -n, 'x': 0}}
        # ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•จ.
        
        if 0 <= (dog['y'] + move[op]['y']) < height and 0 <= (dog['x'] + move[op]['x']) < width:
        # ๊ฐœ์˜ ํ˜„์žฌ ์œ„์น˜์—์„œ ์ด๋™ํ–ˆ์„๋•Œ ๊ณต์›์˜ ์ขŒํ‘œ๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋Š”์ง€ ๊ฒ€์‚ฌํ•จ.
        
            check_block = True
            # ์ด๋™ํ•  ๊ฒฝ๋กœ์— ๋ธ”๋ก์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
            
            for y, x in block: # ์ฒ˜์Œ์— ์žฅ์• ๋ฌผ๋“ค์˜ ์ขŒํ‘œ๋ฅผ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ๋“ค์„ ๋Œ๋ฉด์„œ
            
            	# min max ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” ๋งˆ์ด๋„ˆ์Šค ์ขŒํ‘œ๋กœ ์ด๋™ํ• ์ˆ˜ ์žˆ๊ณ ,
                # ํ”Œ๋Ÿฌ์Šค ์ขŒํ‘œ๋กœ ์ด๋™ํ• ์ˆ˜์žˆ๊ธฐ ๋•Œ๋ฌธ์— min, max๋ฌธ์œผ๋กœ ๋‹ค์–‘ํ•œ ๊ฒฝ์šฐ๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉ์ณค์Œ.
                if min(dog['y'], dog['y'] + move[op]['y']) <= y <= max(dog['y'], dog['y'] + move[op]['y']):
                    if min(dog['x'], dog['x'] + move[op]['x']) <= x <= max(dog['x'], dog['x'] + move[op]['x']):
                        # ํ˜„์žฌ ๊ฐœ์˜ ์œ„์น˜์™€ ์ด๋™ํ•  ์œ„์น˜ ์‚ฌ์ด์— ์žฅ์• ๋ฌผ์ด ์žˆ๋‹ค๋ฉด
                        # ์ด๋™ํ•˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ check_block ๋ณ€์ˆ˜๋ฅผ false๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ์ข…๋ฃŒ
                        check_block = False
                        break
                        
            if check_block: # ์ด๋™ํ•  ๊ฒฝ๋กœ์— ์žฅ์• ๋ฌผ์ด ์—†๋‹ค๋ฉด ๊ฐœ์˜ ์œ„์น˜๋ฅผ ์ด๋™์‹œํ‚ด
                dog['y'] += move[op]['y']
                dog['x'] += move[op]['x']
        
    return [dog['y'],dog['x']]
 """
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€