방향 회전 조건이 까다로워서 구현하기 어려웠던 문제였다
우선 방문처리를 위한 d 리스트와 회전을 위한 turn_left() 함수를 구현했다
while문 조건을 True로 설정해주고
1) 우선 turn_left()를 실행시켜 방향을 갱신하고, nx와 ny를 갱신했다
2) 만약 청소할 수 있는 구역이 있다면 방문, 청소, 이동을 처리해주고 turn 횟수를 다시 0으로 초기화해주고,
청소할 수 있는 구역이 없다면 turn+1을 해주었다
3) 만약 turn이 4가 됐을 경우는 한 칸 뒤 위치가 벽이 아니라면 후진처리와 turn을 0으로 초기화해주었고, 벽이면 break를 해주었다
소스 코드
n, m = map(int, input().split())
x, y, direction = map(int, input().split())
d = [[0] * m for _ in range(n)]
d[x][y] = 1
graph = []
for i in range(n):
graph.append(list(map(int, input().split())))
# 북, 동, 남, 서
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
def turn_left():
global direction
direction -= 1
if direction == -1:
direction = 3
count = 1
turn = 0
while True:
turn_left()
nx = x + dx[direction]
ny = y + dy[direction]
if d[nx][ny] == 0 and graph[nx][ny] == 0:
d[nx][ny] = 1
x = nx
y = ny
count += 1
turn = 0
continue
else:
turn += 1
if turn == 4:
nx = x - dx[direction]
ny = y - dy[direction]
if graph[nx][ny] == 0:
x = nx
y = ny
else:
break
turn = 0
print(count)