[문제]
https://www.acmicpc.net/problem/14503
주어진 조건을 순서대로 그대로 구현하면 되는 문제입니다.
[초기화]
def __init__(self):
self.n, self.m = map(int, input().split())
self.r, self.c, self.d = map(int, input().split())
self.grid = [list(map(int, input().split())) for _ in range(self.n)]
self.answer = 0
[풀이]
def solve(self):
directions = [[-1, 0], [0, 1], [1, 0], [0, -1]]
while True:
if self.grid[self.r][self.c] == 0: # 1. 현재 칸이 아직 청소되지 않은 경우, 현재 칸을 청소한다.
self.grid[self.r][self.c] = -1 # 청소
self.answer += 1 # 청소한 구역의 수 갱신
elif self.grid[self.r - 1][self.c] != 0 and \
self.grid[self.r + 1][self.c] != 0 and \
self.grid[self.r][self.c - 1] != 0 and \
self.grid[self.r][self.c + 1] != 0:
# 2. 현재 칸의 주변 4칸 중 청소되지 않은 빈 칸이 없는 경우(벽 포함)
dr, dc = directions[self.d] # 이동 방향
mr, mc = self.r - dr, self.c - dc # 후진 위치
if self.grid[mr][mc] != 1: # 2.1. 바라보는 방향을 유지한 채로 한 칸 후진할 수 있다면 한 칸 후진하고 1번으로 돌아간다.
self.r, self.c = mr, mc
else: # 2.2. 바라보는 방향의 뒤쪽 칸이 벽이라 후진할 수 없다면 작동을 멈춘다.
break
else: # 3. 현재 칸의 주변 4칸 중 청소되지 않은 빈 칸이 있는 경우
self.d = 3 if self.d - 1 < 0 else self.d - 1 # 3.1. 반시계 방향으로 90도 회전
dr, dc = directions[self.d]
mr, mc = self.r + dr, self.c + dc
if self.grid[mr][mc] == 0: # 3.2. 바라보는 방향을 기준으로 앞쪽 칸이 청소되지 않은 빈 칸인 경우 한 칸 전진한다.
self.r, self.c = mr, mc
print(self.answer)
- 현재 위치가 청소되지 않은 방이라면 청소
- 현재 위치 네 방향이 전부 청소되지 않은 방이거나, 전부 벽이거나 청소되지 않은 방 + 벽이라면
- 후진했을 때, 이동가능한 방(청소되지 않은 방)이 있다면 이동
- 그렇지 않다면 청소 종료
- 위 두 조건에 충족되지 않는다면, 현재 칸 주변에 청소되지 않은 칸이 존재
- 반시계 방향으로 회전
- 회전한 방향에 맞춰 이동가능할 때, 빈칸인 경우 이동
[전체 코드]
class Main:
def __init__(self):
self.n, self.m = map(int, input().split())
self.r, self.c, self.d = map(int, input().split())
self.grid = [list(map(int, input().split())) for _ in range(self.n)]
self.answer = 0
def solve(self):
directions = [[-1, 0], [0, 1], [1, 0], [0, -1]]
while True:
if self.grid[self.r][self.c] == 0: # 1. 현재 칸이 아직 청소되지 않은 경우, 현재 칸을 청소한다.
self.grid[self.r][self.c] = -1 # 청소
self.answer += 1 # 청소한 구역의 수 갱신
elif self.grid[self.r - 1][self.c] != 0 and \
self.grid[self.r + 1][self.c] != 0 and \
self.grid[self.r][self.c - 1] != 0 and \
self.grid[self.r][self.c + 1] != 0:
# 2. 현재 칸의 주변 4칸 중 청소되지 않은 빈 칸이 없는 경우(벽 포함)
dr, dc = directions[self.d] # 이동 방향
mr, mc = self.r - dr, self.c - dc # 후진 위치
if self.grid[mr][mc] != 1: # 2.1. 바라보는 방향을 유지한 채로 한 칸 후진할 수 있다면 한 칸 후진하고 1번으로 돌아간다.
self.r, self.c = mr, mc
else: # 2.2. 바라보는 방향의 뒤쪽 칸이 벽이라 후진할 수 없다면 작동을 멈춘다.
break
else: # 3. 현재 칸의 주변 4칸 중 청소되지 않은 빈 칸이 있는 경우
self.d = 3 if self.d - 1 < 0 else self.d - 1 # 3.1. 반시계 방향으로 90도 회전
dr, dc = directions[self.d]
mr, mc = self.r + dr, self.c + dc
if self.grid[mr][mc] == 0: # 3.2. 바라보는 방향을 기준으로 앞쪽 칸이 청소되지 않은 빈 칸인 경우 한 칸 전진한다.
self.r, self.c = mr, mc
print(self.answer)
problem = Main()
problem.solve()
보시다시피 문제에서 주어진 조건을 그대로 구현하면 되는 문제였습니다.
'Algorithm > 백준' 카테고리의 다른 글
[백준: Java] 2169 - 로봇 조종하기 (0) | 2024.11.13 |
---|---|
[백준: Python] 2234 - 성곽 (2) | 2024.11.12 |
[백준: Java] 14442 - 벽 부수고 이동하기 2 (3) | 2024.11.10 |
[백준: Python] 14002 - 가장 긴 증가하는 부분 수열 4 (1) | 2024.11.09 |
[백준: Java] 2295 - 세 수의 합 (0) | 2024.11.08 |