https://school.programmers.co.kr/learn/courses/30/lessons/150365
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
from collections import deque
dx = [1, 0, 0, -1]
dy = [0, -1, 1, 0]
command = "dlru"
def solution(n, m, x, y, r, c, k):
min_distance = abs(r-x) + abs(c-y)
if k < min_distance or min_distance%2 != k%2:
return "impossible"
q = deque([('', x, y, 0)])
while q:
s, x, y, cnt = q.popleft()
if cnt == k:
continue
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 < nx <= n and 0 < ny <= m:
if cnt + 1 == k and nx == r and ny == c:
return s+command[i]
if abs(r-nx) + abs(c-ny) + cnt + 1 > k: continue
q.append((s+command[i], nx, ny, cnt+1))
break
return "impossible"
[LeetCode] Longest Palindrome Substring | 투포인터 | 파이썬 알고리즘 인터뷰, 정답, 소스코드 (1) | 2023.06.03 |
---|---|
[파이썬 알고리즘 인터뷰] 가장 흔한 단어 | 정규표현식 re.sub(), Counter (0) | 2023.05.31 |
[프로그래머스] 150367. 표현 가능한 이진트리| 시뮬레이션, 구현 | 파이썬, 소스코드, 정답 (1) | 2023.05.26 |
[파이썬 알고리즘 인터뷰] 자기 자신을 제외한 배열의 곱 | 리스트, 선형 자료구조 (1) | 2023.05.24 |
[프로그래머스] 152995. 인사고과| 정렬, 구현 | 파이썬, 소스코드, 정답 (1) | 2023.05.23 |