목차

로봇 청소기

ps
링크acmicpc.net/…
출처BOJ
문제 번호14503
문제명로봇 청소기
레벨골드 5
분류

시뮬레이션

시간복잡도O(NM)
인풋사이즈N<=50, M<=50
사용한 언어Python
제출기록32468KB / 92ms
최고기록52ms
해결날짜2022/09/13

풀이

코드

"""Solution code for "BOJ 14503. 로봇 청소기".

- Problem link: https://www.acmicpc.net/problem/14503
- Solution link: http://www.teferi.net/ps/problems/boj/14503

Tags: [Simulation]
"""

import collections
import sys

DELTA = collections.deque([(-1, 0), (0, -1), (1, 0), (0, 1)])
DIRT = '0'
WALL = '1'
CLEAN = '2'


def main():
    N, M = [int(x) for x in input().split()]
    r, c, d = [int(x) for x in input().split()]
    grid = [input().split() for _ in range(N)]

    answer = 0
    DELTA.rotate(d)
    while grid[r][c] != WALL:
        if grid[r][c] == DIRT:
            grid[r][c] = CLEAN
            answer += 1
        dr, dc = DELTA[1]
        nr, nc = r + dr, c + dc
        if grid[nr][nc] == DIRT:
            r, c = nr, nc
            DELTA.rotate(-1)
        elif all(grid[r + dr][c + dc] != DIRT for dr, dc in DELTA):
            dr, dc = DELTA[2]
            r, c = r + dr, c + dc
        else:
            DELTA.rotate(-1)
            
    print(answer)


if __name__ == '__main__':
    main()