๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป CS

๋ฏธ๋กœ ํƒˆ์ถœ ๋ช…๋ น์–ด

by dev.py 2023. 2. 2.

์‹œ๋„ ํ–ˆ๋˜ ๋ฐฉ๋ฒ• - BFS (๊ฐ„๋งŒ์— BFS ๋กœ ํ’€๊ฒ ๋‹ค๊ณ  ๋งˆ์Œ๋จน๋Š” ๋ฐ”๋žŒ์—..)

 

๊ฒฐ๋ก  -

 

  1. ๋จผ์ € ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐํ•ด์„œ ๋˜๋‚˜ ์•ˆ๋˜๋‚˜ ํ™•์ธํ›„
  2. ๋ณ€์œ„๋Ÿ‰์„ ๊ณ„์‚ฐํ•ด์„œ d, l, r, u ์จ์•ผ ๋˜๋Š”๊ฑฐ ๊ณ„์‚ฐํ•˜๊ณ 
  3. ๋‚จ์€ ์นด์šดํŠธ์—์„œ d, l๋ฅผ ๋จผ์ €๊ฐ€์„œ (์ตœ๋Œ€ (n,1)) ๊ฐ„๋‹ค
  4. ๋‹ค์Œ์— ๊ทธ๋ž˜๋„ ์นด์šดํŠธ๊ฐ€ ๋‚จ์œผ๋ฉด rlrlrlrlrl ๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

 

 

 

 

from collections import deque

# def solution(n, m, x, y, r, c, k):
#     answer = ''
#     q = deque([(x,y,"", 0)])
#     while q:
#         pos_x, pos_y, path, count = q.popleft()
#         print(pos_x, pos_y, path, count)
        
#         if count == k :
#             if pos_x == r and pos_y == c :
#                 # print(pos_x, pos_y, path, count)
#                 answer = path
#                 return answer
#             else :
#                 continue
#         if  abs(r-pos_x) + abs(c-pos_y) > k -count :
#             print("๊ฐ€์ง€์น˜๊ธฐ ",pos_x, pos_y, path, count )
#             continue
#         if pos_x < n : # down
#             q.append((pos_x+1,pos_y,path+"d", count+1))
#         if pos_y > 1 : # left 
#             q.append((pos_x,pos_y-1,path+"l", count+1))
#         if pos_y < m : # right
#             q.append((pos_x,pos_y+1,path+"r", count+1))
#         if pos_x > 1 : # up
#             q.append((pos_x-1,pos_y,path+"u", count+1))
            
#     answer = "impossible"
#     return answer

def solution(n, m, x, y, r, c, k):
    answer = ''
    dist = abs(x-r)+abs(y-c)
    k -= dist
    if k < 0 or k%2 != 0:
        return "impossible"
    
    direction = {'d':0, 'l':0, 'r':0, 'u':0}
    if x > r:
        direction['u'] += x-r
    else:
        direction['d'] += r-x
    if y > c:
        direction['l'] += y-c
    else:
        direction['r'] += c-y
        
    answer += 'd'*direction['d']
    d = min(int(k/2), n-(x+direction['d']))
    answer += 'd'*d
    direction['u'] += d
    k -= 2*d
    
    answer += 'l'*direction['l']
    l = min(int(k/2), y-direction['l']-1)
    answer += 'l'*l
    direction['r'] += l
    k -= 2*l
    
    answer += 'rl'*int(k/2)
    answer += 'r'*direction['r']
    answer += 'u'*direction['u']
    return answer