오구의코딩모험

[Python] 14503번 : 로봇 청소기 본문

프로그래밍 공부/백준 알고리즘

[Python] 14503번 : 로봇 청소기

오구.cpp 2023. 2. 16. 00:35
반응형

 

문제 3줄 요약

1. 로봇 청소기가 청소를 한다.

2. 위의 3 가지 패턴을 반복하며 작동한다.

3. 로봇 청소기가 멈출 때까지 청소한 칸의 개수를 출력하라

 

 

 

내용이 길지만,

기능을 차근차근 하나씩 구현해보자!

 

from sys import stdin
## 1번 기능 : 해당 칸 청소
def func_1(input_list,room):
x = input_list[0]
y = input_list[1]
if room[x][y] == 0:
room[x][y] = 2
return 1
return 0
## 2번 기능 : 상하좌우가 청소된 경우
def func_2(input_list,room):
x = input_list[0]
y = input_list[1]
direction = input_list[2]
## Right
if y+1 < M:
if room[x][y+1] == 0:
return 0
## Left
if y > 0:
if room[x][y-1] == 0:
return 0
## Up
if x > 0:
if room[x-1][y] == 0:
return 0
## Down
if x+1 < N:
if room[x+1][y] == 0:
return 0
## 2-1 : 후진
if (direction == 0) and (x+1 < N):
if room[x+1][y] == 2:
input_list[0] += 1
return 1
elif (direction == 1) and (y > 0):
if room[x][y-1] == 2:
input_list[1] -= 1
return 1
elif (direction == 2) and (x > 0):
if room[x-1][y] == 2:
input_list[0] -= 1
return 1
elif (direction == 3) and (y+1 < M):
if room[x][y+1] == 2:
input_list[1] += 1
return 1
## 2-2 : 작동 멈춤
return 2
## 3번 기능 : 상하좌우 중 청소할 칸이 있는 경우
def func_3(input_list,room):
x = input_list[0]
y = input_list[1]
if input_list[2] == 0:
input_list[2] = 3
else:
input_list[2] -= 1
## 3-1 : 반시계 90도 회전
direction = input_list[2]
## 3-2 : 청소할 칸으로 한칸 전진
if (direction == 0) and (x > 0):
if room[x-1][y] == 0:
input_list[0] -= 1
return 1
elif (direction == 1) and (y+1 < M):
if room[x][y+1] == 0:
input_list[1] += 1
return 1
elif (direction == 2) and (x+1 < N):
if room[x+1][y] == 0:
input_list[0] += 1
return 1
elif (direction == 3) and (y > 0):
if room[x][y-1] == 0:
input_list[1] -= 1
return 1
else:
return 0
if __name__ == "__main__":
## 방의 크기 N, M
N, M = map(int, stdin.readline().split())
## 처음 로봇 청소기 좌표 및 방향
input_list = list(map(int, stdin.readline().split()))
clean_cnt = 0
room = []
for x in range(N):
line = []
## 각 장소의 상태
for y in map(int, stdin.readline().split()):
line.append(y)
room.append(line)
clean_cnt += func_1(input_list,room)
## 청소 작동
while(True):
case = func_2(input_list,room)
if case == 0:
if func_3(input_list,room):
clean_cnt += func_1(input_list,room)
elif case == 1:
continue
elif case == 2:
break
## 청소한 칸 출력
print(clean_cnt)

 

특별한 알고리즘 없이 구현하는 문제였기에

어렵지 않았지만,

 

문제 설명을 제대로 이해하지 못해 시간을 많이 허비했다..

질문 게시판을 보니 다들 문제 설명이 헷갈렸던 모양이다.

3-3에서 1번으로 간다는 것이 다시 처음으로 돌아가는건지,

3-1로 돌아가는건지 직관적으로 이해가 되지않아

테스트케이스 틀린 것을 수정해가며 이해한 문제다.

 

내 독해력에 문제가 있나...?

 

여튼,

끝!

반응형

'프로그래밍 공부 > 백준 알고리즘' 카테고리의 다른 글

[Python] 7576번 : 토마토  (0) 2023.02.16
[Python] 15686번 : 치킨 배달  (0) 2023.02.16
[Python] 14502번 : 연구소  (0) 2023.02.15
[Python] 1351번 : 무한 수열  (0) 2023.02.14
[Python] 1043번 : 거짓말  (0) 2023.02.14
Comments