
HSAT 1회 정기 코딩 인증평가 기출 | 로봇이 지나간 경로
하 진짜 내 머리를 터지게 한 너 짜증난다 후하 !
문제에서 핵심은 시작점과 끝점을 파악하고서 두 점중 어디서 어느 방향을 보며 출발할 것 인지를 파악하는것이다.
그래서 나는 check_map 함수를 이용해서 시작 지점이랑 방향을 결정하고 지금까지 이동한 방향을 배열에 저장했다.
그리고 이 문제는 명령 개수 최소화 방법이 여러개라면 그 중 한가지만 출력하게 되어있으므로 마지막에 방법을 찾았다면 sys.exit()으로 종료시켜버렸다.
이 문제 .. 어려워요 .. 사실 너무 헷갈리게 짜서 좋은 코드가 아니라고 생각하지만 그래도 억지로라도 맞았다 히히
import sys
from collections import deque
H, W = map(int, input().split())
maps = [list(sys.stdin.readline()) for _ in range(H)]
visited = [[False] * W for _ in range(H)]
num_directions = [[-1, 0], [0, -1], [1, 0], [0, 1]]
directions = ['^', '<', 'v', '>']
ans = []
def check_map(x, y):
cnt = 0
for i in range(len(num_directions)):
dx, dy = num_directions[i][0], num_directions[i][1]
mx, my = x + dx, y + dy
if 0 <= mx < H and 0 <= my < W and maps[mx][my] == '#':
start = directions[i]
cnt += 1
if cnt > 1:
return False
return start
def bfs(x, y):
path = deque([])
q = deque([[x, y]])
visited[x][y] = True
while q:
cx, cy = q.popleft()
for i in range(len(num_directions)):
dx, dy = num_directions[i][0], num_directions[i][1]
mx, my = cx + dx, cy + dy
if 0 <= mx < H and 0 <= my < W and maps[mx][my] == '#' and not visited[mx][my]:
visited[mx][my] = True
path.append(directions[i])
q.append([mx, my])
return path
for i in range(H):
for j in range(W):
if maps[i][j] == '#' and check_map(i, j):
path = bfs(i, j)
print(i + 1, j + 1)
print(path[0])
current = path.popleft()
cnt = 1
for next in path:
if current == next:
cnt += 1
current = next
if cnt % 2 == 0:
ans.append("A")
cnt = 0
else:
if directions[directions.index(current) - 1] == next:
ans.append('R')
else:
ans.append('L')
current = next
cnt = 1
for i in ans:
print(i, end="")
sys.exit()
