[백준] 14503번 로봇 청소기 (bfs, 시뮬레이션, python)

728x90
반응형

www.acmicpc.net/problem/14503

 

14503번: 로봇 청소기

로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어

www.acmicpc.net

방향 회전에 후진할 경우 이동방향, 회전할 경우 이동방향을 계산해서 이동시킨다 

        # 북동남서 0 1 2 3 -> 후진: 2 3 0 1
        # 북동남서 0 1 2 3 -> 회전: 3 0 1 2

from _collections import deque

n,m=map(int,input().split())
vis=[[0]*m for _ in range(n)]
r,c,d=map(int,input().split())

a=[list(map(int,input().split())) for _ in range(n)]

dx,dy=[0,1,0,-1],[-1,0,1,0] #  북동남서


q=deque()
q.append((r,c,d))

res=1
while q:
    y,x,cur=q.popleft()
    a[y][x] = 2
    temp=cur
    for i in range(4):
        # 북동남서 0 1 2 3 -> 후진: 2 3 0 1
        # 북동남서 0 1 2 3 -> 회전: 3 0 1 2
        temp-=1
        if temp==-1: temp=3
        yy, xx = y + dy[temp], x + dx[temp]
        if 0<=yy<n and 0<=xx<m and a[yy][xx]==0:
            res+=1
            a[yy][xx]=2
            q.append((yy,xx,temp))
            break
        elif i==3:
            temp=(cur+2)%4
            yy, xx = y + dy[temp], x + dx[temp]
            if a[yy][xx]==1: print(res)
            else: q.append((yy, xx, cur))  # 방향은 그대로






728x90
반응형
TAGS.

Comments