728x90
안녕하세요. 이번주는 구현 문제를 풀어보려고 합니다.
https://www.acmicpc.net/problem/14503
14503번: 로봇 청소기
첫째 줄에 방의 크기 $N$과 $M$이 입력된다. $(3 \le N, M \le 50)$ 둘째 줄에 처음에 로봇 청소기가 있는 칸의 좌표 $(r, c)$와 처음에 로봇 청소기가 바라보는 방향 $d$가 입력된다. $d$가 $0$인 경우 북쪽
www.acmicpc.net
저는 해당 문제를 각각의 기능을 함수화 하였고, 조건문으로 문제를 해결하였습니다. cleaned라는 배열을 통하여 빈방들의 청소 여부를 확인 합니다. 재귀 함수를 통하여 1번 위치로 돌아가는 방식으로 구현하였습니다.
N, M = map(int, input().split())
direction = [0, 1, 2, 3] # 북 동 남 서 현재 방향
moveback = [[1,0], [0,-1], [-1,0], [0,1]] # 뒤로 이동
movefront = [[-1,0], [0,1], [1,0], [0,-1]] # 앞으로 이동
room = []
robot = list()
robotdirection = 0
for _ in range(1):
i,j,k = map(int, input().split())
robot.append(i)
robot.append(j)
robotdirection = k
for _ in range(N):
room.append(list(map(int, input().split())))
cleaned = [[False for _ in range(M)] for _ in range(N)]
counter = 0
def robotCleaner(i,j, status):
global counter
if not (N > i >=0 and M > j >= 0) :
return
if not cleaned[i][j]:
cleaned[i][j] = True
counter+=1 # 청소한 방 개수 카운트
if checkCleanRoom(i+1,j) and checkCleanRoom(i-1,j) and checkCleanRoom(i,j+1) and checkCleanRoom(i,j-1): # 현재 칸의 주변 4칸 중 청소되지 않은 빈 칸이 없는 경우
for k in range(len(direction)):
if direction[k] == status:
di, dj = moveback[k]
if checkWall(di+i, dj+j): # 바라보는 방향의 뒤쪽 칸이 벽이라 후진할 수 없다면 작동을 멈춘다.
return
else: # 바라보는 방향을 유지한 채로 한 칸 후진할 수 있다면 한 칸 후진하고 1번으로 돌아간다.
robotCleaner(di+i, dj+j, status)
else: # 현재 칸의 주변 4칸 중 청소되지 않은 빈 칸이 있는 경우
cur = Rotate(status) # 반시계 방향으로 90도 회전한다.
di, dj = movefront[direction.index(cur)]
if not checkCleanRoom(di+i, dj+j):
robotCleaner(di+i, dj+j, cur) # 바라보는 방향을 기준으로 앞쪽 칸이 청소되지 않은 빈 칸인 경우 한 칸 전진한다. 1번으로 돌아간다.
else:
robotCleaner(i,j, cur) # 그렇지 않은경우 전진하지않고 1번으로 돌아간다.
def Rotate(status):
cur = direction.index(status)
if 0 > cur-1:
return direction[3]
else:
return direction[cur-1]
def checkWall(i,j):
if N > i >=0 and M > j >= 0:
if room[i][j] == 1:
return True
else :
return False
else:
return True
def checkCleanRoom(i,j):
if N > i >=0 and M > j >= 0 and room[i][j] == 0 and not cleaned[i][j]:
return False
else :
return True
robotCleaner(robot[0], robot[1], robotdirection)
print(counter)
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 1463번 : 1로 만들기 (2) | 2023.07.13 |
---|---|
[백준] 14500번 : 테트로미노 (0) | 2023.07.04 |
[백준] 1261번 : 알고스팟 (0) | 2023.07.02 |
[백준] 15686번 : 치킨거리 (0) | 2023.07.02 |
[백준] 3109번 : 빵집 (0) | 2023.07.02 |