구현
- 구현문제라 특벽한 로직은 없습니다.
- 문제에 맞게 북동남서를 설정(dr,dc)
- 왼쪽 회전 방향을 구하기 위해 나머지 연산 사용(
nd = (d+3)%4
)
- 2-1 조건에 부합할 경우에만 break를 걸어 첫번쨰 반복으로 돌아와서 청소
- 그 외의 경우에는 두번째 반복문에서 반복
- 변수에서 now를 사용했는데, 그냥 r,c,d로 직접 할당 했으면 이해가 더 쉬웠을거 같다
import sys
# N = row , M = column
N,M = map(int,sys.stdin.readline().split())
position = tuple(map(int,sys.stdin.readline().split()))
arr = [list(map(int,sys.stdin.readline().split())) for _ in range(N)]
# 북동남서
dr,dc = (-1,0,1,0),(0,1,0,-1)
def solve(now:tuple[int,int,int])->int:
ans = 0
while True:
r,c,d = now
# 현재 위치 청소
arr[r][c] = 2
ans += 1
cnt = 0
while True:
# 왼쪽
nd = (d+3)%4
nr,nc = r+dr[nd],c+dc[nd]
# 왼쪽이 비어 있을 경우
if arr[nr][nc] == 0:
now = (nr,nc,nd)
break
else:
# 방향전환만 함
if cnt < 4:
d = nd
cnt += 1
# 네방향 다 청소가 되어있음
else:
nr,nc = r-dr[d],c-dc[d]
# 후진
if arr[nr][nc] != 1:
r,c = nr,nc
cnt = 0
# 더이상 불가
else:
return ans
ans = solve(position)
print(ans)