나의 풀이
1. 성공
n, m = map(int, input().split())
x, y, d = map(int, input().split())
space = [list(map(int, input().split())) for _ in range(n)]
dir = [0, 1, 2, 3]
go = [(-1, 0), (0, 1), (1, 0), (0, -1)]
rotation = 0
count = 0
while True:
back_d = dir[d - 2]
new_x = x + go[back_d][0]
new_y = y + go[back_d][1]
if rotation == 4:
if space[new_x][new_y] == 1
or new_x < 0 or new_x > n
or new_y < 0 or new_y > m:
break
else:
x = new_x
y = new_y
count += 1
else:
next_d = dir[d - 1]
new_x = x + go[next_d][0]
new_y = y + go[next_d][1]
d = next_d
if space[new_x][new_y] == 1
or space[new_x][new_y] == 2
or new_x < 0 or new_x > n
or new_y < 0 or new_y > m:
rotation += 1
continue
space[x][y] = 2
x = new_x
y = new_y
rotation = 0
count += 1
print(count)