์๋ ํ๋ ๋ฐฉ๋ฒ - BFS (๊ฐ๋ง์ BFS ๋ก ํ๊ฒ ๋ค๊ณ ๋ง์๋จน๋ ๋ฐ๋์..)
๊ฒฐ๋ก -
- ๋จผ์ ๊ฑฐ๋ฆฌ ๊ณ์ฐํด์ ๋๋ ์๋๋ ํ์ธํ
- ๋ณ์๋์ ๊ณ์ฐํด์ d, l, r, u ์จ์ผ ๋๋๊ฑฐ ๊ณ์ฐํ๊ณ
- ๋จ์ ์นด์ดํธ์์ d, l๋ฅผ ๋จผ์ ๊ฐ์ (์ต๋ (n,1)) ๊ฐ๋ค
- ๋ค์์ ๊ทธ๋๋ ์นด์ดํธ๊ฐ ๋จ์ผ๋ฉด 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
'๐ป CS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ต์ ๊ณต๋ฐฐ์(์ต๋ ๊ณต์ฝ์) ์ฐพ๊ธฐ - ์ ํด๋ฆฌ๋ ํธ์ ๋ฒ (๋๋จธ์ง ์ ๋ฆฌ) (0) | 2024.01.20 |
---|---|
๋ฐฑ์ค ํ๋ธ ์ฐ๋ํ๊ธฐ (0) | 2024.01.16 |
ํํ ๊ฐ๋ฅํ ์ด์งํธ๋ฆฌ (0) | 2023.01.31 |
์ธ์ฌ๊ณ ๊ณผ (0) | 2023.01.30 |
ํ๋ก๊ทธ๋๋จธ์ค ๋ ๋ฒจ 1 ๋ค ํ๊ณ (0) | 2022.08.11 |