Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- GAN
- 스마트팩토리
- re_lunchu
- Algorithm
- JavaScript
- Python
- 소프티어
- programmers
- 현대
- 파이썬
- 백준
- alogorithm
- 토이프로젝트
- softeer
- boj
- 프로그래머스
- 힙큐
- Baekjoon
- Java
- 현대자동차
- 자바스크립트
- 알고리즘
- 탐욕법
- 자바
- 오블완
- cs공부
- 딥러닝
- 티스토리챌린지
- cim
- heapq
Archives
- Today
- Total
eaz_coding
[Softeer] 로봇이 지나간 경로 본문
문제
요약
로봇이 이동한 경로를 가기 위한 첫 시작 지점과 방향, 이동하기 위한 명령어를 찾으시오.
원본
https://softeer.ai/practice/6275
Softeer - 현대자동차그룹 SW인재확보플랫폼
softeer.ai
풀이
시뮬레이션형 문제로 시간은 걸리지만 금방 풀 수 있는 문제 같다.
처음 헤맸던 부분, 방향을 바꾸면 무조건 두칸을 이동하는 것을 안해줬다.
좌표 상에서 방향을 탐색할 때, 원래 진행방향과 방향이 다르면 왼쪽 혹은 오른쪽으로 이동한 뒤
한칸을 이동한 위치가 된다. 따라서 방향 전환하고 두칸 전진!
import sys
from collections import deque
input = sys.stdin.readline
h, w = map(int, input().split())
arr = [list(input().strip()) for _ in range(h)]
visited = [[0]*w for _ in range(h)]
d = [(-1, 0), (0,-1), (1,0), (0,1)]
def findStart():
for i in range(h):
for j in range(w):
cnt = 0
if arr[i][j] == "#":
for di, dj in d:
ni, nj = i+di, j+dj
if 0 <= ni < h and 0 <= nj < w and arr[ni][nj] =="#":
cnt += 1
if cnt == 1:
return i, j
def move():
q = deque([(si, sj)])
visited[si][sj] = 1
answer = ""
nd = -1
directions = ['^', '<', 'v', '>']
while q:
x, y = q.popleft()
visited[x][y] = 1
for i in range(4):
nx, ny = x+d[i][0], y+d[i][1]
if 0 <= nx < h and 0 <= ny < w and arr[nx][ny] == "#" and visited[nx][ny] == 0:
q.append((nx, ny))
visited[nx][ny] = 1
if x == si and y == sj:
print(directions[i])
answer += 'A'
tx, ty = q.pop()
q.append((tx+d[i][0], ty+d[i][1]))
else:
if nd != i:
if nd == 0:
answer += 'L' if i == 1 else 'R'
elif nd == 1:
answer += 'L' if i == 2 else 'R'
elif nd == 2:
answer += 'L' if i == 3 else 'R'
elif nd == 3:
answer += 'L' if i == 0 else 'R'
answer += 'A'
tx, ty = q.pop()
q.append((tx+d[i][0], ty+d[i][1]))
nd = i
break
return answer
si, sj = findStart()
print(si+1, sj+1)
answer = move()
print(answer)
'eaz_algorithm' 카테고리의 다른 글
[Programmers] 석유 시추 (0) | 2024.03.28 |
---|---|
[Softeer] 나무 섭지 (1) | 2024.03.22 |
[Softeer] 함께하는 효도 (2) | 2024.03.20 |
[Programmers] 다리를 지나는 트럭 (0) | 2024.03.19 |
[Softeer] 동계 테스트 시점 예측 (2) | 2024.03.18 |