https://www.acmicpc.net/problem/14503
공부 날짜 : 2022.08.29
정답 참조 여부 : X
로봇청소기의 알고리즘은 정해져있고 정해진 상황에서 얼마나 넓은 영역을 청소 가능한건지 출력하는 문제로 문제에서 주어진대로 구현만 하면 되는 문제였다.
0 -> 1 -> 2 -> 3 의순서로 방향이 정해져 있었는데 dx,dy만 신경써주고 왼쪽을 본다고 할때 크게 신경쓰지 않고
dir = (dir + 1) % 4
로 작성했다가 오른쪽 회전하는대로 되어서 오답이 나왔었다.
수정 후에는 RecursionError가 떴고 재귀깊이만 설정해주면 되는 문제라
sys.setrecursionlimit(10**6)
코드만 추가 해 주니 정답으로 나왔다
import sys
input = sys.stdin.readline
#RecursionError에러방지용 재귀 깊이 설정
sys.setrecursionlimit(10**6)
n,m = map(int, input().split())
x,y,dir = map(int,input().split())
graph = []
for _ in range(n):
graph.append(list(map(int,input().split())))
#0->북 1->동 2->남 3->서
dx = [-1,0,1,0]
dy = [0,1,0,-1]
result = 0
def step1():
global result,graph,x,y,dir
#청소한 구역을 2로 표시
graph[x][y] = 2
result += 1
step2()
#graph가 제시되는 조건에서 모든 첫행과 마지막행, 첫열 마지막열은 벽으로 주어지므로 index판단은 제외
def step2(count = 0):
global result,graph,x,y,dx,dy,dir
if count == 4:
nx = x - dx[dir]
ny = y - dy[dir]
if graph[nx][ny] != 1:
x,y = nx,ny
step2()
else:
return
else:
dir = (dir - 1) % 4
nx = x + dx[dir]
ny = y + dy[dir]
if graph[nx][ny] == 0:
x,y = nx, ny
step1()
elif graph[nx][ny] == 1 or graph[nx][ny] == 2:
step2(count + 1)
step1()
print(result)