728x90

구현 문제. 차근차근 구현하면 어렵지 않다.

스택을 사용할 필요는 없는데 머리에서 그려보기 편해서 써봤다.

from sys import stdin

R, C = map(int, stdin.readline().split())
r, c, d = map(int, stdin.readline().split())
arr = [list(map(int, stdin.readline().split())) for x in range(R)]
visited = [[0]*C for x in range(R)]

# 'left', 'bottom', 'right', 'up'
head = [(0,-1), (1,0), (0,1), (-1,0)]

# 바라보는 방향 init
idx_head = 3 - d

result = 0

stack = [(r, c)]
while stack:
    cr, cc = stack.pop()

    if not visited[cr][cc]:
        visited[cr][cc] = 1
        result += 1
    able = False
    # 방향전환으로 갈 수 있는 방향 찾기
    # 왼쪽부터 가야 하므로 방향부터 옮기고 찾기
    for i in range(4):
        idx_head = (idx_head+1)%4
        # 갈 수 있으면 전진 (스택에 추가)
        nr, nc = cr + head[idx_head][0], cc + head[idx_head][1]
        if 0 <= nr < R and 0 <= nc < C and not visited[nr][nc] and not arr[nr][nc]:
            stack.append((nr, nc))
            able = True
            break

    # for문을 빠져나왔다는건 다 돌았는데도 갈데가 없다는 것
    # 후진 가능하면 후진
    br, bc = cr - head[idx_head][0], cc - head[idx_head][1]
    if not able:
        if 0 <= br < R and 0 <= bc < C and not arr[br][bc]:
            stack.append((br, bc))
print(result)
  • 네이버 블로그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기