백준 14503번 로봇청소기
https://www.acmicpc.net/problem/14503
PYTHON
N, M = map(int, input().split())
r, c, d = map(int, input().split())
dy = [-1, 0, 1, 0]
dx = [0, 1, 0, -1]
direction = [3, 0, 1, 2]
board = []
for i in range(N):
board.append(list(map(int, input().split())))
def check(location, r, c, d):
new_d = direction[d]
new_r = r + dy[new_d]
new_c = c + dx[new_d]
if board[new_r][new_c] == 0:
board[new_r][new_c] = 2
location.append([new_r, new_c, new_d])
return True
return False
def fresh(board, r, c, d):
location = [[r, c, d]]
board[r][c] = 2
count = 1
while location:
r, c, d = location.pop()
can_fresh = False
for i in range(4):
if check(location, r, c, d):
count+=1
can_fresh = True
break
else:
d = direction[d]
if can_fresh == False:
if board[r- dy[d]][c-dx[d]] != 1:
location.append([r - dy[d], c - dx[d], d])
return count
print(fresh(board, r, c, d))
이번 문제는 주어진 규칙이 있는 로봇청소기를 구현하는 문제였습니다. 코드의 길이와 깔끔함에 있어서는 다양하게 차이가 나겠지만 구현하는 일은 조건을 상세하게 따져가며 하다보면 잘 풀리는 것 같습니다!
여러가지 어플리케이션과 웹사이트를 구현해보며 여기저기서 굴렀던 실력이 이런 문제에서 발휘가 되지 않나 싶습니다 ㅎㅎ
행복
이 문제 풀고 나서 게시판에서 다른 사람들의 문제 풀이를 구경하던 도중
어떤 분이 자신의 멍청한 로봇청소기좀 고쳐달라고 글을 올리셨더군요 ㅋㅋㅋㅋㅋㅋ
백준 문제 풀면서 처음 웃어봤습니다 :) 감사합니다!