📖 문제 링크
https://www.acmicpc.net/problem/14503
14503번: 로봇 청소기
로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어
www.acmicpc.net
✅ 최종 코드
from collections import deque
# 0:북 / 1:동 / 2:남 / 3:서
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
def bfs(direction):
global answer
x, y = robot.popleft()
if board[x][y] == 0:
board[x][y] = 2 #벽이아닌, 청소한곳은 2
answer += 1
for i in range(4):
nd = (direction+3)%4
nx = x+dx[nd]
ny = y+dy[nd]
# 청소안된곳 청소하고 회전 후 전진, 청소 반복 = bfs
if board[nx][ny] == 0:
robot.append([nx, ny])
bfs(nd)
return
direction = nd
#네방향 모두 청소가 되어있거나 벽인경우
nd = (direction+2) % 4
nx = x + dx[nd]
ny = y + dy[nd]
if board[nx][ny] != 1 :
robot.append([nx, ny])
bfs(direction)
#후진불가
else:
return
n, m = map(int, input().split())
r, c, d = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(n)]
robot = deque()
robot.append([r, c])
answer = 0
bfs(d)
print(answer)
'알고리즘 > Python' 카테고리의 다른 글
[백준/Python] 7562번 : 나이트의 이동 (0) | 2022.04.14 |
---|---|
[백준/Python] 7576번: 토마토 (JAVA버전 추가) (0) | 2022.04.13 |
[백준/Python] 14499번 : 주사위 굴리기 (0) | 2022.03.31 |
[백준/Python] 13458번 : 시험 감독 (0) | 2022.03.30 |
[백준/Python] 10610번: 30 (0) | 2022.03.06 |