728x90
반응형
https://www.acmicpc.net/problem/14503
14503번: 로봇 청소기
로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어
www.acmicpc.net
<전체 코드>
import sys
N, M = map(int, sys.stdin.readline().split())
r, c, d = map(int, sys.stdin.readline().split())
# 0 북, 1 동, 2 남, 3 서
graph = []
for _ in range(N):
graph.append(list(map(int, sys.stdin.readline().split())))
now_row, now_col = r, c
graph[now_row][now_col] = 2
count = 1
leftrow = [0, -1, 0, 1]
leftcol = [-1, 0, 1, 0]
backrow = [1, 0, -1, 0]
backcol = [0, -1, 0, 1]
while True:
find = False
for i in range(4):
if graph[now_row+leftrow[d]][now_col+leftcol[d]] != 0:
d -= 1
if d == -1:
d = 3
else:
now_row += leftrow[d]
now_col += leftcol[d]
find = True
d -= 1
if d == -1:
d = 3
break
if find:
graph[now_row][now_col] = 2
count += 1
# 4방향 다 청소된 경우
else:
# 벽인경우
if d == 4:
d = 0
if graph[now_row+backrow[d]][now_col+backcol[d]] == 1:
break
# 벽이 아닌 경우
else:
now_row += backrow[d]
now_col += backcol[d]
print(count)
728x90
반응형
'# Coding > # 백준' 카테고리의 다른 글
[백준 / 13305] 주유소 - Python (0) | 2021.07.23 |
---|---|
[백준 / 11779] 최소비용 구하기 2 - Python (0) | 2021.07.23 |
[백준 / 1912] 연속합 - Python (0) | 2021.07.13 |
[백준 / 10422] 괄호 - Python (0) | 2021.07.07 |
[백준 / 4811] 알약 - Python (0) | 2021.07.07 |